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
|
|
66 int av_parser_parse(AVCodecParserContext *s,
|
|
67 AVCodecContext *avctx,
|
|
68 uint8_t **poutbuf, int *poutbuf_size,
|
|
69 const uint8_t *buf, int buf_size)
|
|
70 {
|
|
71 int index;
|
|
72 /* WARNING: the returned index can be negative */
|
|
73 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
|
|
74 /* update the file pointer */
|
|
75 if (*poutbuf_size) {
|
|
76 s->frame_offset = s->last_frame_offset;
|
|
77 s->last_frame_offset = s->cur_offset + index;
|
|
78 }
|
|
79 if (index < 0)
|
|
80 index = 0;
|
|
81 s->cur_offset += index;
|
|
82 return index;
|
|
83 }
|
|
84
|
|
85 void av_parser_close(AVCodecParserContext *s)
|
|
86 {
|
|
87 if (s->parser->parser_close)
|
|
88 s->parser->parser_close(s);
|
|
89 av_free(s->priv_data);
|
|
90 av_free(s);
|
|
91 }
|
|
92
|
|
93 /*****************************************************/
|
|
94
|
|
95 //#define END_NOT_FOUND (-100)
|
|
96
|
|
97 #define PICTURE_START_CODE 0x00000100
|
|
98 #define SEQ_START_CODE 0x000001b3
|
|
99 #define EXT_START_CODE 0x000001b5
|
|
100 #define SLICE_MIN_START_CODE 0x00000101
|
|
101 #define SLICE_MAX_START_CODE 0x000001af
|
|
102
|
|
103 typedef struct ParseContext1{
|
|
104 uint8_t *buffer;
|
|
105 int index;
|
|
106 int last_index;
|
|
107 int buffer_size;
|
|
108 uint32_t state; ///< contains the last few bytes in MSB order
|
|
109 int frame_start_found;
|
|
110 int overread; ///< the number of bytes which where irreversibly read from the next frame
|
|
111 int overread_index; ///< the index into ParseContext1.buffer of the overreaded bytes
|
|
112
|
|
113 /* MPEG2 specific */
|
|
114 int frame_rate;
|
|
115 int progressive_sequence;
|
|
116 int width, height;
|
1614
|
117
|
1613
|
118 /* XXX: suppress that, needed by MPEG4 */
|
|
119 MpegEncContext *enc;
|
1614
|
120 int first_picture;
|
1613
|
121 } ParseContext1;
|
|
122
|
|
123 /**
|
|
124 * combines the (truncated) bitstream to a complete frame
|
|
125 * @returns -1 if no complete frame could be created
|
|
126 */
|
|
127 static int ff_combine_frame1(ParseContext1 *pc, int next, uint8_t **buf, int *buf_size)
|
|
128 {
|
|
129 #if 0
|
|
130 if(pc->overread){
|
|
131 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
|
|
132 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
|
|
133 }
|
|
134 #endif
|
|
135
|
|
136 /* copy overreaded bytes from last frame into buffer */
|
|
137 for(; pc->overread>0; pc->overread--){
|
|
138 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
|
|
139 }
|
|
140
|
|
141 pc->last_index= pc->index;
|
|
142
|
|
143 /* copy into buffer end return */
|
|
144 if(next == END_NOT_FOUND){
|
|
145 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
146
|
|
147 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
|
|
148 pc->index += *buf_size;
|
|
149 return -1;
|
|
150 }
|
|
151
|
|
152 *buf_size=
|
|
153 pc->overread_index= pc->index + next;
|
|
154
|
|
155 /* append to buffer */
|
|
156 if(pc->index){
|
|
157 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
158
|
|
159 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
|
|
160 pc->index = 0;
|
|
161 *buf= pc->buffer;
|
|
162 }
|
|
163
|
|
164 /* store overread bytes */
|
|
165 for(;next < 0; next++){
|
|
166 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
|
|
167 pc->overread++;
|
|
168 }
|
|
169
|
|
170 #if 0
|
|
171 if(pc->overread){
|
|
172 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
|
|
173 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
|
|
174 }
|
|
175 #endif
|
|
176
|
|
177 return 0;
|
|
178 }
|
|
179
|
|
180 /**
|
|
181 * finds the end of the current frame in the bitstream.
|
|
182 * @return the position of the first byte of the next frame, or -1
|
|
183 */
|
|
184 static int mpeg1_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
|
|
185 {
|
|
186 int i;
|
|
187 uint32_t state;
|
|
188
|
|
189 state= pc->state;
|
|
190
|
|
191 i=0;
|
|
192 if(!pc->frame_start_found){
|
|
193 for(i=0; i<buf_size; i++){
|
|
194 state= (state<<8) | buf[i];
|
|
195 if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
|
|
196 i++;
|
|
197 pc->frame_start_found=1;
|
|
198 break;
|
|
199 }
|
|
200 }
|
|
201 }
|
|
202
|
|
203 if(pc->frame_start_found){
|
|
204 for(; i<buf_size; i++){
|
|
205 state= (state<<8) | buf[i];
|
|
206 if((state&0xFFFFFF00) == 0x100){
|
|
207 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
|
|
208 pc->frame_start_found=0;
|
|
209 pc->state=-1;
|
|
210 return i-3;
|
|
211 }
|
|
212 }
|
|
213 }
|
|
214 }
|
|
215 pc->state= state;
|
|
216 return END_NOT_FOUND;
|
|
217 }
|
|
218
|
|
219 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
|
|
220 {
|
|
221 const uint8_t *buf_ptr;
|
|
222 unsigned int state=0xFFFFFFFF, v;
|
|
223 int val;
|
|
224
|
|
225 buf_ptr = *pbuf_ptr;
|
|
226 while (buf_ptr < buf_end) {
|
|
227 v = *buf_ptr++;
|
|
228 if (state == 0x000001) {
|
|
229 state = ((state << 8) | v) & 0xffffff;
|
|
230 val = state;
|
|
231 goto found;
|
|
232 }
|
|
233 state = ((state << 8) | v) & 0xffffff;
|
|
234 }
|
|
235 val = -1;
|
|
236 found:
|
|
237 *pbuf_ptr = buf_ptr;
|
|
238 return val;
|
|
239 }
|
|
240
|
|
241 /* XXX: merge with libavcodec ? */
|
|
242 #define MPEG1_FRAME_RATE_BASE 1001
|
|
243
|
|
244 static const int frame_rate_tab[16] = {
|
|
245 0,
|
|
246 24000,
|
|
247 24024,
|
|
248 25025,
|
|
249 30000,
|
|
250 30030,
|
|
251 50050,
|
|
252 60000,
|
|
253 60060,
|
|
254 // Xing's 15fps: (9)
|
|
255 15015,
|
|
256 // libmpeg3's "Unofficial economy rates": (10-13)
|
|
257 5005,
|
|
258 10010,
|
|
259 12012,
|
|
260 15015,
|
|
261 // random, just to avoid segfault !never encode these
|
|
262 25025,
|
|
263 25025,
|
|
264 };
|
|
265
|
|
266 static void mpegvideo_extract_headers(AVCodecParserContext *s,
|
|
267 AVCodecContext *avctx,
|
|
268 const uint8_t *buf, int buf_size)
|
|
269 {
|
|
270 ParseContext1 *pc = s->priv_data;
|
|
271 const uint8_t *buf_end;
|
|
272 int32_t start_code;
|
|
273 int frame_rate_index, ext_type, bytes_left;
|
|
274 int frame_rate_ext_n, frame_rate_ext_d;
|
|
275 int top_field_first, repeat_first_field, progressive_frame;
|
|
276 int horiz_size_ext, vert_size_ext;
|
|
277
|
|
278 s->repeat_pict = 0;
|
|
279 buf_end = buf + buf_size;
|
|
280 while (buf < buf_end) {
|
|
281 start_code = find_start_code(&buf, buf_end);
|
|
282 bytes_left = buf_end - buf;
|
|
283 switch(start_code) {
|
|
284 case PICTURE_START_CODE:
|
|
285 if (bytes_left >= 2) {
|
|
286 s->pict_type = (buf[1] >> 3) & 7;
|
|
287 }
|
|
288 break;
|
|
289 case SEQ_START_CODE:
|
|
290 if (bytes_left >= 4) {
|
|
291 pc->width = avctx->width = (buf[0] << 4) | (buf[1] >> 4);
|
|
292 pc->height = avctx->height = ((buf[1] & 0x0f) << 8) | buf[2];
|
|
293 frame_rate_index = buf[3] & 0xf;
|
|
294 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
|
|
295 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
|
|
296 }
|
|
297 break;
|
|
298 case EXT_START_CODE:
|
|
299 if (bytes_left >= 1) {
|
|
300 ext_type = (buf[0] >> 4);
|
|
301 switch(ext_type) {
|
|
302 case 0x1: /* sequence extension */
|
|
303 if (bytes_left >= 6) {
|
|
304 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
|
|
305 vert_size_ext = (buf[2] >> 5) & 3;
|
|
306 frame_rate_ext_n = (buf[5] >> 5) & 3;
|
|
307 frame_rate_ext_d = (buf[5] & 0x1f);
|
|
308 pc->progressive_sequence = buf[1] & (1 << 3);
|
|
309
|
|
310 avctx->width = pc->width | (horiz_size_ext << 12);
|
|
311 avctx->height = pc->height | (vert_size_ext << 12);
|
|
312 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
|
|
313 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
|
|
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 {
|
|
358 ParseContext1 *pc = s->priv_data;
|
|
359 int next;
|
|
360
|
|
361 next= mpeg1_find_frame_end(pc, buf, buf_size);
|
|
362
|
|
363 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
364 *poutbuf = NULL;
|
|
365 *poutbuf_size = 0;
|
|
366 return buf_size;
|
|
367 }
|
|
368 /* we have a full frame : we just parse the first few MPEG headers
|
|
369 to have the full timing information. The time take by this
|
|
370 function should be negligible for uncorrupted streams */
|
|
371 mpegvideo_extract_headers(s, avctx, buf, buf_size);
|
|
372 #if 0
|
|
373 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
|
|
374 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
|
|
375 #endif
|
|
376
|
|
377 *poutbuf = (uint8_t *)buf;
|
|
378 *poutbuf_size = buf_size;
|
|
379 return next;
|
|
380 }
|
|
381
|
|
382 static void mpegvideo_parse_close(AVCodecParserContext *s)
|
|
383 {
|
|
384 ParseContext1 *pc = s->priv_data;
|
|
385
|
|
386 av_free(pc->buffer);
|
|
387 av_free(pc->enc);
|
|
388 }
|
|
389
|
|
390 /*************************/
|
|
391
|
|
392 /**
|
|
393 * finds the end of the current frame in the bitstream.
|
|
394 * @return the position of the first byte of the next frame, or -1
|
|
395 */
|
|
396 static int mpeg4_find_frame_end(ParseContext1 *pc,
|
|
397 const uint8_t *buf, int buf_size)
|
|
398 {
|
|
399 int vop_found, i;
|
|
400 uint32_t state;
|
|
401
|
|
402 vop_found= pc->frame_start_found;
|
|
403 state= pc->state;
|
|
404
|
|
405 i=0;
|
|
406 if(!vop_found){
|
|
407 for(i=0; i<buf_size; i++){
|
|
408 state= (state<<8) | buf[i];
|
|
409 if(state == 0x1B6){
|
|
410 i++;
|
|
411 vop_found=1;
|
|
412 break;
|
|
413 }
|
|
414 }
|
|
415 }
|
|
416
|
|
417 if(vop_found){
|
|
418 for(; i<buf_size; i++){
|
|
419 state= (state<<8) | buf[i];
|
|
420 if((state&0xFFFFFF00) == 0x100){
|
|
421 pc->frame_start_found=0;
|
|
422 pc->state=-1;
|
|
423 return i-3;
|
|
424 }
|
|
425 }
|
|
426 }
|
|
427 pc->frame_start_found= vop_found;
|
|
428 pc->state= state;
|
|
429 return END_NOT_FOUND;
|
|
430 }
|
|
431
|
|
432 /* used by parser */
|
|
433 /* XXX: make it use less memory */
|
|
434 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
|
|
435 AVCodecContext *avctx,
|
|
436 const uint8_t *buf, int buf_size)
|
|
437 {
|
|
438 ParseContext1 *pc = s1->priv_data;
|
|
439 MpegEncContext *s = pc->enc;
|
|
440 GetBitContext gb1, *gb = &gb1;
|
|
441 int ret;
|
|
442
|
|
443 s->avctx = avctx;
|
1614
|
444 s->current_picture_ptr = &s->current_picture;
|
|
445
|
|
446 if (avctx->extradata_size && pc->first_picture){
|
|
447 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
|
|
448 ret = ff_mpeg4_decode_picture_header(s, gb);
|
|
449 }
|
|
450
|
1613
|
451 init_get_bits(gb, buf, 8 * buf_size);
|
|
452 ret = ff_mpeg4_decode_picture_header(s, gb);
|
|
453 if (s->width) {
|
|
454 avctx->width = s->width;
|
|
455 avctx->height = s->height;
|
|
456 }
|
1614
|
457 pc->first_picture = 0;
|
1613
|
458 return ret;
|
|
459 }
|
|
460
|
|
461 int mpeg4video_parse_init(AVCodecParserContext *s)
|
|
462 {
|
|
463 ParseContext1 *pc = s->priv_data;
|
1614
|
464
|
1613
|
465 pc->enc = av_mallocz(sizeof(MpegEncContext));
|
|
466 if (!pc->enc)
|
|
467 return -1;
|
1614
|
468 pc->first_picture = 1;
|
1613
|
469 return 0;
|
|
470 }
|
|
471
|
|
472 static int mpeg4video_parse(AVCodecParserContext *s,
|
|
473 AVCodecContext *avctx,
|
|
474 uint8_t **poutbuf, int *poutbuf_size,
|
|
475 const uint8_t *buf, int buf_size)
|
|
476 {
|
|
477 ParseContext1 *pc = s->priv_data;
|
|
478 int next;
|
|
479
|
|
480 next= mpeg4_find_frame_end(pc, buf, buf_size);
|
|
481
|
|
482 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
483 *poutbuf = NULL;
|
|
484 *poutbuf_size = 0;
|
|
485 return buf_size;
|
|
486 }
|
|
487 av_mpeg4_decode_header(s, avctx, buf, buf_size);
|
|
488
|
|
489 *poutbuf = (uint8_t *)buf;
|
|
490 *poutbuf_size = buf_size;
|
|
491 return next;
|
|
492 }
|
|
493
|
|
494 /*************************/
|
|
495
|
|
496 static int h263_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
|
|
497 {
|
|
498 int vop_found, i;
|
|
499 uint32_t state;
|
|
500
|
|
501 vop_found= pc->frame_start_found;
|
|
502 state= pc->state;
|
|
503
|
|
504 i=0;
|
|
505 if(!vop_found){
|
|
506 for(i=0; i<buf_size; i++){
|
|
507 state= (state<<8) | buf[i];
|
|
508 if(state>>(32-22) == 0x20){
|
|
509 i++;
|
|
510 vop_found=1;
|
|
511 break;
|
|
512 }
|
|
513 }
|
|
514 }
|
|
515
|
|
516 if(vop_found){
|
|
517 for(; i<buf_size; i++){
|
|
518 state= (state<<8) | buf[i];
|
|
519 if(state>>(32-22) == 0x20){
|
|
520 pc->frame_start_found=0;
|
|
521 pc->state=-1;
|
|
522 return i-3;
|
|
523 }
|
|
524 }
|
|
525 }
|
|
526 pc->frame_start_found= vop_found;
|
|
527 pc->state= state;
|
|
528
|
|
529 return END_NOT_FOUND;
|
|
530 }
|
|
531
|
|
532 static int h263_parse(AVCodecParserContext *s,
|
|
533 AVCodecContext *avctx,
|
|
534 uint8_t **poutbuf, int *poutbuf_size,
|
|
535 const uint8_t *buf, int buf_size)
|
|
536 {
|
|
537 ParseContext1 *pc = s->priv_data;
|
|
538 int next;
|
|
539
|
|
540 next= h263_find_frame_end(pc, buf, buf_size);
|
|
541
|
|
542 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
543 *poutbuf = NULL;
|
|
544 *poutbuf_size = 0;
|
|
545 return buf_size;
|
|
546 }
|
|
547
|
|
548 *poutbuf = (uint8_t *)buf;
|
|
549 *poutbuf_size = buf_size;
|
|
550 return next;
|
|
551 }
|
|
552
|
|
553 /*************************/
|
|
554
|
|
555 /**
|
|
556 * finds the end of the current frame in the bitstream.
|
|
557 * @return the position of the first byte of the next frame, or -1
|
|
558 */
|
|
559 static int h264_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
|
|
560 {
|
|
561 int i;
|
|
562 uint32_t state;
|
|
563 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
|
|
564 // mb_addr= pc->mb_addr - 1;
|
|
565 state= pc->state;
|
|
566 //FIXME this will fail with slices
|
|
567 for(i=0; i<buf_size; i++){
|
|
568 state= (state<<8) | buf[i];
|
|
569 if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
|
|
570 if(pc->frame_start_found){
|
|
571 pc->state=-1;
|
|
572 pc->frame_start_found= 0;
|
|
573 return i-3;
|
|
574 }
|
|
575 pc->frame_start_found= 1;
|
|
576 }
|
|
577 }
|
|
578
|
|
579 pc->state= state;
|
|
580 return END_NOT_FOUND;
|
|
581 }
|
|
582
|
|
583 static int h264_parse(AVCodecParserContext *s,
|
|
584 AVCodecContext *avctx,
|
|
585 uint8_t **poutbuf, int *poutbuf_size,
|
|
586 const uint8_t *buf, int buf_size)
|
|
587 {
|
|
588 ParseContext1 *pc = s->priv_data;
|
|
589 int next;
|
|
590
|
|
591 next= h264_find_frame_end(pc, buf, buf_size);
|
|
592
|
|
593 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
594 *poutbuf = NULL;
|
|
595 *poutbuf_size = 0;
|
|
596 return buf_size;
|
|
597 }
|
|
598
|
|
599 *poutbuf = (uint8_t *)buf;
|
|
600 *poutbuf_size = buf_size;
|
|
601 return next;
|
|
602 }
|
|
603
|
|
604 /*************************/
|
|
605
|
|
606 typedef struct MpegAudioParseContext {
|
|
607 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
|
|
608 uint8_t *inbuf_ptr;
|
|
609 int frame_size;
|
|
610 int free_format_frame_size;
|
|
611 int free_format_next_header;
|
|
612 } MpegAudioParseContext;
|
|
613
|
|
614 #define MPA_HEADER_SIZE 4
|
|
615
|
|
616 /* header + layer + bitrate + freq + lsf/mpeg25 */
|
|
617 #define SAME_HEADER_MASK \
|
|
618 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
|
|
619
|
|
620 static int mpegaudio_parse_init(AVCodecParserContext *s1)
|
|
621 {
|
|
622 MpegAudioParseContext *s = s1->priv_data;
|
|
623 s->inbuf_ptr = s->inbuf;
|
|
624 return 0;
|
|
625 }
|
|
626
|
|
627 static int mpegaudio_parse(AVCodecParserContext *s1,
|
|
628 AVCodecContext *avctx,
|
|
629 uint8_t **poutbuf, int *poutbuf_size,
|
|
630 const uint8_t *buf, int buf_size)
|
|
631 {
|
|
632 MpegAudioParseContext *s = s1->priv_data;
|
|
633 int len, ret;
|
|
634 uint32_t header;
|
|
635 const uint8_t *buf_ptr;
|
|
636
|
|
637 *poutbuf = NULL;
|
|
638 *poutbuf_size = 0;
|
|
639 buf_ptr = buf;
|
|
640 while (buf_size > 0) {
|
|
641 len = s->inbuf_ptr - s->inbuf;
|
|
642 if (s->frame_size == 0) {
|
|
643 /* special case for next header for first frame in free
|
|
644 format case (XXX: find a simpler method) */
|
|
645 if (s->free_format_next_header != 0) {
|
|
646 s->inbuf[0] = s->free_format_next_header >> 24;
|
|
647 s->inbuf[1] = s->free_format_next_header >> 16;
|
|
648 s->inbuf[2] = s->free_format_next_header >> 8;
|
|
649 s->inbuf[3] = s->free_format_next_header;
|
|
650 s->inbuf_ptr = s->inbuf + 4;
|
|
651 s->free_format_next_header = 0;
|
|
652 goto got_header;
|
|
653 }
|
|
654 /* no header seen : find one. We need at least MPA_HEADER_SIZE
|
|
655 bytes to parse it */
|
|
656 len = MPA_HEADER_SIZE - len;
|
|
657 if (len > buf_size)
|
|
658 len = buf_size;
|
|
659 if (len > 0) {
|
|
660 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
661 buf_ptr += len;
|
|
662 buf_size -= len;
|
|
663 s->inbuf_ptr += len;
|
|
664 }
|
|
665 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
|
|
666 got_header:
|
|
667 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
|
|
668 (s->inbuf[2] << 8) | s->inbuf[3];
|
|
669
|
|
670 ret = mpa_decode_header(avctx, header);
|
|
671 if (ret < 0) {
|
|
672 /* no sync found : move by one byte (inefficient, but simple!) */
|
|
673 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
|
|
674 s->inbuf_ptr--;
|
|
675 dprintf("skip %x\n", header);
|
|
676 /* reset free format frame size to give a chance
|
|
677 to get a new bitrate */
|
|
678 s->free_format_frame_size = 0;
|
|
679 } else {
|
|
680 s->frame_size = ret;
|
|
681 #if 0
|
|
682 /* free format: prepare to compute frame size */
|
|
683 if (decode_header(s, header) == 1) {
|
|
684 s->frame_size = -1;
|
|
685 }
|
|
686 #endif
|
|
687 }
|
|
688 }
|
|
689 } else
|
|
690 #if 0
|
|
691 if (s->frame_size == -1) {
|
|
692 /* free format : find next sync to compute frame size */
|
|
693 len = MPA_MAX_CODED_FRAME_SIZE - len;
|
|
694 if (len > buf_size)
|
|
695 len = buf_size;
|
|
696 if (len == 0) {
|
|
697 /* frame too long: resync */
|
|
698 s->frame_size = 0;
|
|
699 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
|
|
700 s->inbuf_ptr--;
|
|
701 } else {
|
|
702 uint8_t *p, *pend;
|
|
703 uint32_t header1;
|
|
704 int padding;
|
|
705
|
|
706 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
707 /* check for header */
|
|
708 p = s->inbuf_ptr - 3;
|
|
709 pend = s->inbuf_ptr + len - 4;
|
|
710 while (p <= pend) {
|
|
711 header = (p[0] << 24) | (p[1] << 16) |
|
|
712 (p[2] << 8) | p[3];
|
|
713 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
|
|
714 (s->inbuf[2] << 8) | s->inbuf[3];
|
|
715 /* check with high probability that we have a
|
|
716 valid header */
|
|
717 if ((header & SAME_HEADER_MASK) ==
|
|
718 (header1 & SAME_HEADER_MASK)) {
|
|
719 /* header found: update pointers */
|
|
720 len = (p + 4) - s->inbuf_ptr;
|
|
721 buf_ptr += len;
|
|
722 buf_size -= len;
|
|
723 s->inbuf_ptr = p;
|
|
724 /* compute frame size */
|
|
725 s->free_format_next_header = header;
|
|
726 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
|
|
727 padding = (header1 >> 9) & 1;
|
|
728 if (s->layer == 1)
|
|
729 s->free_format_frame_size -= padding * 4;
|
|
730 else
|
|
731 s->free_format_frame_size -= padding;
|
|
732 dprintf("free frame size=%d padding=%d\n",
|
|
733 s->free_format_frame_size, padding);
|
|
734 decode_header(s, header1);
|
|
735 goto next_data;
|
|
736 }
|
|
737 p++;
|
|
738 }
|
|
739 /* not found: simply increase pointers */
|
|
740 buf_ptr += len;
|
|
741 s->inbuf_ptr += len;
|
|
742 buf_size -= len;
|
|
743 }
|
|
744 } else
|
|
745 #endif
|
|
746 if (len < s->frame_size) {
|
|
747 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
|
|
748 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
|
|
749 len = s->frame_size - len;
|
|
750 if (len > buf_size)
|
|
751 len = buf_size;
|
|
752 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
753 buf_ptr += len;
|
|
754 s->inbuf_ptr += len;
|
|
755 buf_size -= len;
|
|
756 }
|
|
757 // next_data:
|
|
758 if (s->frame_size > 0 &&
|
|
759 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
|
|
760 *poutbuf = s->inbuf;
|
|
761 *poutbuf_size = s->inbuf_ptr - s->inbuf;
|
|
762 s->inbuf_ptr = s->inbuf;
|
|
763 s->frame_size = 0;
|
|
764 break;
|
|
765 }
|
|
766 }
|
|
767 return buf_ptr - buf;
|
|
768 }
|
|
769
|
|
770 #ifdef CONFIG_AC3
|
|
771 extern int a52_syncinfo (const uint8_t * buf, int * flags,
|
|
772 int * sample_rate, int * bit_rate);
|
|
773
|
|
774 typedef struct AC3ParseContext {
|
|
775 uint8_t inbuf[4096]; /* input buffer */
|
|
776 uint8_t *inbuf_ptr;
|
|
777 int frame_size;
|
|
778 int flags;
|
|
779 } AC3ParseContext;
|
|
780
|
|
781 #define AC3_HEADER_SIZE 7
|
|
782 #define A52_LFE 16
|
|
783
|
|
784 static int ac3_parse_init(AVCodecParserContext *s1)
|
|
785 {
|
|
786 AC3ParseContext *s = s1->priv_data;
|
|
787 s->inbuf_ptr = s->inbuf;
|
|
788 return 0;
|
|
789 }
|
|
790
|
|
791 static int ac3_parse(AVCodecParserContext *s1,
|
|
792 AVCodecContext *avctx,
|
|
793 uint8_t **poutbuf, int *poutbuf_size,
|
|
794 const uint8_t *buf, int buf_size)
|
|
795 {
|
|
796 AC3ParseContext *s = s1->priv_data;
|
|
797 const uint8_t *buf_ptr;
|
|
798 int len, sample_rate, bit_rate;
|
|
799 static const int ac3_channels[8] = {
|
|
800 2, 1, 2, 3, 3, 4, 4, 5
|
|
801 };
|
|
802
|
|
803 *poutbuf = NULL;
|
|
804 *poutbuf_size = 0;
|
|
805
|
|
806 buf_ptr = buf;
|
|
807 while (buf_size > 0) {
|
|
808 len = s->inbuf_ptr - s->inbuf;
|
|
809 if (s->frame_size == 0) {
|
|
810 /* no header seen : find one. We need at least 7 bytes to parse it */
|
|
811 len = AC3_HEADER_SIZE - len;
|
|
812 if (len > buf_size)
|
|
813 len = buf_size;
|
|
814 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
815 buf_ptr += len;
|
|
816 s->inbuf_ptr += len;
|
|
817 buf_size -= len;
|
|
818 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
|
|
819 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
|
|
820 if (len == 0) {
|
|
821 /* no sync found : move by one byte (inefficient, but simple!) */
|
|
822 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
|
|
823 s->inbuf_ptr--;
|
|
824 } else {
|
|
825 s->frame_size = len;
|
|
826 /* update codec info */
|
|
827 avctx->sample_rate = sample_rate;
|
|
828 avctx->channels = ac3_channels[s->flags & 7];
|
|
829 if (s->flags & A52_LFE)
|
|
830 avctx->channels++;
|
|
831 avctx->bit_rate = bit_rate;
|
|
832 avctx->frame_size = 6 * 256;
|
|
833 }
|
|
834 }
|
|
835 } else if (len < s->frame_size) {
|
|
836 len = s->frame_size - len;
|
|
837 if (len > buf_size)
|
|
838 len = buf_size;
|
|
839
|
|
840 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
841 buf_ptr += len;
|
|
842 s->inbuf_ptr += len;
|
|
843 buf_size -= len;
|
|
844 } else {
|
|
845 *poutbuf = s->inbuf;
|
|
846 *poutbuf_size = s->frame_size;
|
|
847 s->inbuf_ptr = s->inbuf;
|
|
848 s->frame_size = 0;
|
|
849 break;
|
|
850 }
|
|
851 }
|
|
852 return buf_ptr - buf;
|
|
853 }
|
|
854 #endif
|
|
855
|
|
856 AVCodecParser mpegvideo_parser = {
|
|
857 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
|
|
858 sizeof(ParseContext1),
|
|
859 NULL,
|
|
860 mpegvideo_parse,
|
|
861 mpegvideo_parse_close,
|
|
862 };
|
|
863
|
|
864 AVCodecParser mpeg4video_parser = {
|
|
865 { CODEC_ID_MPEG4 },
|
|
866 sizeof(ParseContext1),
|
|
867 mpeg4video_parse_init,
|
|
868 mpeg4video_parse,
|
|
869 mpegvideo_parse_close,
|
|
870 };
|
|
871
|
|
872 AVCodecParser h263_parser = {
|
|
873 { CODEC_ID_H263 },
|
|
874 sizeof(ParseContext1),
|
|
875 NULL,
|
|
876 h263_parse,
|
|
877 mpegvideo_parse_close,
|
|
878 };
|
|
879
|
|
880 AVCodecParser h264_parser = {
|
|
881 { CODEC_ID_H264 },
|
|
882 sizeof(ParseContext1),
|
|
883 NULL,
|
|
884 h264_parse,
|
|
885 mpegvideo_parse_close,
|
|
886 };
|
|
887
|
|
888 AVCodecParser mpegaudio_parser = {
|
|
889 { CODEC_ID_MP2, CODEC_ID_MP3 },
|
|
890 sizeof(MpegAudioParseContext),
|
|
891 mpegaudio_parse_init,
|
|
892 mpegaudio_parse,
|
|
893 NULL,
|
|
894 };
|
|
895
|
|
896 #ifdef CONFIG_AC3
|
|
897 AVCodecParser ac3_parser = {
|
|
898 { CODEC_ID_AC3 },
|
|
899 sizeof(AC3ParseContext),
|
|
900 ac3_parse_init,
|
|
901 ac3_parse,
|
|
902 NULL,
|
|
903 };
|
|
904 #endif
|