Commit | Line | Data |
---|---|---|
8424cf50 FB |
1 | /* |
2 | * Audio and Video frame extraction | |
3 | * Copyright (c) 2003 Fabrice Bellard. | |
4 | * Copyright (c) 2003 Michael Niedermayer. | |
5 | * | |
b78e7197 DB |
6 | * This file is part of FFmpeg. |
7 | * | |
8 | * FFmpeg is free software; you can redistribute it and/or | |
8424cf50 FB |
9 | * modify it under the terms of the GNU Lesser General Public |
10 | * License as published by the Free Software Foundation; either | |
b78e7197 | 11 | * version 2.1 of the License, or (at your option) any later version. |
8424cf50 | 12 | * |
b78e7197 | 13 | * FFmpeg is distributed in the hope that it will be useful, |
8424cf50 FB |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
b78e7197 | 19 | * License along with FFmpeg; if not, write to the Free Software |
5509bffa | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
8424cf50 FB |
21 | */ |
22 | #include "avcodec.h" | |
23 | #include "mpegvideo.h" | |
24 | #include "mpegaudio.h" | |
25 | ||
26 | AVCodecParser *av_first_parser = NULL; | |
27 | ||
28 | void av_register_codec_parser(AVCodecParser *parser) | |
29 | { | |
30 | parser->next = av_first_parser; | |
31 | av_first_parser = parser; | |
32 | } | |
33 | ||
34 | AVCodecParserContext *av_parser_init(int codec_id) | |
35 | { | |
36 | AVCodecParserContext *s; | |
37 | AVCodecParser *parser; | |
38 | int ret; | |
115329f1 | 39 | |
8845e427 MN |
40 | if(codec_id == CODEC_ID_NONE) |
41 | return NULL; | |
8424cf50 FB |
42 | |
43 | for(parser = av_first_parser; parser != NULL; parser = parser->next) { | |
44 | if (parser->codec_ids[0] == codec_id || | |
45 | parser->codec_ids[1] == codec_id || | |
99f06236 MN |
46 | parser->codec_ids[2] == codec_id || |
47 | parser->codec_ids[3] == codec_id || | |
48 | parser->codec_ids[4] == codec_id) | |
8424cf50 FB |
49 | goto found; |
50 | } | |
51 | return NULL; | |
52 | found: | |
53 | s = av_mallocz(sizeof(AVCodecParserContext)); | |
54 | if (!s) | |
55 | return NULL; | |
56 | s->parser = parser; | |
57 | s->priv_data = av_mallocz(parser->priv_data_size); | |
58 | if (!s->priv_data) { | |
59 | av_free(s); | |
60 | return NULL; | |
61 | } | |
62 | if (parser->parser_init) { | |
63 | ret = parser->parser_init(s); | |
64 | if (ret != 0) { | |
65 | av_free(s->priv_data); | |
66 | av_free(s); | |
67 | return NULL; | |
68 | } | |
69 | } | |
a62aecce | 70 | s->fetch_timestamp=1; |
8424cf50 FB |
71 | return s; |
72 | } | |
73 | ||
6e45e928 FB |
74 | /* NOTE: buf_size == 0 is used to signal EOF so that the last frame |
75 | can be returned if necessary */ | |
115329f1 | 76 | int av_parser_parse(AVCodecParserContext *s, |
8424cf50 | 77 | AVCodecContext *avctx, |
115329f1 | 78 | uint8_t **poutbuf, int *poutbuf_size, |
b84f2a35 FB |
79 | const uint8_t *buf, int buf_size, |
80 | int64_t pts, int64_t dts) | |
8424cf50 | 81 | { |
b84f2a35 | 82 | int index, i, k; |
6e45e928 | 83 | uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
115329f1 | 84 | |
6e45e928 FB |
85 | if (buf_size == 0) { |
86 | /* padding is always necessary even if EOF, so we add it here */ | |
87 | memset(dummy_buf, 0, sizeof(dummy_buf)); | |
88 | buf = dummy_buf; | |
b84f2a35 FB |
89 | } else { |
90 | /* add a new packet descriptor */ | |
91 | k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
92 | s->cur_frame_start_index = k; | |
93 | s->cur_frame_offset[k] = s->cur_offset; | |
94 | s->cur_frame_pts[k] = pts; | |
95 | s->cur_frame_dts[k] = dts; | |
96 | ||
97 | /* fill first PTS/DTS */ | |
a62aecce MN |
98 | if (s->fetch_timestamp){ |
99 | s->fetch_timestamp=0; | |
b84f2a35 FB |
100 | s->last_pts = pts; |
101 | s->last_dts = dts; | |
c77a9a0e MN |
102 | s->cur_frame_pts[k] = |
103 | s->cur_frame_dts[k] = AV_NOPTS_VALUE; | |
b84f2a35 | 104 | } |
6e45e928 FB |
105 | } |
106 | ||
8424cf50 FB |
107 | /* WARNING: the returned index can be negative */ |
108 | index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
c77a9a0e | 109 | //av_log(NULL, AV_LOG_DEBUG, "parser: in:%lld, %lld, out:%lld, %lld, in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id); |
8424cf50 FB |
110 | /* update the file pointer */ |
111 | if (*poutbuf_size) { | |
b84f2a35 | 112 | /* fill the data for the current frame */ |
8424cf50 | 113 | s->frame_offset = s->last_frame_offset; |
b84f2a35 FB |
114 | s->pts = s->last_pts; |
115 | s->dts = s->last_dts; | |
115329f1 | 116 | |
b84f2a35 | 117 | /* offset of the next frame */ |
8424cf50 | 118 | s->last_frame_offset = s->cur_offset + index; |
b84f2a35 FB |
119 | /* find the packet in which the new frame starts. It |
120 | is tricky because of MPEG video start codes | |
121 | which can begin in one packet and finish in | |
122 | another packet. In the worst case, an MPEG | |
123 | video start code could be in 4 different | |
124 | packets. */ | |
125 | k = s->cur_frame_start_index; | |
126 | for(i = 0; i < AV_PARSER_PTS_NB; i++) { | |
127 | if (s->last_frame_offset >= s->cur_frame_offset[k]) | |
128 | break; | |
129 | k = (k - 1) & (AV_PARSER_PTS_NB - 1); | |
130 | } | |
a62aecce | 131 | |
b84f2a35 FB |
132 | s->last_pts = s->cur_frame_pts[k]; |
133 | s->last_dts = s->cur_frame_dts[k]; | |
115329f1 | 134 | |
a62aecce MN |
135 | /* some parsers tell us the packet size even before seeing the first byte of the next packet, |
136 | so the next pts/dts is in the next chunk */ | |
137 | if(index == buf_size){ | |
138 | s->fetch_timestamp=1; | |
139 | } | |
8424cf50 FB |
140 | } |
141 | if (index < 0) | |
142 | index = 0; | |
143 | s->cur_offset += index; | |
144 | return index; | |
145 | } | |
146 | ||
73480a15 MN |
147 | /** |
148 | * | |
149 | * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed | |
748c2fca | 150 | * @deprecated use AVBitstreamFilter |
73480a15 | 151 | */ |
90ad92b3 MN |
152 | int av_parser_change(AVCodecParserContext *s, |
153 | AVCodecContext *avctx, | |
115329f1 | 154 | uint8_t **poutbuf, int *poutbuf_size, |
90ad92b3 | 155 | const uint8_t *buf, int buf_size, int keyframe){ |
115329f1 | 156 | |
90ad92b3 | 157 | if(s && s->parser->split){ |
73480a15 | 158 | if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){ |
90ad92b3 MN |
159 | int i= s->parser->split(avctx, buf, buf_size); |
160 | buf += i; | |
161 | buf_size -= i; | |
162 | } | |
163 | } | |
164 | ||
79396ac6 MR |
165 | /* cast to avoid warning about discarding qualifiers */ |
166 | *poutbuf= (uint8_t *) buf; | |
90ad92b3 MN |
167 | *poutbuf_size= buf_size; |
168 | if(avctx->extradata){ | |
169 | if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) | |
170 | /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/ | |
171 | /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){ | |
172 | int size= buf_size + avctx->extradata_size; | |
173 | *poutbuf_size= size; | |
174 | *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); | |
115329f1 | 175 | |
90ad92b3 | 176 | memcpy(*poutbuf, avctx->extradata, avctx->extradata_size); |
73480a15 | 177 | memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); |
90ad92b3 MN |
178 | return 1; |
179 | } | |
180 | } | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
8424cf50 FB |
185 | void av_parser_close(AVCodecParserContext *s) |
186 | { | |
187 | if (s->parser->parser_close) | |
188 | s->parser->parser_close(s); | |
189 | av_free(s->priv_data); | |
190 | av_free(s); | |
191 | } | |
192 | ||
193 | /*****************************************************/ | |
194 | ||
195 | //#define END_NOT_FOUND (-100) | |
196 | ||
bb270c08 DB |
197 | #define PICTURE_START_CODE 0x00000100 |
198 | #define SEQ_START_CODE 0x000001b3 | |
199 | #define EXT_START_CODE 0x000001b5 | |
200 | #define SLICE_MIN_START_CODE 0x00000101 | |
201 | #define SLICE_MAX_START_CODE 0x000001af | |
8424cf50 FB |
202 | |
203 | typedef struct ParseContext1{ | |
e4cb187d MN |
204 | ParseContext pc; |
205 | /* XXX/FIXME PC1 vs. PC */ | |
8424cf50 FB |
206 | /* MPEG2 specific */ |
207 | int frame_rate; | |
208 | int progressive_sequence; | |
209 | int width, height; | |
c6f353ff | 210 | |
8424cf50 FB |
211 | /* XXX: suppress that, needed by MPEG4 */ |
212 | MpegEncContext *enc; | |
c6f353ff | 213 | int first_picture; |
8424cf50 FB |
214 | } ParseContext1; |
215 | ||
216 | /** | |
217 | * combines the (truncated) bitstream to a complete frame | |
218 | * @returns -1 if no complete frame could be created | |
219 | */ | |
e4cb187d | 220 | int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
8424cf50 FB |
221 | { |
222 | #if 0 | |
223 | if(pc->overread){ | |
224 | printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
225 | printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
226 | } | |
227 | #endif | |
228 | ||
229 | /* copy overreaded bytes from last frame into buffer */ | |
230 | for(; pc->overread>0; pc->overread--){ | |
231 | pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
232 | } | |
f48c0551 MN |
233 | |
234 | /* flush remaining if EOF */ | |
235 | if(!*buf_size && next == END_NOT_FOUND){ | |
236 | next= 0; | |
237 | } | |
238 | ||
8424cf50 FB |
239 | pc->last_index= pc->index; |
240 | ||
241 | /* copy into buffer end return */ | |
242 | if(next == END_NOT_FOUND){ | |
243 | pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
244 | ||
245 | memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
246 | pc->index += *buf_size; | |
247 | return -1; | |
248 | } | |
249 | ||
250 | *buf_size= | |
251 | pc->overread_index= pc->index + next; | |
115329f1 | 252 | |
8424cf50 FB |
253 | /* append to buffer */ |
254 | if(pc->index){ | |
255 | pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
256 | ||
257 | memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
258 | pc->index = 0; | |
259 | *buf= pc->buffer; | |
260 | } | |
261 | ||
262 | /* store overread bytes */ | |
263 | for(;next < 0; next++){ | |
264 | pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
265 | pc->overread++; | |
266 | } | |
267 | ||
268 | #if 0 | |
269 | if(pc->overread){ | |
270 | printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
271 | printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
272 | } | |
273 | #endif | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
8424cf50 FB |
278 | /* XXX: merge with libavcodec ? */ |
279 | #define MPEG1_FRAME_RATE_BASE 1001 | |
280 | ||
281 | static const int frame_rate_tab[16] = { | |
115329f1 | 282 | 0, |
8424cf50 FB |
283 | 24000, |
284 | 24024, | |
285 | 25025, | |
286 | 30000, | |
287 | 30030, | |
288 | 50050, | |
289 | 60000, | |
290 | 60060, | |
291 | // Xing's 15fps: (9) | |
292 | 15015, | |
293 | // libmpeg3's "Unofficial economy rates": (10-13) | |
294 | 5005, | |
295 | 10010, | |
296 | 12012, | |
297 | 15015, | |
298 | // random, just to avoid segfault !never encode these | |
299 | 25025, | |
300 | 25025, | |
301 | }; | |
302 | ||
af9e7d18 | 303 | #ifdef CONFIG_MPEGVIDEO_PARSER |
c0df9d75 | 304 | //FIXME move into mpeg12.c |
115329f1 | 305 | static void mpegvideo_extract_headers(AVCodecParserContext *s, |
8424cf50 FB |
306 | AVCodecContext *avctx, |
307 | const uint8_t *buf, int buf_size) | |
308 | { | |
309 | ParseContext1 *pc = s->priv_data; | |
310 | const uint8_t *buf_end; | |
191e8ca7 | 311 | uint32_t start_code; |
8424cf50 FB |
312 | int frame_rate_index, ext_type, bytes_left; |
313 | int frame_rate_ext_n, frame_rate_ext_d; | |
8dab64b6 | 314 | int picture_structure, top_field_first, repeat_first_field, progressive_frame; |
8c787559 | 315 | int horiz_size_ext, vert_size_ext, bit_rate_ext; |
90ad92b3 | 316 | //FIXME replace the crap with get_bits() |
8424cf50 FB |
317 | s->repeat_pict = 0; |
318 | buf_end = buf + buf_size; | |
319 | while (buf < buf_end) { | |
82fcbc14 MN |
320 | start_code= -1; |
321 | buf= ff_find_start_code(buf, buf_end, &start_code); | |
8424cf50 FB |
322 | bytes_left = buf_end - buf; |
323 | switch(start_code) { | |
324 | case PICTURE_START_CODE: | |
325 | if (bytes_left >= 2) { | |
326 | s->pict_type = (buf[1] >> 3) & 7; | |
327 | } | |
328 | break; | |
329 | case SEQ_START_CODE: | |
8c787559 | 330 | if (bytes_left >= 7) { |
0b2346d3 MN |
331 | pc->width = (buf[0] << 4) | (buf[1] >> 4); |
332 | pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
21adafec | 333 | avcodec_set_dimensions(avctx, pc->width, pc->height); |
8424cf50 | 334 | frame_rate_index = buf[3] & 0xf; |
c0df9d75 MN |
335 | pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index]; |
336 | avctx->time_base.num = MPEG1_FRAME_RATE_BASE; | |
8c787559 | 337 | avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400; |
3e9d718e FB |
338 | avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
339 | avctx->sub_id = 1; | |
8424cf50 FB |
340 | } |
341 | break; | |
342 | case EXT_START_CODE: | |
343 | if (bytes_left >= 1) { | |
344 | ext_type = (buf[0] >> 4); | |
345 | switch(ext_type) { | |
346 | case 0x1: /* sequence extension */ | |
347 | if (bytes_left >= 6) { | |
348 | horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
349 | vert_size_ext = (buf[2] >> 5) & 3; | |
8c787559 | 350 | bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); |
8424cf50 FB |
351 | frame_rate_ext_n = (buf[5] >> 5) & 3; |
352 | frame_rate_ext_d = (buf[5] & 0x1f); | |
353 | pc->progressive_sequence = buf[1] & (1 << 3); | |
8ca5d3bb | 354 | avctx->has_b_frames= !(buf[5] >> 7); |
8424cf50 | 355 | |
0b2346d3 MN |
356 | pc->width |=(horiz_size_ext << 12); |
357 | pc->height |=( vert_size_ext << 12); | |
8c787559 | 358 | avctx->bit_rate += (bit_rate_ext << 18) * 400; |
21adafec | 359 | avcodec_set_dimensions(avctx, pc->width, pc->height); |
c0df9d75 MN |
360 | avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1); |
361 | avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
3e9d718e | 362 | avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
8424cf50 FB |
363 | avctx->sub_id = 2; /* forces MPEG2 */ |
364 | } | |
365 | break; | |
366 | case 0x8: /* picture coding extension */ | |
367 | if (bytes_left >= 5) { | |
5bb994e2 | 368 | picture_structure = buf[2]&3; |
8424cf50 FB |
369 | top_field_first = buf[3] & (1 << 7); |
370 | repeat_first_field = buf[3] & (1 << 1); | |
371 | progressive_frame = buf[4] & (1 << 7); | |
115329f1 | 372 | |
8424cf50 FB |
373 | /* check if we must repeat the frame */ |
374 | if (repeat_first_field) { | |
375 | if (pc->progressive_sequence) { | |
376 | if (top_field_first) | |
377 | s->repeat_pict = 4; | |
378 | else | |
379 | s->repeat_pict = 2; | |
380 | } else if (progressive_frame) { | |
381 | s->repeat_pict = 1; | |
382 | } | |
383 | } | |
115329f1 DB |
384 | |
385 | /* the packet only represents half a frame | |
8dab64b6 MN |
386 | XXX,FIXME maybe find a different solution */ |
387 | if(picture_structure != 3) | |
388 | s->repeat_pict = -1; | |
8424cf50 FB |
389 | } |
390 | break; | |
391 | } | |
392 | } | |
393 | break; | |
394 | case -1: | |
395 | goto the_end; | |
396 | default: | |
397 | /* we stop parsing when we encounter a slice. It ensures | |
398 | that this function takes a negligible amount of time */ | |
115329f1 | 399 | if (start_code >= SLICE_MIN_START_CODE && |
8424cf50 FB |
400 | start_code <= SLICE_MAX_START_CODE) |
401 | goto the_end; | |
402 | break; | |
403 | } | |
404 | } | |
405 | the_end: ; | |
406 | } | |
407 | ||
408 | static int mpegvideo_parse(AVCodecParserContext *s, | |
409 | AVCodecContext *avctx, | |
115329f1 | 410 | uint8_t **poutbuf, int *poutbuf_size, |
8424cf50 FB |
411 | const uint8_t *buf, int buf_size) |
412 | { | |
e4cb187d MN |
413 | ParseContext1 *pc1 = s->priv_data; |
414 | ParseContext *pc= &pc1->pc; | |
8424cf50 | 415 | int next; |
115329f1 | 416 | |
7cbaa7ba MN |
417 | if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
418 | next= buf_size; | |
419 | }else{ | |
420 | next= ff_mpeg1_find_frame_end(pc, buf, buf_size); | |
115329f1 | 421 | |
7cbaa7ba MN |
422 | if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
423 | *poutbuf = NULL; | |
424 | *poutbuf_size = 0; | |
425 | return buf_size; | |
426 | } | |
115329f1 | 427 | |
8424cf50 FB |
428 | } |
429 | /* we have a full frame : we just parse the first few MPEG headers | |
430 | to have the full timing information. The time take by this | |
431 | function should be negligible for uncorrupted streams */ | |
432 | mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
433 | #if 0 | |
115329f1 | 434 | printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", |
c0df9d75 | 435 | s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict); |
8424cf50 FB |
436 | #endif |
437 | ||
438 | *poutbuf = (uint8_t *)buf; | |
439 | *poutbuf_size = buf_size; | |
440 | return next; | |
441 | } | |
442 | ||
90ad92b3 MN |
443 | static int mpegvideo_split(AVCodecContext *avctx, |
444 | const uint8_t *buf, int buf_size) | |
445 | { | |
446 | int i; | |
447 | uint32_t state= -1; | |
115329f1 | 448 | |
90ad92b3 MN |
449 | for(i=0; i<buf_size; i++){ |
450 | state= (state<<8) | buf[i]; | |
451 | if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) | |
73480a15 | 452 | return i-3; |
90ad92b3 MN |
453 | } |
454 | return 0; | |
455 | } | |
af9e7d18 | 456 | #endif /* CONFIG_MPEGVIDEO_PARSER */ |
90ad92b3 | 457 | |
e4cb187d | 458 | void ff_parse_close(AVCodecParserContext *s) |
8424cf50 | 459 | { |
e4cb187d | 460 | ParseContext *pc = s->priv_data; |
8424cf50 FB |
461 | |
462 | av_free(pc->buffer); | |
8424cf50 FB |
463 | } |
464 | ||
e4cb187d | 465 | static void parse1_close(AVCodecParserContext *s) |
8424cf50 | 466 | { |
e4cb187d | 467 | ParseContext1 *pc1 = s->priv_data; |
8424cf50 | 468 | |
e4cb187d MN |
469 | av_free(pc1->pc.buffer); |
470 | av_free(pc1->enc); | |
8424cf50 FB |
471 | } |
472 | ||
e4cb187d MN |
473 | /*************************/ |
474 | ||
af9e7d18 | 475 | #ifdef CONFIG_MPEG4VIDEO_PARSER |
8424cf50 FB |
476 | /* used by parser */ |
477 | /* XXX: make it use less memory */ | |
115329f1 | 478 | static int av_mpeg4_decode_header(AVCodecParserContext *s1, |
8424cf50 FB |
479 | AVCodecContext *avctx, |
480 | const uint8_t *buf, int buf_size) | |
481 | { | |
482 | ParseContext1 *pc = s1->priv_data; | |
483 | MpegEncContext *s = pc->enc; | |
484 | GetBitContext gb1, *gb = &gb1; | |
485 | int ret; | |
486 | ||
487 | s->avctx = avctx; | |
c6f353ff FB |
488 | s->current_picture_ptr = &s->current_picture; |
489 | ||
490 | if (avctx->extradata_size && pc->first_picture){ | |
491 | init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
492 | ret = ff_mpeg4_decode_picture_header(s, gb); | |
493 | } | |
494 | ||
8424cf50 FB |
495 | init_get_bits(gb, buf, 8 * buf_size); |
496 | ret = ff_mpeg4_decode_picture_header(s, gb); | |
497 | if (s->width) { | |
21adafec | 498 | avcodec_set_dimensions(avctx, s->width, s->height); |
8424cf50 | 499 | } |
7cbaa7ba | 500 | s1->pict_type= s->pict_type; |
c6f353ff | 501 | pc->first_picture = 0; |
8424cf50 FB |
502 | return ret; |
503 | } | |
504 | ||
e96682e6 | 505 | static int mpeg4video_parse_init(AVCodecParserContext *s) |
8424cf50 FB |
506 | { |
507 | ParseContext1 *pc = s->priv_data; | |
c6f353ff | 508 | |
8424cf50 FB |
509 | pc->enc = av_mallocz(sizeof(MpegEncContext)); |
510 | if (!pc->enc) | |
511 | return -1; | |
c6f353ff | 512 | pc->first_picture = 1; |
8424cf50 FB |
513 | return 0; |
514 | } | |
515 | ||
516 | static int mpeg4video_parse(AVCodecParserContext *s, | |
517 | AVCodecContext *avctx, | |
115329f1 | 518 | uint8_t **poutbuf, int *poutbuf_size, |
8424cf50 FB |
519 | const uint8_t *buf, int buf_size) |
520 | { | |
e4cb187d | 521 | ParseContext *pc = s->priv_data; |
8424cf50 | 522 | int next; |
115329f1 | 523 | |
7cbaa7ba MN |
524 | if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
525 | next= buf_size; | |
526 | }else{ | |
527 | next= ff_mpeg4_find_frame_end(pc, buf, buf_size); | |
115329f1 | 528 | |
7cbaa7ba MN |
529 | if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
530 | *poutbuf = NULL; | |
531 | *poutbuf_size = 0; | |
532 | return buf_size; | |
533 | } | |
8424cf50 FB |
534 | } |
535 | av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
536 | ||
537 | *poutbuf = (uint8_t *)buf; | |
538 | *poutbuf_size = buf_size; | |
539 | return next; | |
540 | } | |
af9e7d18 | 541 | #endif |
8424cf50 | 542 | |
af9e7d18 | 543 | #ifdef CONFIG_CAVSVIDEO_PARSER |
b482e2d1 MN |
544 | static int cavsvideo_parse(AVCodecParserContext *s, |
545 | AVCodecContext *avctx, | |
546 | uint8_t **poutbuf, int *poutbuf_size, | |
547 | const uint8_t *buf, int buf_size) | |
548 | { | |
549 | ParseContext *pc = s->priv_data; | |
550 | int next; | |
551 | ||
552 | if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ | |
553 | next= buf_size; | |
554 | }else{ | |
555 | next= ff_cavs_find_frame_end(pc, buf, buf_size); | |
556 | ||
557 | if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { | |
558 | *poutbuf = NULL; | |
559 | *poutbuf_size = 0; | |
560 | return buf_size; | |
561 | } | |
562 | } | |
563 | *poutbuf = (uint8_t *)buf; | |
564 | *poutbuf_size = buf_size; | |
565 | return next; | |
566 | } | |
af9e7d18 | 567 | #endif /* CONFIG_CAVSVIDEO_PARSER */ |
b482e2d1 | 568 | |
90ad92b3 MN |
569 | static int mpeg4video_split(AVCodecContext *avctx, |
570 | const uint8_t *buf, int buf_size) | |
571 | { | |
572 | int i; | |
573 | uint32_t state= -1; | |
115329f1 | 574 | |
90ad92b3 MN |
575 | for(i=0; i<buf_size; i++){ |
576 | state= (state<<8) | buf[i]; | |
577 | if(state == 0x1B3 || state == 0x1B6) | |
73480a15 | 578 | return i-3; |
90ad92b3 MN |
579 | } |
580 | return 0; | |
581 | } | |
582 | ||
8424cf50 FB |
583 | /*************************/ |
584 | ||
af9e7d18 | 585 | #ifdef CONFIG_MPEGAUDIO_PARSER |
8424cf50 | 586 | typedef struct MpegAudioParseContext { |
bb270c08 | 587 | uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ |
8424cf50 FB |
588 | uint8_t *inbuf_ptr; |
589 | int frame_size; | |
590 | int free_format_frame_size; | |
591 | int free_format_next_header; | |
4b9ac0b5 MN |
592 | uint32_t header; |
593 | int header_count; | |
8424cf50 FB |
594 | } MpegAudioParseContext; |
595 | ||
596 | #define MPA_HEADER_SIZE 4 | |
597 | ||
598 | /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
88730be6 | 599 | #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
8424cf50 | 600 | #define SAME_HEADER_MASK \ |
e993bc03 | 601 | (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
8424cf50 FB |
602 | |
603 | static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
604 | { | |
605 | MpegAudioParseContext *s = s1->priv_data; | |
606 | s->inbuf_ptr = s->inbuf; | |
607 | return 0; | |
608 | } | |
609 | ||
610 | static int mpegaudio_parse(AVCodecParserContext *s1, | |
611 | AVCodecContext *avctx, | |
115329f1 | 612 | uint8_t **poutbuf, int *poutbuf_size, |
8424cf50 FB |
613 | const uint8_t *buf, int buf_size) |
614 | { | |
615 | MpegAudioParseContext *s = s1->priv_data; | |
4b9ac0b5 | 616 | int len, ret, sr; |
8424cf50 FB |
617 | uint32_t header; |
618 | const uint8_t *buf_ptr; | |
619 | ||
620 | *poutbuf = NULL; | |
621 | *poutbuf_size = 0; | |
622 | buf_ptr = buf; | |
623 | while (buf_size > 0) { | |
bb270c08 DB |
624 | len = s->inbuf_ptr - s->inbuf; |
625 | if (s->frame_size == 0) { | |
8424cf50 FB |
626 | /* special case for next header for first frame in free |
627 | format case (XXX: find a simpler method) */ | |
628 | if (s->free_format_next_header != 0) { | |
629 | s->inbuf[0] = s->free_format_next_header >> 24; | |
630 | s->inbuf[1] = s->free_format_next_header >> 16; | |
631 | s->inbuf[2] = s->free_format_next_header >> 8; | |
632 | s->inbuf[3] = s->free_format_next_header; | |
633 | s->inbuf_ptr = s->inbuf + 4; | |
634 | s->free_format_next_header = 0; | |
635 | goto got_header; | |
636 | } | |
bb270c08 | 637 | /* no header seen : find one. We need at least MPA_HEADER_SIZE |
8424cf50 | 638 | bytes to parse it */ |
4e2d6a45 | 639 | len = FFMIN(MPA_HEADER_SIZE - len, buf_size); |
bb270c08 DB |
640 | if (len > 0) { |
641 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
642 | buf_ptr += len; | |
643 | buf_size -= len; | |
644 | s->inbuf_ptr += len; | |
645 | } | |
646 | if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
8424cf50 | 647 | got_header: |
4b9ac0b5 | 648 | sr= avctx->sample_rate; |
bb270c08 DB |
649 | header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
650 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
8424cf50 FB |
651 | |
652 | ret = mpa_decode_header(avctx, header); | |
653 | if (ret < 0) { | |
4b9ac0b5 | 654 | s->header_count= -2; |
bb270c08 DB |
655 | /* no sync found : move by one byte (inefficient, but simple!) */ |
656 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
657 | s->inbuf_ptr--; | |
8424cf50 FB |
658 | dprintf("skip %x\n", header); |
659 | /* reset free format frame size to give a chance | |
660 | to get a new bitrate */ | |
661 | s->free_format_frame_size = 0; | |
bb270c08 | 662 | } else { |
4b9ac0b5 MN |
663 | if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) |
664 | s->header_count= -3; | |
665 | s->header= header; | |
666 | s->header_count++; | |
8424cf50 | 667 | s->frame_size = ret; |
115329f1 | 668 | |
8424cf50 FB |
669 | #if 0 |
670 | /* free format: prepare to compute frame size */ | |
bb270c08 DB |
671 | if (decode_header(s, header) == 1) { |
672 | s->frame_size = -1; | |
8424cf50 FB |
673 | } |
674 | #endif | |
bb270c08 | 675 | } |
4b9ac0b5 MN |
676 | if(s->header_count <= 0) |
677 | avctx->sample_rate= sr; //FIXME ugly | |
bb270c08 | 678 | } |
115329f1 | 679 | } else |
8424cf50 FB |
680 | #if 0 |
681 | if (s->frame_size == -1) { | |
682 | /* free format : find next sync to compute frame size */ | |
bb270c08 DB |
683 | len = MPA_MAX_CODED_FRAME_SIZE - len; |
684 | if (len > buf_size) | |
685 | len = buf_size; | |
8424cf50 | 686 | if (len == 0) { |
bb270c08 | 687 | /* frame too long: resync */ |
8424cf50 | 688 | s->frame_size = 0; |
bb270c08 DB |
689 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
690 | s->inbuf_ptr--; | |
8424cf50 FB |
691 | } else { |
692 | uint8_t *p, *pend; | |
693 | uint32_t header1; | |
694 | int padding; | |
695 | ||
696 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
697 | /* check for header */ | |
698 | p = s->inbuf_ptr - 3; | |
699 | pend = s->inbuf_ptr + len - 4; | |
700 | while (p <= pend) { | |
701 | header = (p[0] << 24) | (p[1] << 16) | | |
702 | (p[2] << 8) | p[3]; | |
703 | header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
704 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
705 | /* check with high probability that we have a | |
706 | valid header */ | |
707 | if ((header & SAME_HEADER_MASK) == | |
708 | (header1 & SAME_HEADER_MASK)) { | |
709 | /* header found: update pointers */ | |
710 | len = (p + 4) - s->inbuf_ptr; | |
711 | buf_ptr += len; | |
712 | buf_size -= len; | |
713 | s->inbuf_ptr = p; | |
714 | /* compute frame size */ | |
715 | s->free_format_next_header = header; | |
716 | s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
717 | padding = (header1 >> 9) & 1; | |
718 | if (s->layer == 1) | |
719 | s->free_format_frame_size -= padding * 4; | |
720 | else | |
721 | s->free_format_frame_size -= padding; | |
115329f1 | 722 | dprintf("free frame size=%d padding=%d\n", |
8424cf50 FB |
723 | s->free_format_frame_size, padding); |
724 | decode_header(s, header1); | |
725 | goto next_data; | |
726 | } | |
727 | p++; | |
728 | } | |
729 | /* not found: simply increase pointers */ | |
730 | buf_ptr += len; | |
731 | s->inbuf_ptr += len; | |
732 | buf_size -= len; | |
733 | } | |
bb270c08 | 734 | } else |
8424cf50 FB |
735 | #endif |
736 | if (len < s->frame_size) { | |
737 | if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
738 | s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
4e2d6a45 | 739 | len = FFMIN(s->frame_size - len, buf_size); |
bb270c08 DB |
740 | memcpy(s->inbuf_ptr, buf_ptr, len); |
741 | buf_ptr += len; | |
742 | s->inbuf_ptr += len; | |
743 | buf_size -= len; | |
744 | } | |
4e2d6a45 MN |
745 | |
746 | if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf | |
747 | && buf_size + buf_ptr - buf >= s->frame_size){ | |
748 | if(s->header_count > 0){ | |
749 | *poutbuf = buf; | |
750 | *poutbuf_size = s->frame_size; | |
751 | } | |
752 | buf_ptr = buf + s->frame_size; | |
753 | s->inbuf_ptr = s->inbuf; | |
754 | s->frame_size = 0; | |
755 | break; | |
756 | } | |
757 | ||
8424cf50 | 758 | // next_data: |
115329f1 | 759 | if (s->frame_size > 0 && |
8424cf50 | 760 | (s->inbuf_ptr - s->inbuf) >= s->frame_size) { |
4b9ac0b5 MN |
761 | if(s->header_count > 0){ |
762 | *poutbuf = s->inbuf; | |
763 | *poutbuf_size = s->inbuf_ptr - s->inbuf; | |
764 | } | |
bb270c08 DB |
765 | s->inbuf_ptr = s->inbuf; |
766 | s->frame_size = 0; | |
767 | break; | |
768 | } | |
8424cf50 FB |
769 | } |
770 | return buf_ptr - buf; | |
771 | } | |
af9e7d18 | 772 | #endif /* CONFIG_MPEGAUDIO_PARSER */ |
8424cf50 | 773 | |
af9e7d18 | 774 | #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER) |
da46276f | 775 | /* also used for ADTS AAC */ |
8424cf50 | 776 | typedef struct AC3ParseContext { |
8424cf50 FB |
777 | uint8_t *inbuf_ptr; |
778 | int frame_size; | |
da46276f MR |
779 | int header_size; |
780 | int (*sync)(const uint8_t *buf, int *channels, int *sample_rate, | |
781 | int *bit_rate, int *samples); | |
4f8ff17e | 782 | uint8_t inbuf[8192]; /* input buffer */ |
8424cf50 FB |
783 | } AC3ParseContext; |
784 | ||
785 | #define AC3_HEADER_SIZE 7 | |
100a7422 | 786 | #define AAC_HEADER_SIZE 7 |
62327e28 | 787 | |
af9e7d18 | 788 | #ifdef CONFIG_AC3_PARSER |
62327e28 MR |
789 | static const int ac3_sample_rates[4] = { |
790 | 48000, 44100, 32000, 0 | |
791 | }; | |
792 | ||
793 | static const int ac3_frame_sizes[64][3] = { | |
4f59b684 DB |
794 | { 64, 69, 96 }, |
795 | { 64, 70, 96 }, | |
796 | { 80, 87, 120 }, | |
797 | { 80, 88, 120 }, | |
798 | { 96, 104, 144 }, | |
799 | { 96, 105, 144 }, | |
800 | { 112, 121, 168 }, | |
801 | { 112, 122, 168 }, | |
802 | { 128, 139, 192 }, | |
803 | { 128, 140, 192 }, | |
804 | { 160, 174, 240 }, | |
805 | { 160, 175, 240 }, | |
806 | { 192, 208, 288 }, | |
807 | { 192, 209, 288 }, | |
808 | { 224, 243, 336 }, | |
809 | { 224, 244, 336 }, | |
810 | { 256, 278, 384 }, | |
811 | { 256, 279, 384 }, | |
812 | { 320, 348, 480 }, | |
813 | { 320, 349, 480 }, | |
814 | { 384, 417, 576 }, | |
815 | { 384, 418, 576 }, | |
816 | { 448, 487, 672 }, | |
817 | { 448, 488, 672 }, | |
818 | { 512, 557, 768 }, | |
819 | { 512, 558, 768 }, | |
820 | { 640, 696, 960 }, | |
821 | { 640, 697, 960 }, | |
822 | { 768, 835, 1152 }, | |
823 | { 768, 836, 1152 }, | |
824 | { 896, 975, 1344 }, | |
825 | { 896, 976, 1344 }, | |
62327e28 MR |
826 | { 1024, 1114, 1536 }, |
827 | { 1024, 1115, 1536 }, | |
828 | { 1152, 1253, 1728 }, | |
829 | { 1152, 1254, 1728 }, | |
830 | { 1280, 1393, 1920 }, | |
831 | { 1280, 1394, 1920 }, | |
832 | }; | |
833 | ||
834 | static const int ac3_bitrates[64] = { | |
835 | 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112, | |
836 | 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, | |
837 | 384, 448, 448, 512, 512, 576, 576, 640, 640, | |
838 | }; | |
839 | ||
840 | static const int ac3_channels[8] = { | |
841 | 2, 1, 2, 3, 3, 4, 4, 5 | |
842 | }; | |
af9e7d18 | 843 | #endif /* CONFIG_AC3_PARSER */ |
62327e28 | 844 | |
af9e7d18 | 845 | #ifdef CONFIG_AAC_PARSER |
608bd843 | 846 | static const int aac_sample_rates[16] = { |
da46276f MR |
847 | 96000, 88200, 64000, 48000, 44100, 32000, |
848 | 24000, 22050, 16000, 12000, 11025, 8000, 7350 | |
849 | }; | |
850 | ||
608bd843 | 851 | static const int aac_channels[8] = { |
da46276f MR |
852 | 0, 1, 2, 3, 4, 5, 6, 8 |
853 | }; | |
af9e7d18 | 854 | #endif |
da46276f | 855 | |
af9e7d18 | 856 | #ifdef CONFIG_AC3_PARSER |
62327e28 | 857 | static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate, |
da46276f | 858 | int *bit_rate, int *samples) |
62327e28 MR |
859 | { |
860 | unsigned int fscod, frmsizecod, acmod, bsid, lfeon; | |
861 | GetBitContext bits; | |
862 | ||
863 | init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8); | |
864 | ||
865 | if(get_bits(&bits, 16) != 0x0b77) | |
4f59b684 | 866 | return 0; |
62327e28 | 867 | |
100a7422 | 868 | skip_bits(&bits, 16); /* crc */ |
62327e28 MR |
869 | fscod = get_bits(&bits, 2); |
870 | frmsizecod = get_bits(&bits, 6); | |
871 | ||
872 | if(!ac3_sample_rates[fscod]) | |
4f59b684 | 873 | return 0; |
62327e28 MR |
874 | |
875 | bsid = get_bits(&bits, 5); | |
876 | if(bsid > 8) | |
4f59b684 | 877 | return 0; |
100a7422 | 878 | skip_bits(&bits, 3); /* bsmod */ |
62327e28 MR |
879 | acmod = get_bits(&bits, 3); |
880 | if(acmod & 1 && acmod != 1) | |
100a7422 | 881 | skip_bits(&bits, 2); /* cmixlev */ |
62327e28 | 882 | if(acmod & 4) |
100a7422 | 883 | skip_bits(&bits, 2); /* surmixlev */ |
62327e28 | 884 | if(acmod & 2) |
100a7422 MR |
885 | skip_bits(&bits, 2); /* dsurmod */ |
886 | lfeon = get_bits1(&bits); | |
62327e28 MR |
887 | |
888 | *sample_rate = ac3_sample_rates[fscod]; | |
889 | *bit_rate = ac3_bitrates[frmsizecod] * 1000; | |
890 | *channels = ac3_channels[acmod] + lfeon; | |
da46276f | 891 | *samples = 6 * 256; |
62327e28 MR |
892 | |
893 | return ac3_frame_sizes[frmsizecod][fscod] * 2; | |
894 | } | |
af9e7d18 | 895 | #endif /* CONFIG_AC3_PARSER */ |
8424cf50 | 896 | |
af9e7d18 | 897 | #ifdef CONFIG_AAC_PARSER |
da46276f MR |
898 | static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate, |
899 | int *bit_rate, int *samples) | |
900 | { | |
901 | GetBitContext bits; | |
902 | int size, rdb, ch, sr; | |
903 | ||
904 | init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8); | |
905 | ||
906 | if(get_bits(&bits, 12) != 0xfff) | |
907 | return 0; | |
908 | ||
100a7422 MR |
909 | skip_bits1(&bits); /* id */ |
910 | skip_bits(&bits, 2); /* layer */ | |
911 | skip_bits1(&bits); /* protection_absent */ | |
912 | skip_bits(&bits, 2); /* profile_objecttype */ | |
913 | sr = get_bits(&bits, 4); /* sample_frequency_index */ | |
da46276f MR |
914 | if(!aac_sample_rates[sr]) |
915 | return 0; | |
100a7422 MR |
916 | skip_bits1(&bits); /* private_bit */ |
917 | ch = get_bits(&bits, 3); /* channel_configuration */ | |
da46276f MR |
918 | if(!aac_channels[ch]) |
919 | return 0; | |
100a7422 MR |
920 | skip_bits1(&bits); /* original/copy */ |
921 | skip_bits1(&bits); /* home */ | |
da46276f MR |
922 | |
923 | /* adts_variable_header */ | |
100a7422 MR |
924 | skip_bits1(&bits); /* copyright_identification_bit */ |
925 | skip_bits1(&bits); /* copyright_identification_start */ | |
926 | size = get_bits(&bits, 13); /* aac_frame_length */ | |
927 | skip_bits(&bits, 11); /* adts_buffer_fullness */ | |
928 | rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */ | |
da46276f MR |
929 | |
930 | *channels = aac_channels[ch]; | |
931 | *sample_rate = aac_sample_rates[sr]; | |
932 | *samples = (rdb + 1) * 1024; | |
933 | *bit_rate = size * 8 * *sample_rate / *samples; | |
934 | ||
935 | return size; | |
936 | } | |
af9e7d18 | 937 | #endif /* CONFIG_AAC_PARSER */ |
da46276f | 938 | |
af9e7d18 | 939 | #ifdef CONFIG_AC3_PARSER |
8424cf50 FB |
940 | static int ac3_parse_init(AVCodecParserContext *s1) |
941 | { | |
942 | AC3ParseContext *s = s1->priv_data; | |
943 | s->inbuf_ptr = s->inbuf; | |
da46276f MR |
944 | s->header_size = AC3_HEADER_SIZE; |
945 | s->sync = ac3_sync; | |
8424cf50 FB |
946 | return 0; |
947 | } | |
af9e7d18 | 948 | #endif |
8424cf50 | 949 | |
af9e7d18 | 950 | #ifdef CONFIG_AAC_PARSER |
da46276f MR |
951 | static int aac_parse_init(AVCodecParserContext *s1) |
952 | { | |
953 | AC3ParseContext *s = s1->priv_data; | |
954 | s->inbuf_ptr = s->inbuf; | |
955 | s->header_size = AAC_HEADER_SIZE; | |
956 | s->sync = aac_sync; | |
957 | return 0; | |
958 | } | |
af9e7d18 | 959 | #endif |
da46276f MR |
960 | |
961 | /* also used for ADTS AAC */ | |
8424cf50 FB |
962 | static int ac3_parse(AVCodecParserContext *s1, |
963 | AVCodecContext *avctx, | |
115329f1 | 964 | uint8_t **poutbuf, int *poutbuf_size, |
8424cf50 FB |
965 | const uint8_t *buf, int buf_size) |
966 | { | |
967 | AC3ParseContext *s = s1->priv_data; | |
968 | const uint8_t *buf_ptr; | |
da46276f | 969 | int len, sample_rate, bit_rate, channels, samples; |
8424cf50 FB |
970 | |
971 | *poutbuf = NULL; | |
972 | *poutbuf_size = 0; | |
973 | ||
974 | buf_ptr = buf; | |
975 | while (buf_size > 0) { | |
976 | len = s->inbuf_ptr - s->inbuf; | |
977 | if (s->frame_size == 0) { | |
da46276f MR |
978 | /* no header seen : find one. We need at least s->header_size |
979 | bytes to parse it */ | |
980 | len = FFMIN(s->header_size - len, buf_size); | |
d8a91afd | 981 | |
8424cf50 FB |
982 | memcpy(s->inbuf_ptr, buf_ptr, len); |
983 | buf_ptr += len; | |
984 | s->inbuf_ptr += len; | |
985 | buf_size -= len; | |
da46276f MR |
986 | if ((s->inbuf_ptr - s->inbuf) == s->header_size) { |
987 | len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate, | |
988 | &samples); | |
8424cf50 FB |
989 | if (len == 0) { |
990 | /* no sync found : move by one byte (inefficient, but simple!) */ | |
da46276f | 991 | memmove(s->inbuf, s->inbuf + 1, s->header_size - 1); |
8424cf50 FB |
992 | s->inbuf_ptr--; |
993 | } else { | |
bb270c08 | 994 | s->frame_size = len; |
8424cf50 FB |
995 | /* update codec info */ |
996 | avctx->sample_rate = sample_rate; | |
20da3179 | 997 | /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
da46276f MR |
998 | if(avctx->codec_id == CODEC_ID_AC3){ |
999 | if(avctx->channels!=1 && avctx->channels!=2){ | |
1000 | avctx->channels = channels; | |
1001 | } | |
1002 | } else { | |
4f59b684 | 1003 | avctx->channels = channels; |
20da3179 | 1004 | } |
bb270c08 | 1005 | avctx->bit_rate = bit_rate; |
da46276f | 1006 | avctx->frame_size = samples; |
8424cf50 FB |
1007 | } |
1008 | } | |
d8a91afd MN |
1009 | } else { |
1010 | len = FFMIN(s->frame_size - len, buf_size); | |
8424cf50 FB |
1011 | |
1012 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
1013 | buf_ptr += len; | |
1014 | s->inbuf_ptr += len; | |
1015 | buf_size -= len; | |
d8a91afd MN |
1016 | |
1017 | if(s->inbuf_ptr - s->inbuf == s->frame_size){ | |
1018 | *poutbuf = s->inbuf; | |
1019 | *poutbuf_size = s->frame_size; | |
1020 | s->inbuf_ptr = s->inbuf; | |
1021 | s->frame_size = 0; | |
1022 | break; | |
1023 | } | |
8424cf50 FB |
1024 | } |
1025 | } | |
1026 | return buf_ptr - buf; | |
1027 | } | |
af9e7d18 | 1028 | #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */ |
8424cf50 | 1029 | |
af9e7d18 | 1030 | #ifdef CONFIG_MPEGVIDEO_PARSER |
8424cf50 FB |
1031 | AVCodecParser mpegvideo_parser = { |
1032 | { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
1033 | sizeof(ParseContext1), | |
1034 | NULL, | |
1035 | mpegvideo_parse, | |
e4cb187d | 1036 | parse1_close, |
90ad92b3 | 1037 | mpegvideo_split, |
8424cf50 | 1038 | }; |
af9e7d18 MR |
1039 | #endif |
1040 | #ifdef CONFIG_MPEG4VIDEO_PARSER | |
8424cf50 FB |
1041 | AVCodecParser mpeg4video_parser = { |
1042 | { CODEC_ID_MPEG4 }, | |
1043 | sizeof(ParseContext1), | |
1044 | mpeg4video_parse_init, | |
1045 | mpeg4video_parse, | |
e4cb187d | 1046 | parse1_close, |
90ad92b3 | 1047 | mpeg4video_split, |
8424cf50 | 1048 | }; |
af9e7d18 MR |
1049 | #endif |
1050 | #ifdef CONFIG_CAVSVIDEO_PARSER | |
b482e2d1 MN |
1051 | AVCodecParser cavsvideo_parser = { |
1052 | { CODEC_ID_CAVS }, | |
1053 | sizeof(ParseContext1), | |
1054 | NULL, | |
1055 | cavsvideo_parse, | |
1056 | parse1_close, | |
1057 | mpeg4video_split, | |
1058 | }; | |
29c5cdca | 1059 | #endif |
af9e7d18 | 1060 | #ifdef CONFIG_MPEGAUDIO_PARSER |
8424cf50 FB |
1061 | AVCodecParser mpegaudio_parser = { |
1062 | { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
1063 | sizeof(MpegAudioParseContext), | |
1064 | mpegaudio_parse_init, | |
1065 | mpegaudio_parse, | |
1066 | NULL, | |
1067 | }; | |
af9e7d18 MR |
1068 | #endif |
1069 | #ifdef CONFIG_AC3_PARSER | |
8424cf50 FB |
1070 | AVCodecParser ac3_parser = { |
1071 | { CODEC_ID_AC3 }, | |
1072 | sizeof(AC3ParseContext), | |
1073 | ac3_parse_init, | |
1074 | ac3_parse, | |
1075 | NULL, | |
1076 | }; | |
af9e7d18 MR |
1077 | #endif |
1078 | #ifdef CONFIG_AAC_PARSER | |
da46276f MR |
1079 | AVCodecParser aac_parser = { |
1080 | { CODEC_ID_AAC }, | |
1081 | sizeof(AC3ParseContext), | |
1082 | aac_parse_init, | |
1083 | ac3_parse, | |
1084 | NULL, | |
1085 | }; | |
af9e7d18 | 1086 | #endif |