2
* Audio and Video frame extraction
3
* Copyright (c) 2003 Fabrice Bellard.
4
* Copyright (c) 2003 Michael Niedermayer.
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.
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.
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
21
#include "mpegvideo.h"
22
#include "mpegaudio.h"
24
AVCodecParser *av_first_parser = NULL;
26
void av_register_codec_parser(AVCodecParser *parser)
28
parser->next = av_first_parser;
29
av_first_parser = parser;
32
AVCodecParserContext *av_parser_init(int codec_id)
34
AVCodecParserContext *s;
35
AVCodecParser *parser;
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)
46
s = av_mallocz(sizeof(AVCodecParserContext));
50
s->priv_data = av_mallocz(parser->priv_data_size);
55
if (parser->parser_init) {
56
ret = parser->parser_init(s);
58
av_free(s->priv_data);
66
/* NOTE: buf_size == 0 is used to signal EOF so that the last frame
67
can be returned if necessary */
68
int av_parser_parse(AVCodecParserContext *s,
69
AVCodecContext *avctx,
70
uint8_t **poutbuf, int *poutbuf_size,
71
const uint8_t *buf, int buf_size,
72
int64_t pts, int64_t dts)
75
uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
78
/* padding is always necessary even if EOF, so we add it here */
79
memset(dummy_buf, 0, sizeof(dummy_buf));
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;
89
/* fill first PTS/DTS */
90
if (s->cur_offset == 0) {
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 */
100
/* fill the data for the current frame */
101
s->frame_offset = s->last_frame_offset;
102
s->pts = s->last_pts;
103
s->dts = s->last_dts;
105
/* offset of the next frame */
106
s->last_frame_offset = s->cur_offset + index;
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
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])
117
k = (k - 1) & (AV_PARSER_PTS_NB - 1);
119
s->last_pts = s->cur_frame_pts[k];
120
s->last_dts = s->cur_frame_dts[k];
124
s->cur_offset += index;
128
void av_parser_close(AVCodecParserContext *s)
130
if (s->parser->parser_close)
131
s->parser->parser_close(s);
132
av_free(s->priv_data);
136
/*****************************************************/
138
//#define END_NOT_FOUND (-100)
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
146
typedef struct ParseContext1{
148
/* XXX/FIXME PC1 vs. PC */
151
int progressive_sequence;
154
/* XXX: suppress that, needed by MPEG4 */
160
* combines the (truncated) bitstream to a complete frame
161
* @returns -1 if no complete frame could be created
163
int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
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]);
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++];
177
pc->last_index= pc->index;
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);
183
memcpy(&pc->buffer[pc->index], *buf, *buf_size);
184
pc->index += *buf_size;
189
pc->overread_index= pc->index + next;
191
/* append to buffer */
193
pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
195
memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
200
/* store overread bytes */
201
for(;next < 0; next++){
202
pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
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]);
216
static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
218
const uint8_t *buf_ptr;
219
unsigned int state=0xFFFFFFFF, v;
223
while (buf_ptr < buf_end) {
225
if (state == 0x000001) {
226
state = ((state << 8) | v) & 0xffffff;
230
state = ((state << 8) | v) & 0xffffff;
238
/* XXX: merge with libavcodec ? */
239
#define MPEG1_FRAME_RATE_BASE 1001
241
static const int frame_rate_tab[16] = {
253
// libmpeg3's "Unofficial economy rates": (10-13)
258
// random, just to avoid segfault !never encode these
263
static void mpegvideo_extract_headers(AVCodecParserContext *s,
264
AVCodecContext *avctx,
265
const uint8_t *buf, int buf_size)
267
ParseContext1 *pc = s->priv_data;
268
const uint8_t *buf_end;
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;
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;
281
case PICTURE_START_CODE:
282
if (bytes_left >= 2) {
283
s->pict_type = (buf[1] >> 3) & 7;
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;
293
avctx->codec_id = CODEC_ID_MPEG1VIDEO;
298
if (bytes_left >= 1) {
299
ext_type = (buf[0] >> 4);
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);
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);
313
avctx->codec_id = CODEC_ID_MPEG2VIDEO;
314
avctx->sub_id = 2; /* forces MPEG2 */
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);
323
/* check if we must repeat the frame */
324
if (repeat_first_field) {
325
if (pc->progressive_sequence) {
330
} else if (progressive_frame) {
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)
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)
358
ParseContext1 *pc1 = s->priv_data;
359
ParseContext *pc= &pc1->pc;
362
next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
364
if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
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);
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);
378
*poutbuf = (uint8_t *)buf;
379
*poutbuf_size = buf_size;
383
void ff_parse_close(AVCodecParserContext *s)
385
ParseContext *pc = s->priv_data;
390
static void parse1_close(AVCodecParserContext *s)
392
ParseContext1 *pc1 = s->priv_data;
394
av_free(pc1->pc.buffer);
398
/*************************/
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)
406
ParseContext1 *pc = s1->priv_data;
407
MpegEncContext *s = pc->enc;
408
GetBitContext gb1, *gb = &gb1;
412
s->current_picture_ptr = &s->current_picture;
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);
419
init_get_bits(gb, buf, 8 * buf_size);
420
ret = ff_mpeg4_decode_picture_header(s, gb);
422
avctx->width = s->width;
423
avctx->height = s->height;
425
pc->first_picture = 0;
429
static int mpeg4video_parse_init(AVCodecParserContext *s)
431
ParseContext1 *pc = s->priv_data;
433
pc->enc = av_mallocz(sizeof(MpegEncContext));
436
pc->first_picture = 1;
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)
445
ParseContext *pc = s->priv_data;
448
next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
450
if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
455
av_mpeg4_decode_header(s, avctx, buf, buf_size);
457
*poutbuf = (uint8_t *)buf;
458
*poutbuf_size = buf_size;
462
/*************************/
464
typedef struct MpegAudioParseContext {
465
uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
468
int free_format_frame_size;
469
int free_format_next_header;
470
} MpegAudioParseContext;
472
#define MPA_HEADER_SIZE 4
474
/* header + layer + bitrate + freq + lsf/mpeg25 */
475
#define SAME_HEADER_MASK \
476
(0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
478
static int mpegaudio_parse_init(AVCodecParserContext *s1)
480
MpegAudioParseContext *s = s1->priv_data;
481
s->inbuf_ptr = s->inbuf;
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)
490
MpegAudioParseContext *s = s1->priv_data;
493
const uint8_t *buf_ptr;
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;
512
/* no header seen : find one. We need at least MPA_HEADER_SIZE
514
len = MPA_HEADER_SIZE - len;
518
memcpy(s->inbuf_ptr, buf_ptr, len);
523
if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
525
header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
526
(s->inbuf[2] << 8) | s->inbuf[3];
528
ret = mpa_decode_header(avctx, header);
530
/* no sync found : move by one byte (inefficient, but simple!) */
531
memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
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;
540
/* free format: prepare to compute frame size */
541
if (decode_header(s, header) == 1) {
549
if (s->frame_size == -1) {
550
/* free format : find next sync to compute frame size */
551
len = MPA_MAX_CODED_FRAME_SIZE - len;
555
/* frame too long: resync */
557
memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
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;
569
header = (p[0] << 24) | (p[1] << 16) |
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
575
if ((header & SAME_HEADER_MASK) ==
576
(header1 & SAME_HEADER_MASK)) {
577
/* header found: update pointers */
578
len = (p + 4) - s->inbuf_ptr;
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;
587
s->free_format_frame_size -= padding * 4;
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);
597
/* not found: simply increase pointers */
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;
610
memcpy(s->inbuf_ptr, buf_ptr, len);
616
if (s->frame_size > 0 &&
617
(s->inbuf_ptr - s->inbuf) >= s->frame_size) {
619
*poutbuf_size = s->inbuf_ptr - s->inbuf;
620
s->inbuf_ptr = s->inbuf;
625
return buf_ptr - buf;
629
extern int a52_syncinfo (const uint8_t * buf, int * flags,
630
int * sample_rate, int * bit_rate);
632
typedef struct AC3ParseContext {
633
uint8_t inbuf[4096]; /* input buffer */
639
#define AC3_HEADER_SIZE 7
642
static int ac3_parse_init(AVCodecParserContext *s1)
644
AC3ParseContext *s = s1->priv_data;
645
s->inbuf_ptr = s->inbuf;
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)
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
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;
672
memcpy(s->inbuf_ptr, buf_ptr, len);
676
if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
677
len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
679
/* no sync found : move by one byte (inefficient, but simple!) */
680
memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
684
/* update codec info */
685
avctx->sample_rate = sample_rate;
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)
692
avctx->bit_rate = bit_rate;
693
avctx->frame_size = 6 * 256;
696
} else if (len < s->frame_size) {
697
len = s->frame_size - len;
701
memcpy(s->inbuf_ptr, buf_ptr, len);
707
*poutbuf_size = s->frame_size;
708
s->inbuf_ptr = s->inbuf;
713
return buf_ptr - buf;
717
AVCodecParser mpegvideo_parser = {
718
{ CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
719
sizeof(ParseContext1),
725
AVCodecParser mpeg4video_parser = {
727
sizeof(ParseContext1),
728
mpeg4video_parse_init,
733
AVCodecParser mpegaudio_parser = {
734
{ CODEC_ID_MP2, CODEC_ID_MP3 },
735
sizeof(MpegAudioParseContext),
736
mpegaudio_parse_init,
742
AVCodecParser ac3_parser = {
744
sizeof(AC3ParseContext),