2665
|
1 /**
|
|
2 Copyright (C) 2005 Måns Rullgård
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person
|
|
5 obtaining a copy of this software and associated documentation
|
|
6 files (the "Software"), to deal in the Software without
|
|
7 restriction, including without limitation the rights to use, copy,
|
|
8 modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9 of the Software, and to permit persons to whom the Software is
|
|
10 furnished to do so, subject to the following conditions:
|
|
11
|
|
12 The above copyright notice and this permission notice shall be
|
|
13 included in all copies or substantial portions of the Software.
|
|
14
|
|
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
22 DEALINGS IN THE SOFTWARE.
|
|
23 **/
|
|
24
|
|
25 #include <stdlib.h>
|
|
26 #include <theora/theora.h>
|
|
27 #include "avcodec.h"
|
|
28
|
|
29 typedef struct TheoraContext {
|
|
30 theora_info info;
|
|
31 theora_state state;
|
|
32 theora_comment comment;
|
|
33 ogg_packet op;
|
|
34 } TheoraContext;
|
|
35
|
|
36 static int
|
|
37 Theora_decode_frame(AVCodecContext *ctx, void *outdata, int *outdata_size,
|
|
38 uint8_t *buf, int buf_size)
|
|
39 {
|
|
40 TheoraContext *thc = ctx->priv_data;
|
|
41 AVFrame *frame = outdata;
|
|
42 yuv_buffer yuv;
|
|
43
|
|
44 thc->op.packet = buf;
|
|
45 thc->op.bytes = buf_size;
|
|
46
|
|
47 if(theora_decode_packetin(&thc->state, &thc->op))
|
|
48 return -1;
|
|
49
|
|
50 theora_decode_YUVout(&thc->state, &yuv);
|
|
51
|
|
52 frame->data[0] = yuv.y;
|
|
53 frame->data[1] = yuv.u;
|
|
54 frame->data[2] = yuv.v;
|
|
55 frame->linesize[0] = yuv.y_stride;
|
|
56 frame->linesize[1] = yuv.uv_stride;
|
|
57 frame->linesize[2] = yuv.uv_stride;
|
|
58
|
|
59 *outdata_size = sizeof(*frame);
|
|
60 return buf_size;
|
|
61 }
|
|
62
|
|
63 static int
|
|
64 Theora_decode_end(AVCodecContext *ctx)
|
|
65 {
|
|
66 TheoraContext *thc = ctx->priv_data;
|
|
67 theora_info_clear(&thc->info);
|
|
68 theora_comment_clear(&thc->comment);
|
|
69 return 0;
|
|
70 }
|
|
71
|
|
72 static int
|
|
73 Theora_decode_init(AVCodecContext *ctx)
|
|
74 {
|
|
75 TheoraContext *thc = ctx->priv_data;
|
|
76 int size, hs, i;
|
|
77 ogg_packet op;
|
|
78 uint8_t *cdp;
|
|
79
|
|
80 if(ctx->extradata_size < 6)
|
|
81 return -1;
|
|
82
|
|
83 theora_info_init(&thc->info);
|
|
84
|
|
85 memset(&op, 0, sizeof(op));
|
|
86 cdp = ctx->extradata;
|
|
87 size = ctx->extradata_size;
|
|
88
|
|
89 for(i = 0; i < 3; i++){
|
|
90 hs = *cdp++ << 8;
|
|
91 hs += *cdp++;
|
|
92 size -= 2;
|
|
93
|
|
94 if(hs > size){
|
|
95 av_log(ctx, AV_LOG_ERROR, "extradata too small: %i > %i\n",
|
|
96 hs, size);
|
|
97 return -1;
|
|
98 }
|
|
99
|
|
100 op.packet = cdp;
|
|
101 op.bytes = hs;
|
|
102 op.b_o_s = !i;
|
|
103 if(theora_decode_header(&thc->info, &thc->comment, &op))
|
|
104 return -1;
|
|
105 op.packetno++;
|
|
106
|
|
107 cdp += hs;
|
|
108 size -= hs;
|
|
109 }
|
|
110
|
|
111 theora_decode_init(&thc->state, &thc->info);
|
|
112
|
|
113 ctx->width = thc->info.width;
|
|
114 ctx->height = thc->info.height;
|
|
115 ctx->time_base.num = thc->info.fps_denominator;
|
|
116 ctx->time_base.den = thc->info.fps_numerator;
|
|
117 ctx->pix_fmt = PIX_FMT_YUV420P; /* FIXME: others are possible */
|
|
118
|
|
119 return 0;
|
|
120 }
|
|
121
|
|
122 AVCodec oggtheora_decoder = {
|
|
123 "theora",
|
|
124 CODEC_TYPE_VIDEO,
|
|
125 CODEC_ID_THEORA,
|
|
126 sizeof(TheoraContext),
|
|
127 Theora_decode_init,
|
|
128 NULL,
|
|
129 Theora_decode_end,
|
|
130 Theora_decode_frame,
|
|
131 0,
|
|
132 NULL
|
|
133 };
|