annotate oggtheora.c @ 2929:a97159871cdb libavcodec

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