aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/oggtheora.c
blob: 9cdb4478cdd38c1e893fbf388d8517ee21f62676 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
    Copyright (C) 2005  Måns Rullgård

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
**/

#include <stdlib.h>
#include <theora/theora.h>
#include "avcodec.h"

typedef struct TheoraContext {
    theora_info info;
    theora_state state;
    theora_comment comment;
    ogg_packet op;
} TheoraContext;

static int
Theora_decode_frame(AVCodecContext *ctx, void *outdata, int *outdata_size,
                    uint8_t *buf, int buf_size)
{
    TheoraContext *thc = ctx->priv_data;
    AVFrame *frame = outdata;
    yuv_buffer yuv;

    thc->op.packet = buf;
    thc->op.bytes = buf_size;

    if(theora_decode_packetin(&thc->state, &thc->op))
        return -1;

    theora_decode_YUVout(&thc->state, &yuv);

    frame->data[0] = yuv.y;
    frame->data[1] = yuv.u;
    frame->data[2] = yuv.v;
    frame->linesize[0] = yuv.y_stride;
    frame->linesize[1] = yuv.uv_stride;
    frame->linesize[2] = yuv.uv_stride;

    *outdata_size = sizeof(*frame);
    return buf_size;
}

static int
Theora_decode_end(AVCodecContext *ctx)
{
    TheoraContext *thc = ctx->priv_data;
    theora_info_clear(&thc->info);
    theora_comment_clear(&thc->comment);
    return 0;
}

static int
Theora_decode_init(AVCodecContext *ctx)
{
    TheoraContext *thc = ctx->priv_data;
    int size, hs, i;
    ogg_packet op;
    uint8_t *cdp;

    if(ctx->extradata_size < 6)
        return -1;

    theora_info_init(&thc->info);

    memset(&op, 0, sizeof(op));
    cdp = ctx->extradata;
    size = ctx->extradata_size;

    for(i = 0; i < 3; i++){
        hs = *cdp++ << 8;
        hs += *cdp++;
        size -= 2;

        if(hs > size){
            av_log(ctx, AV_LOG_ERROR, "extradata too small: %i > %i\n",
                   hs, size);
            return -1;
        }

        op.packet = cdp;
        op.bytes = hs;
        op.b_o_s = !i;
        if(theora_decode_header(&thc->info, &thc->comment, &op))
            return -1;
        op.packetno++;

        cdp += hs;
        size -= hs;
    }

    theora_decode_init(&thc->state, &thc->info);

    ctx->width = thc->info.width;
    ctx->height = thc->info.height;
    ctx->time_base.num = thc->info.fps_denominator;
    ctx->time_base.den = thc->info.fps_numerator;
    ctx->pix_fmt = PIX_FMT_YUV420P; /* FIXME: others are possible */

    return 0;
}

AVCodec oggtheora_decoder = {
    "theora",
    CODEC_TYPE_VIDEO,
    CODEC_ID_THEORA,
    sizeof(TheoraContext),
    Theora_decode_init,
    NULL,
    Theora_decode_end,
    Theora_decode_frame,
    0,
    NULL
};