Mercurial > libavcodec.hg
annotate parser.c @ 2029:934117474c3b libavcodec
more data_size=0 cleanup
author | michael |
---|---|
date | Fri, 21 May 2004 14:40:35 +0000 |
parents | f65d87bfdd5a |
children | f796043935f3 |
rev | line source |
---|---|
1613 | 1 /* |
2 * Audio and Video frame extraction | |
3 * Copyright (c) 2003 Fabrice Bellard. | |
4 * Copyright (c) 2003 Michael Niedermayer. | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 */ | |
20 #include "avcodec.h" | |
21 #include "mpegvideo.h" | |
22 #include "mpegaudio.h" | |
23 | |
24 AVCodecParser *av_first_parser = NULL; | |
25 | |
26 void av_register_codec_parser(AVCodecParser *parser) | |
27 { | |
28 parser->next = av_first_parser; | |
29 av_first_parser = parser; | |
30 } | |
31 | |
32 AVCodecParserContext *av_parser_init(int codec_id) | |
33 { | |
34 AVCodecParserContext *s; | |
35 AVCodecParser *parser; | |
36 int ret; | |
37 | |
38 for(parser = av_first_parser; parser != NULL; parser = parser->next) { | |
39 if (parser->codec_ids[0] == codec_id || | |
40 parser->codec_ids[1] == codec_id || | |
41 parser->codec_ids[2] == codec_id) | |
42 goto found; | |
43 } | |
44 return NULL; | |
45 found: | |
46 s = av_mallocz(sizeof(AVCodecParserContext)); | |
47 if (!s) | |
48 return NULL; | |
49 s->parser = parser; | |
50 s->priv_data = av_mallocz(parser->priv_data_size); | |
51 if (!s->priv_data) { | |
52 av_free(s); | |
53 return NULL; | |
54 } | |
55 if (parser->parser_init) { | |
56 ret = parser->parser_init(s); | |
57 if (ret != 0) { | |
58 av_free(s->priv_data); | |
59 av_free(s); | |
60 return NULL; | |
61 } | |
62 } | |
63 return s; | |
64 } | |
65 | |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
66 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
67 can be returned if necessary */ |
1613 | 68 int av_parser_parse(AVCodecParserContext *s, |
69 AVCodecContext *avctx, | |
70 uint8_t **poutbuf, int *poutbuf_size, | |
1696 | 71 const uint8_t *buf, int buf_size, |
72 int64_t pts, int64_t dts) | |
1613 | 73 { |
1696 | 74 int index, i, k; |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
75 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
76 |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
77 if (buf_size == 0) { |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
78 /* padding is always necessary even if EOF, so we add it here */ |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
79 memset(dummy_buf, 0, sizeof(dummy_buf)); |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
80 buf = dummy_buf; |
1696 | 81 } else { |
82 /* add a new packet descriptor */ | |
83 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
84 s->cur_frame_start_index = k; | |
85 s->cur_frame_offset[k] = s->cur_offset; | |
86 s->cur_frame_pts[k] = pts; | |
87 s->cur_frame_dts[k] = dts; | |
88 | |
89 /* fill first PTS/DTS */ | |
90 if (s->cur_offset == 0) { | |
91 s->last_pts = pts; | |
92 s->last_dts = dts; | |
93 } | |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
94 } |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
95 |
1613 | 96 /* WARNING: the returned index can be negative */ |
97 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
98 /* update the file pointer */ | |
99 if (*poutbuf_size) { | |
1696 | 100 /* fill the data for the current frame */ |
1613 | 101 s->frame_offset = s->last_frame_offset; |
1696 | 102 s->pts = s->last_pts; |
103 s->dts = s->last_dts; | |
104 | |
105 /* offset of the next frame */ | |
1613 | 106 s->last_frame_offset = s->cur_offset + index; |
1696 | 107 /* find the packet in which the new frame starts. It |
108 is tricky because of MPEG video start codes | |
109 which can begin in one packet and finish in | |
110 another packet. In the worst case, an MPEG | |
111 video start code could be in 4 different | |
112 packets. */ | |
113 k = s->cur_frame_start_index; | |
114 for(i = 0; i < AV_PARSER_PTS_NB; i++) { | |
115 if (s->last_frame_offset >= s->cur_frame_offset[k]) | |
116 break; | |
117 k = (k - 1) & (AV_PARSER_PTS_NB - 1); | |
118 } | |
119 s->last_pts = s->cur_frame_pts[k]; | |
120 s->last_dts = s->cur_frame_dts[k]; | |
1613 | 121 } |
122 if (index < 0) | |
123 index = 0; | |
124 s->cur_offset += index; | |
125 return index; | |
126 } | |
127 | |
128 void av_parser_close(AVCodecParserContext *s) | |
129 { | |
130 if (s->parser->parser_close) | |
131 s->parser->parser_close(s); | |
132 av_free(s->priv_data); | |
133 av_free(s); | |
134 } | |
135 | |
136 /*****************************************************/ | |
137 | |
138 //#define END_NOT_FOUND (-100) | |
139 | |
140 #define PICTURE_START_CODE 0x00000100 | |
141 #define SEQ_START_CODE 0x000001b3 | |
142 #define EXT_START_CODE 0x000001b5 | |
143 #define SLICE_MIN_START_CODE 0x00000101 | |
144 #define SLICE_MAX_START_CODE 0x000001af | |
145 | |
146 typedef struct ParseContext1{ | |
1988 | 147 ParseContext pc; |
148 /* XXX/FIXME PC1 vs. PC */ | |
1613 | 149 /* MPEG2 specific */ |
150 int frame_rate; | |
151 int progressive_sequence; | |
152 int width, height; | |
1614 | 153 |
1613 | 154 /* XXX: suppress that, needed by MPEG4 */ |
155 MpegEncContext *enc; | |
1614 | 156 int first_picture; |
1613 | 157 } ParseContext1; |
158 | |
159 /** | |
160 * combines the (truncated) bitstream to a complete frame | |
161 * @returns -1 if no complete frame could be created | |
162 */ | |
1988 | 163 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
1613 | 164 { |
165 #if 0 | |
166 if(pc->overread){ | |
167 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
168 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
169 } | |
170 #endif | |
171 | |
172 /* copy overreaded bytes from last frame into buffer */ | |
173 for(; pc->overread>0; pc->overread--){ | |
174 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
175 } | |
176 | |
177 pc->last_index= pc->index; | |
178 | |
179 /* copy into buffer end return */ | |
180 if(next == END_NOT_FOUND){ | |
181 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
182 | |
183 memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
184 pc->index += *buf_size; | |
185 return -1; | |
186 } | |
187 | |
188 *buf_size= | |
189 pc->overread_index= pc->index + next; | |
190 | |
191 /* append to buffer */ | |
192 if(pc->index){ | |
193 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
194 | |
195 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
196 pc->index = 0; | |
197 *buf= pc->buffer; | |
198 } | |
199 | |
200 /* store overread bytes */ | |
201 for(;next < 0; next++){ | |
202 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
203 pc->overread++; | |
204 } | |
205 | |
206 #if 0 | |
207 if(pc->overread){ | |
208 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
209 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
210 } | |
211 #endif | |
212 | |
213 return 0; | |
214 } | |
215 | |
216 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end) | |
217 { | |
218 const uint8_t *buf_ptr; | |
219 unsigned int state=0xFFFFFFFF, v; | |
220 int val; | |
221 | |
222 buf_ptr = *pbuf_ptr; | |
223 while (buf_ptr < buf_end) { | |
224 v = *buf_ptr++; | |
225 if (state == 0x000001) { | |
226 state = ((state << 8) | v) & 0xffffff; | |
227 val = state; | |
228 goto found; | |
229 } | |
230 state = ((state << 8) | v) & 0xffffff; | |
231 } | |
232 val = -1; | |
233 found: | |
234 *pbuf_ptr = buf_ptr; | |
235 return val; | |
236 } | |
237 | |
238 /* XXX: merge with libavcodec ? */ | |
239 #define MPEG1_FRAME_RATE_BASE 1001 | |
240 | |
241 static const int frame_rate_tab[16] = { | |
242 0, | |
243 24000, | |
244 24024, | |
245 25025, | |
246 30000, | |
247 30030, | |
248 50050, | |
249 60000, | |
250 60060, | |
251 // Xing's 15fps: (9) | |
252 15015, | |
253 // libmpeg3's "Unofficial economy rates": (10-13) | |
254 5005, | |
255 10010, | |
256 12012, | |
257 15015, | |
258 // random, just to avoid segfault !never encode these | |
259 25025, | |
260 25025, | |
261 }; | |
262 | |
263 static void mpegvideo_extract_headers(AVCodecParserContext *s, | |
264 AVCodecContext *avctx, | |
265 const uint8_t *buf, int buf_size) | |
266 { | |
267 ParseContext1 *pc = s->priv_data; | |
268 const uint8_t *buf_end; | |
269 int32_t start_code; | |
270 int frame_rate_index, ext_type, bytes_left; | |
271 int frame_rate_ext_n, frame_rate_ext_d; | |
272 int top_field_first, repeat_first_field, progressive_frame; | |
273 int horiz_size_ext, vert_size_ext; | |
274 | |
275 s->repeat_pict = 0; | |
276 buf_end = buf + buf_size; | |
277 while (buf < buf_end) { | |
278 start_code = find_start_code(&buf, buf_end); | |
279 bytes_left = buf_end - buf; | |
280 switch(start_code) { | |
281 case PICTURE_START_CODE: | |
282 if (bytes_left >= 2) { | |
283 s->pict_type = (buf[1] >> 3) & 7; | |
284 } | |
285 break; | |
286 case SEQ_START_CODE: | |
287 if (bytes_left >= 4) { | |
288 pc->width = avctx->width = (buf[0] << 4) | (buf[1] >> 4); | |
289 pc->height = avctx->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
290 frame_rate_index = buf[3] & 0xf; | |
291 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index]; | |
292 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE; | |
1681 | 293 avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
294 avctx->sub_id = 1; | |
1613 | 295 } |
296 break; | |
297 case EXT_START_CODE: | |
298 if (bytes_left >= 1) { | |
299 ext_type = (buf[0] >> 4); | |
300 switch(ext_type) { | |
301 case 0x1: /* sequence extension */ | |
302 if (bytes_left >= 6) { | |
303 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
304 vert_size_ext = (buf[2] >> 5) & 3; | |
305 frame_rate_ext_n = (buf[5] >> 5) & 3; | |
306 frame_rate_ext_d = (buf[5] & 0x1f); | |
307 pc->progressive_sequence = buf[1] & (1 << 3); | |
308 | |
309 avctx->width = pc->width | (horiz_size_ext << 12); | |
310 avctx->height = pc->height | (vert_size_ext << 12); | |
311 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1); | |
312 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
1681 | 313 avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
1613 | 314 avctx->sub_id = 2; /* forces MPEG2 */ |
315 } | |
316 break; | |
317 case 0x8: /* picture coding extension */ | |
318 if (bytes_left >= 5) { | |
319 top_field_first = buf[3] & (1 << 7); | |
320 repeat_first_field = buf[3] & (1 << 1); | |
321 progressive_frame = buf[4] & (1 << 7); | |
322 | |
323 /* check if we must repeat the frame */ | |
324 if (repeat_first_field) { | |
325 if (pc->progressive_sequence) { | |
326 if (top_field_first) | |
327 s->repeat_pict = 4; | |
328 else | |
329 s->repeat_pict = 2; | |
330 } else if (progressive_frame) { | |
331 s->repeat_pict = 1; | |
332 } | |
333 } | |
334 } | |
335 break; | |
336 } | |
337 } | |
338 break; | |
339 case -1: | |
340 goto the_end; | |
341 default: | |
342 /* we stop parsing when we encounter a slice. It ensures | |
343 that this function takes a negligible amount of time */ | |
344 if (start_code >= SLICE_MIN_START_CODE && | |
345 start_code <= SLICE_MAX_START_CODE) | |
346 goto the_end; | |
347 break; | |
348 } | |
349 } | |
350 the_end: ; | |
351 } | |
352 | |
353 static int mpegvideo_parse(AVCodecParserContext *s, | |
354 AVCodecContext *avctx, | |
355 uint8_t **poutbuf, int *poutbuf_size, | |
356 const uint8_t *buf, int buf_size) | |
357 { | |
1988 | 358 ParseContext1 *pc1 = s->priv_data; |
359 ParseContext *pc= &pc1->pc; | |
1613 | 360 int next; |
361 | |
1988 | 362 next= ff_mpeg1_find_frame_end(pc, buf, buf_size); |
1613 | 363 |
1988 | 364 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
1613 | 365 *poutbuf = NULL; |
366 *poutbuf_size = 0; | |
367 return buf_size; | |
368 } | |
369 /* we have a full frame : we just parse the first few MPEG headers | |
370 to have the full timing information. The time take by this | |
371 function should be negligible for uncorrupted streams */ | |
372 mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
373 #if 0 | |
374 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", | |
375 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict); | |
376 #endif | |
377 | |
378 *poutbuf = (uint8_t *)buf; | |
379 *poutbuf_size = buf_size; | |
380 return next; | |
381 } | |
382 | |
1988 | 383 void ff_parse_close(AVCodecParserContext *s) |
1613 | 384 { |
1988 | 385 ParseContext *pc = s->priv_data; |
1613 | 386 |
387 av_free(pc->buffer); | |
1988 | 388 } |
389 | |
390 static void parse1_close(AVCodecParserContext *s) | |
391 { | |
392 ParseContext1 *pc1 = s->priv_data; | |
393 | |
394 av_free(pc1->pc.buffer); | |
395 av_free(pc1->enc); | |
1613 | 396 } |
397 | |
398 /*************************/ | |
399 | |
400 /* used by parser */ | |
401 /* XXX: make it use less memory */ | |
402 static int av_mpeg4_decode_header(AVCodecParserContext *s1, | |
403 AVCodecContext *avctx, | |
404 const uint8_t *buf, int buf_size) | |
405 { | |
406 ParseContext1 *pc = s1->priv_data; | |
407 MpegEncContext *s = pc->enc; | |
408 GetBitContext gb1, *gb = &gb1; | |
409 int ret; | |
410 | |
411 s->avctx = avctx; | |
1614 | 412 s->current_picture_ptr = &s->current_picture; |
413 | |
414 if (avctx->extradata_size && pc->first_picture){ | |
415 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
416 ret = ff_mpeg4_decode_picture_header(s, gb); | |
417 } | |
418 | |
1613 | 419 init_get_bits(gb, buf, 8 * buf_size); |
420 ret = ff_mpeg4_decode_picture_header(s, gb); | |
421 if (s->width) { | |
422 avctx->width = s->width; | |
423 avctx->height = s->height; | |
424 } | |
1614 | 425 pc->first_picture = 0; |
1613 | 426 return ret; |
427 } | |
428 | |
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1988
diff
changeset
|
429 static int mpeg4video_parse_init(AVCodecParserContext *s) |
1613 | 430 { |
431 ParseContext1 *pc = s->priv_data; | |
1614 | 432 |
1613 | 433 pc->enc = av_mallocz(sizeof(MpegEncContext)); |
434 if (!pc->enc) | |
435 return -1; | |
1614 | 436 pc->first_picture = 1; |
1613 | 437 return 0; |
438 } | |
439 | |
440 static int mpeg4video_parse(AVCodecParserContext *s, | |
441 AVCodecContext *avctx, | |
442 uint8_t **poutbuf, int *poutbuf_size, | |
443 const uint8_t *buf, int buf_size) | |
444 { | |
1988 | 445 ParseContext *pc = s->priv_data; |
1613 | 446 int next; |
447 | |
1988 | 448 next= ff_mpeg4_find_frame_end(pc, buf, buf_size); |
1613 | 449 |
1988 | 450 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
1613 | 451 *poutbuf = NULL; |
452 *poutbuf_size = 0; | |
453 return buf_size; | |
454 } | |
455 av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
456 | |
457 *poutbuf = (uint8_t *)buf; | |
458 *poutbuf_size = buf_size; | |
459 return next; | |
460 } | |
461 | |
462 /*************************/ | |
463 | |
464 typedef struct MpegAudioParseContext { | |
465 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ | |
466 uint8_t *inbuf_ptr; | |
467 int frame_size; | |
468 int free_format_frame_size; | |
469 int free_format_next_header; | |
470 } MpegAudioParseContext; | |
471 | |
472 #define MPA_HEADER_SIZE 4 | |
473 | |
474 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
475 #define SAME_HEADER_MASK \ | |
476 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19)) | |
477 | |
478 static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
479 { | |
480 MpegAudioParseContext *s = s1->priv_data; | |
481 s->inbuf_ptr = s->inbuf; | |
482 return 0; | |
483 } | |
484 | |
485 static int mpegaudio_parse(AVCodecParserContext *s1, | |
486 AVCodecContext *avctx, | |
487 uint8_t **poutbuf, int *poutbuf_size, | |
488 const uint8_t *buf, int buf_size) | |
489 { | |
490 MpegAudioParseContext *s = s1->priv_data; | |
491 int len, ret; | |
492 uint32_t header; | |
493 const uint8_t *buf_ptr; | |
494 | |
495 *poutbuf = NULL; | |
496 *poutbuf_size = 0; | |
497 buf_ptr = buf; | |
498 while (buf_size > 0) { | |
499 len = s->inbuf_ptr - s->inbuf; | |
500 if (s->frame_size == 0) { | |
501 /* special case for next header for first frame in free | |
502 format case (XXX: find a simpler method) */ | |
503 if (s->free_format_next_header != 0) { | |
504 s->inbuf[0] = s->free_format_next_header >> 24; | |
505 s->inbuf[1] = s->free_format_next_header >> 16; | |
506 s->inbuf[2] = s->free_format_next_header >> 8; | |
507 s->inbuf[3] = s->free_format_next_header; | |
508 s->inbuf_ptr = s->inbuf + 4; | |
509 s->free_format_next_header = 0; | |
510 goto got_header; | |
511 } | |
512 /* no header seen : find one. We need at least MPA_HEADER_SIZE | |
513 bytes to parse it */ | |
514 len = MPA_HEADER_SIZE - len; | |
515 if (len > buf_size) | |
516 len = buf_size; | |
517 if (len > 0) { | |
518 memcpy(s->inbuf_ptr, buf_ptr, len); | |
519 buf_ptr += len; | |
520 buf_size -= len; | |
521 s->inbuf_ptr += len; | |
522 } | |
523 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
524 got_header: | |
525 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
526 (s->inbuf[2] << 8) | s->inbuf[3]; | |
527 | |
528 ret = mpa_decode_header(avctx, header); | |
529 if (ret < 0) { | |
530 /* no sync found : move by one byte (inefficient, but simple!) */ | |
531 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
532 s->inbuf_ptr--; | |
533 dprintf("skip %x\n", header); | |
534 /* reset free format frame size to give a chance | |
535 to get a new bitrate */ | |
536 s->free_format_frame_size = 0; | |
537 } else { | |
538 s->frame_size = ret; | |
539 #if 0 | |
540 /* free format: prepare to compute frame size */ | |
541 if (decode_header(s, header) == 1) { | |
542 s->frame_size = -1; | |
543 } | |
544 #endif | |
545 } | |
546 } | |
547 } else | |
548 #if 0 | |
549 if (s->frame_size == -1) { | |
550 /* free format : find next sync to compute frame size */ | |
551 len = MPA_MAX_CODED_FRAME_SIZE - len; | |
552 if (len > buf_size) | |
553 len = buf_size; | |
554 if (len == 0) { | |
555 /* frame too long: resync */ | |
556 s->frame_size = 0; | |
557 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
558 s->inbuf_ptr--; | |
559 } else { | |
560 uint8_t *p, *pend; | |
561 uint32_t header1; | |
562 int padding; | |
563 | |
564 memcpy(s->inbuf_ptr, buf_ptr, len); | |
565 /* check for header */ | |
566 p = s->inbuf_ptr - 3; | |
567 pend = s->inbuf_ptr + len - 4; | |
568 while (p <= pend) { | |
569 header = (p[0] << 24) | (p[1] << 16) | | |
570 (p[2] << 8) | p[3]; | |
571 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
572 (s->inbuf[2] << 8) | s->inbuf[3]; | |
573 /* check with high probability that we have a | |
574 valid header */ | |
575 if ((header & SAME_HEADER_MASK) == | |
576 (header1 & SAME_HEADER_MASK)) { | |
577 /* header found: update pointers */ | |
578 len = (p + 4) - s->inbuf_ptr; | |
579 buf_ptr += len; | |
580 buf_size -= len; | |
581 s->inbuf_ptr = p; | |
582 /* compute frame size */ | |
583 s->free_format_next_header = header; | |
584 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
585 padding = (header1 >> 9) & 1; | |
586 if (s->layer == 1) | |
587 s->free_format_frame_size -= padding * 4; | |
588 else | |
589 s->free_format_frame_size -= padding; | |
590 dprintf("free frame size=%d padding=%d\n", | |
591 s->free_format_frame_size, padding); | |
592 decode_header(s, header1); | |
593 goto next_data; | |
594 } | |
595 p++; | |
596 } | |
597 /* not found: simply increase pointers */ | |
598 buf_ptr += len; | |
599 s->inbuf_ptr += len; | |
600 buf_size -= len; | |
601 } | |
602 } else | |
603 #endif | |
604 if (len < s->frame_size) { | |
605 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
606 s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
607 len = s->frame_size - len; | |
608 if (len > buf_size) | |
609 len = buf_size; | |
610 memcpy(s->inbuf_ptr, buf_ptr, len); | |
611 buf_ptr += len; | |
612 s->inbuf_ptr += len; | |
613 buf_size -= len; | |
614 } | |
615 // next_data: | |
616 if (s->frame_size > 0 && | |
617 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { | |
618 *poutbuf = s->inbuf; | |
619 *poutbuf_size = s->inbuf_ptr - s->inbuf; | |
620 s->inbuf_ptr = s->inbuf; | |
621 s->frame_size = 0; | |
622 break; | |
623 } | |
624 } | |
625 return buf_ptr - buf; | |
626 } | |
627 | |
628 #ifdef CONFIG_AC3 | |
629 extern int a52_syncinfo (const uint8_t * buf, int * flags, | |
630 int * sample_rate, int * bit_rate); | |
631 | |
632 typedef struct AC3ParseContext { | |
633 uint8_t inbuf[4096]; /* input buffer */ | |
634 uint8_t *inbuf_ptr; | |
635 int frame_size; | |
636 int flags; | |
637 } AC3ParseContext; | |
638 | |
639 #define AC3_HEADER_SIZE 7 | |
640 #define A52_LFE 16 | |
641 | |
642 static int ac3_parse_init(AVCodecParserContext *s1) | |
643 { | |
644 AC3ParseContext *s = s1->priv_data; | |
645 s->inbuf_ptr = s->inbuf; | |
646 return 0; | |
647 } | |
648 | |
649 static int ac3_parse(AVCodecParserContext *s1, | |
650 AVCodecContext *avctx, | |
651 uint8_t **poutbuf, int *poutbuf_size, | |
652 const uint8_t *buf, int buf_size) | |
653 { | |
654 AC3ParseContext *s = s1->priv_data; | |
655 const uint8_t *buf_ptr; | |
656 int len, sample_rate, bit_rate; | |
657 static const int ac3_channels[8] = { | |
658 2, 1, 2, 3, 3, 4, 4, 5 | |
659 }; | |
660 | |
661 *poutbuf = NULL; | |
662 *poutbuf_size = 0; | |
663 | |
664 buf_ptr = buf; | |
665 while (buf_size > 0) { | |
666 len = s->inbuf_ptr - s->inbuf; | |
667 if (s->frame_size == 0) { | |
668 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
669 len = AC3_HEADER_SIZE - len; | |
670 if (len > buf_size) | |
671 len = buf_size; | |
672 memcpy(s->inbuf_ptr, buf_ptr, len); | |
673 buf_ptr += len; | |
674 s->inbuf_ptr += len; | |
675 buf_size -= len; | |
676 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { | |
677 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
678 if (len == 0) { | |
679 /* no sync found : move by one byte (inefficient, but simple!) */ | |
680 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); | |
681 s->inbuf_ptr--; | |
682 } else { | |
683 s->frame_size = len; | |
684 /* update codec info */ | |
685 avctx->sample_rate = sample_rate; | |
1987 | 686 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
687 if(avctx->channels!=1 && avctx->channels!=2){ | |
688 avctx->channels = ac3_channels[s->flags & 7]; | |
689 if (s->flags & A52_LFE) | |
690 avctx->channels++; | |
691 } | |
1613 | 692 avctx->bit_rate = bit_rate; |
693 avctx->frame_size = 6 * 256; | |
694 } | |
695 } | |
696 } else if (len < s->frame_size) { | |
697 len = s->frame_size - len; | |
698 if (len > buf_size) | |
699 len = buf_size; | |
700 | |
701 memcpy(s->inbuf_ptr, buf_ptr, len); | |
702 buf_ptr += len; | |
703 s->inbuf_ptr += len; | |
704 buf_size -= len; | |
705 } else { | |
706 *poutbuf = s->inbuf; | |
707 *poutbuf_size = s->frame_size; | |
708 s->inbuf_ptr = s->inbuf; | |
709 s->frame_size = 0; | |
710 break; | |
711 } | |
712 } | |
713 return buf_ptr - buf; | |
714 } | |
715 #endif | |
716 | |
717 AVCodecParser mpegvideo_parser = { | |
718 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
719 sizeof(ParseContext1), | |
720 NULL, | |
721 mpegvideo_parse, | |
1988 | 722 parse1_close, |
1613 | 723 }; |
724 | |
725 AVCodecParser mpeg4video_parser = { | |
726 { CODEC_ID_MPEG4 }, | |
727 sizeof(ParseContext1), | |
728 mpeg4video_parse_init, | |
729 mpeg4video_parse, | |
1988 | 730 parse1_close, |
1613 | 731 }; |
732 | |
733 AVCodecParser mpegaudio_parser = { | |
734 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
735 sizeof(MpegAudioParseContext), | |
736 mpegaudio_parse_init, | |
737 mpegaudio_parse, | |
738 NULL, | |
739 }; | |
740 | |
741 #ifdef CONFIG_AC3 | |
742 AVCodecParser ac3_parser = { | |
743 { CODEC_ID_AC3 }, | |
744 sizeof(AC3ParseContext), | |
745 ac3_parse_init, | |
746 ac3_parse, | |
747 NULL, | |
748 }; | |
749 #endif |