00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "avcodec.h"
00021 #include "mpegvideo.h"
00022 #include "mpegaudio.h"
00023
00024 AVCodecParser *av_first_parser = NULL;
00025
00026 void av_register_codec_parser(AVCodecParser *parser)
00027 {
00028 parser->next = av_first_parser;
00029 av_first_parser = parser;
00030 }
00031
00032 AVCodecParserContext *av_parser_init(int codec_id)
00033 {
00034 AVCodecParserContext *s;
00035 AVCodecParser *parser;
00036 int ret;
00037
00038 if(codec_id == CODEC_ID_NONE)
00039 return NULL;
00040
00041 for(parser = av_first_parser; parser != NULL; parser = parser->next) {
00042 if (parser->codec_ids[0] == codec_id ||
00043 parser->codec_ids[1] == codec_id ||
00044 parser->codec_ids[2] == codec_id ||
00045 parser->codec_ids[3] == codec_id ||
00046 parser->codec_ids[4] == codec_id)
00047 goto found;
00048 }
00049 return NULL;
00050 found:
00051 s = av_mallocz(sizeof(AVCodecParserContext));
00052 if (!s)
00053 return NULL;
00054 s->parser = parser;
00055 s->priv_data = av_mallocz(parser->priv_data_size);
00056 if (!s->priv_data) {
00057 av_free(s);
00058 return NULL;
00059 }
00060 if (parser->parser_init) {
00061 ret = parser->parser_init(s);
00062 if (ret != 0) {
00063 av_free(s->priv_data);
00064 av_free(s);
00065 return NULL;
00066 }
00067 }
00068 s->fetch_timestamp=1;
00069 return s;
00070 }
00071
00072
00073
00074 int av_parser_parse(AVCodecParserContext *s,
00075 AVCodecContext *avctx,
00076 uint8_t **poutbuf, int *poutbuf_size,
00077 const uint8_t *buf, int buf_size,
00078 int64_t pts, int64_t dts)
00079 {
00080 int index, i, k;
00081 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00082
00083 if (buf_size == 0) {
00084
00085 memset(dummy_buf, 0, sizeof(dummy_buf));
00086 buf = dummy_buf;
00087 } else {
00088
00089 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00090 s->cur_frame_start_index = k;
00091 s->cur_frame_offset[k] = s->cur_offset;
00092 s->cur_frame_pts[k] = pts;
00093 s->cur_frame_dts[k] = dts;
00094
00095
00096 if (s->fetch_timestamp){
00097 s->fetch_timestamp=0;
00098 s->last_pts = pts;
00099 s->last_dts = dts;
00100 s->cur_frame_pts[k] =
00101 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
00102 }
00103 }
00104
00105
00106 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
00107
00108
00109 if (*poutbuf_size) {
00110
00111 s->frame_offset = s->last_frame_offset;
00112 s->pts = s->last_pts;
00113 s->dts = s->last_dts;
00114
00115
00116 s->last_frame_offset = s->cur_offset + index;
00117
00118
00119
00120
00121
00122
00123 k = s->cur_frame_start_index;
00124 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00125 if (s->last_frame_offset >= s->cur_frame_offset[k])
00126 break;
00127 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
00128 }
00129
00130 s->last_pts = s->cur_frame_pts[k];
00131 s->last_dts = s->cur_frame_dts[k];
00132
00133
00134
00135 if(index == buf_size){
00136 s->fetch_timestamp=1;
00137 }
00138 }
00139 if (index < 0)
00140 index = 0;
00141 s->cur_offset += index;
00142 return index;
00143 }
00144
00149 int av_parser_change(AVCodecParserContext *s,
00150 AVCodecContext *avctx,
00151 uint8_t **poutbuf, int *poutbuf_size,
00152 const uint8_t *buf, int buf_size, int keyframe){
00153
00154 if(s && s->parser->split){
00155 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
00156 int i= s->parser->split(avctx, buf, buf_size);
00157 buf += i;
00158 buf_size -= i;
00159 }
00160 }
00161
00162 *poutbuf= buf;
00163 *poutbuf_size= buf_size;
00164 if(avctx->extradata){
00165 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
00166
00167 ){
00168 int size= buf_size + avctx->extradata_size;
00169 *poutbuf_size= size;
00170 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00171
00172 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
00173 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00174 return 1;
00175 }
00176 }
00177
00178 return 0;
00179 }
00180
00181 void av_parser_close(AVCodecParserContext *s)
00182 {
00183 if (s->parser->parser_close)
00184 s->parser->parser_close(s);
00185 av_free(s->priv_data);
00186 av_free(s);
00187 }
00188
00189
00190
00191
00192
00193 #define PICTURE_START_CODE 0x00000100
00194 #define SEQ_START_CODE 0x000001b3
00195 #define EXT_START_CODE 0x000001b5
00196 #define SLICE_MIN_START_CODE 0x00000101
00197 #define SLICE_MAX_START_CODE 0x000001af
00198
00199 typedef struct ParseContext1{
00200 ParseContext pc;
00201
00202
00203 int frame_rate;
00204 int progressive_sequence;
00205 int width, height;
00206
00207
00208 MpegEncContext *enc;
00209 int first_picture;
00210 } ParseContext1;
00211
00216 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
00217 {
00218 #if 0
00219 if(pc->overread){
00220 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00221 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00222 }
00223 #endif
00224
00225
00226 for(; pc->overread>0; pc->overread--){
00227 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00228 }
00229
00230
00231 if(!*buf_size && next == END_NOT_FOUND){
00232 next= 0;
00233 }
00234
00235 pc->last_index= pc->index;
00236
00237
00238 if(next == END_NOT_FOUND){
00239 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00240
00241 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
00242 pc->index += *buf_size;
00243 return -1;
00244 }
00245
00246 *buf_size=
00247 pc->overread_index= pc->index + next;
00248
00249
00250 if(pc->index){
00251 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00252
00253 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
00254 pc->index = 0;
00255 *buf= pc->buffer;
00256 }
00257
00258
00259 for(;next < 0; next++){
00260 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00261 pc->overread++;
00262 }
00263
00264 #if 0
00265 if(pc->overread){
00266 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00267 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00268 }
00269 #endif
00270
00271 return 0;
00272 }
00273
00274 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
00275 {
00276 const uint8_t *buf_ptr;
00277 unsigned int state=0xFFFFFFFF, v;
00278 int val;
00279
00280 buf_ptr = *pbuf_ptr;
00281 while (buf_ptr < buf_end) {
00282 v = *buf_ptr++;
00283 if (state == 0x000001) {
00284 state = ((state << 8) | v) & 0xffffff;
00285 val = state;
00286 goto found;
00287 }
00288 state = ((state << 8) | v) & 0xffffff;
00289 }
00290 val = -1;
00291 found:
00292 *pbuf_ptr = buf_ptr;
00293 return val;
00294 }
00295
00296
00297 #define MPEG1_FRAME_RATE_BASE 1001
00298
00299 static const int frame_rate_tab[16] = {
00300 0,
00301 24000,
00302 24024,
00303 25025,
00304 30000,
00305 30030,
00306 50050,
00307 60000,
00308 60060,
00309
00310 15015,
00311
00312 5005,
00313 10010,
00314 12012,
00315 15015,
00316
00317 25025,
00318 25025,
00319 };
00320
00321
00322 static void mpegvideo_extract_headers(AVCodecParserContext *s,
00323 AVCodecContext *avctx,
00324 const uint8_t *buf, int buf_size)
00325 {
00326 ParseContext1 *pc = s->priv_data;
00327 const uint8_t *buf_end;
00328 int32_t start_code;
00329 int frame_rate_index, ext_type, bytes_left;
00330 int frame_rate_ext_n, frame_rate_ext_d;
00331 int picture_structure, top_field_first, repeat_first_field, progressive_frame;
00332 int horiz_size_ext, vert_size_ext, bit_rate_ext;
00333
00334 s->repeat_pict = 0;
00335 buf_end = buf + buf_size;
00336 while (buf < buf_end) {
00337 start_code = find_start_code(&buf, buf_end);
00338 bytes_left = buf_end - buf;
00339 switch(start_code) {
00340 case PICTURE_START_CODE:
00341 if (bytes_left >= 2) {
00342 s->pict_type = (buf[1] >> 3) & 7;
00343 }
00344 break;
00345 case SEQ_START_CODE:
00346 if (bytes_left >= 7) {
00347 pc->width = (buf[0] << 4) | (buf[1] >> 4);
00348 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
00349 avcodec_set_dimensions(avctx, pc->width, pc->height);
00350 frame_rate_index = buf[3] & 0xf;
00351 pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index];
00352 avctx->time_base.num = MPEG1_FRAME_RATE_BASE;
00353 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
00354 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
00355 avctx->sub_id = 1;
00356 }
00357 break;
00358 case EXT_START_CODE:
00359 if (bytes_left >= 1) {
00360 ext_type = (buf[0] >> 4);
00361 switch(ext_type) {
00362 case 0x1:
00363 if (bytes_left >= 6) {
00364 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
00365 vert_size_ext = (buf[2] >> 5) & 3;
00366 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
00367 frame_rate_ext_n = (buf[5] >> 5) & 3;
00368 frame_rate_ext_d = (buf[5] & 0x1f);
00369 pc->progressive_sequence = buf[1] & (1 << 3);
00370 avctx->has_b_frames= !(buf[5] >> 7);
00371
00372 pc->width |=(horiz_size_ext << 12);
00373 pc->height |=( vert_size_ext << 12);
00374 avctx->bit_rate += (bit_rate_ext << 18) * 400;
00375 avcodec_set_dimensions(avctx, pc->width, pc->height);
00376 avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1);
00377 avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
00378 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
00379 avctx->sub_id = 2;
00380 }
00381 break;
00382 case 0x8:
00383 if (bytes_left >= 5) {
00384 picture_structure = buf[2]&3;
00385 top_field_first = buf[3] & (1 << 7);
00386 repeat_first_field = buf[3] & (1 << 1);
00387 progressive_frame = buf[4] & (1 << 7);
00388
00389
00390 if (repeat_first_field) {
00391 if (pc->progressive_sequence) {
00392 if (top_field_first)
00393 s->repeat_pict = 4;
00394 else
00395 s->repeat_pict = 2;
00396 } else if (progressive_frame) {
00397 s->repeat_pict = 1;
00398 }
00399 }
00400
00401
00402
00403 if(picture_structure != 3)
00404 s->repeat_pict = -1;
00405 }
00406 break;
00407 }
00408 }
00409 break;
00410 case -1:
00411 goto the_end;
00412 default:
00413
00414
00415 if (start_code >= SLICE_MIN_START_CODE &&
00416 start_code <= SLICE_MAX_START_CODE)
00417 goto the_end;
00418 break;
00419 }
00420 }
00421 the_end: ;
00422 }
00423
00424 static int mpegvideo_parse(AVCodecParserContext *s,
00425 AVCodecContext *avctx,
00426 uint8_t **poutbuf, int *poutbuf_size,
00427 const uint8_t *buf, int buf_size)
00428 {
00429 ParseContext1 *pc1 = s->priv_data;
00430 ParseContext *pc= &pc1->pc;
00431 int next;
00432
00433 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
00434
00435 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
00436 *poutbuf = NULL;
00437 *poutbuf_size = 0;
00438 return buf_size;
00439 }
00440
00441
00442
00443 mpegvideo_extract_headers(s, avctx, buf, buf_size);
00444 #if 0
00445 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
00446 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
00447 #endif
00448
00449 *poutbuf = (uint8_t *)buf;
00450 *poutbuf_size = buf_size;
00451 return next;
00452 }
00453
00454 static int mpegvideo_split(AVCodecContext *avctx,
00455 const uint8_t *buf, int buf_size)
00456 {
00457 int i;
00458 uint32_t state= -1;
00459
00460 for(i=0; i<buf_size; i++){
00461 state= (state<<8) | buf[i];
00462 if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
00463 return i-3;
00464 }
00465 return 0;
00466 }
00467
00468 void ff_parse_close(AVCodecParserContext *s)
00469 {
00470 ParseContext *pc = s->priv_data;
00471
00472 av_free(pc->buffer);
00473 }
00474
00475 static void parse1_close(AVCodecParserContext *s)
00476 {
00477 ParseContext1 *pc1 = s->priv_data;
00478
00479 av_free(pc1->pc.buffer);
00480 av_free(pc1->enc);
00481 }
00482
00483
00484
00485
00486
00487 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
00488 AVCodecContext *avctx,
00489 const uint8_t *buf, int buf_size)
00490 {
00491 ParseContext1 *pc = s1->priv_data;
00492 MpegEncContext *s = pc->enc;
00493 GetBitContext gb1, *gb = &gb1;
00494 int ret;
00495
00496 s->avctx = avctx;
00497 s->current_picture_ptr = &s->current_picture;
00498
00499 if (avctx->extradata_size && pc->first_picture){
00500 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
00501 ret = ff_mpeg4_decode_picture_header(s, gb);
00502 }
00503
00504 init_get_bits(gb, buf, 8 * buf_size);
00505 ret = ff_mpeg4_decode_picture_header(s, gb);
00506 if (s->width) {
00507 avcodec_set_dimensions(avctx, s->width, s->height);
00508 }
00509 pc->first_picture = 0;
00510 return ret;
00511 }
00512
00513 static int mpeg4video_parse_init(AVCodecParserContext *s)
00514 {
00515 ParseContext1 *pc = s->priv_data;
00516
00517 pc->enc = av_mallocz(sizeof(MpegEncContext));
00518 if (!pc->enc)
00519 return -1;
00520 pc->first_picture = 1;
00521 return 0;
00522 }
00523
00524 static int mpeg4video_parse(AVCodecParserContext *s,
00525 AVCodecContext *avctx,
00526 uint8_t **poutbuf, int *poutbuf_size,
00527 const uint8_t *buf, int buf_size)
00528 {
00529 ParseContext *pc = s->priv_data;
00530 int next;
00531
00532 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
00533
00534 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
00535 *poutbuf = NULL;
00536 *poutbuf_size = 0;
00537 return buf_size;
00538 }
00539 av_mpeg4_decode_header(s, avctx, buf, buf_size);
00540
00541 *poutbuf = (uint8_t *)buf;
00542 *poutbuf_size = buf_size;
00543 return next;
00544 }
00545
00546 static int mpeg4video_split(AVCodecContext *avctx,
00547 const uint8_t *buf, int buf_size)
00548 {
00549 int i;
00550 uint32_t state= -1;
00551
00552 for(i=0; i<buf_size; i++){
00553 state= (state<<8) | buf[i];
00554 if(state == 0x1B3 || state == 0x1B6)
00555 return i-3;
00556 }
00557 return 0;
00558 }
00559
00560
00561
00562 typedef struct MpegAudioParseContext {
00563 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];
00564 uint8_t *inbuf_ptr;
00565 int frame_size;
00566 int free_format_frame_size;
00567 int free_format_next_header;
00568 uint32_t header;
00569 int header_count;
00570 } MpegAudioParseContext;
00571
00572 #define MPA_HEADER_SIZE 4
00573
00574
00575 #undef SAME_HEADER_MASK
00576 #define SAME_HEADER_MASK \
00577 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
00578
00579 static int mpegaudio_parse_init(AVCodecParserContext *s1)
00580 {
00581 MpegAudioParseContext *s = s1->priv_data;
00582 s->inbuf_ptr = s->inbuf;
00583 return 0;
00584 }
00585
00586 static int mpegaudio_parse(AVCodecParserContext *s1,
00587 AVCodecContext *avctx,
00588 uint8_t **poutbuf, int *poutbuf_size,
00589 const uint8_t *buf, int buf_size)
00590 {
00591 MpegAudioParseContext *s = s1->priv_data;
00592 int len, ret, sr;
00593 uint32_t header;
00594 const uint8_t *buf_ptr;
00595
00596 *poutbuf = NULL;
00597 *poutbuf_size = 0;
00598 buf_ptr = buf;
00599 while (buf_size > 0) {
00600 len = s->inbuf_ptr - s->inbuf;
00601 if (s->frame_size == 0) {
00602
00603
00604 if (s->free_format_next_header != 0) {
00605 s->inbuf[0] = s->free_format_next_header >> 24;
00606 s->inbuf[1] = s->free_format_next_header >> 16;
00607 s->inbuf[2] = s->free_format_next_header >> 8;
00608 s->inbuf[3] = s->free_format_next_header;
00609 s->inbuf_ptr = s->inbuf + 4;
00610 s->free_format_next_header = 0;
00611 goto got_header;
00612 }
00613
00614
00615 len = MPA_HEADER_SIZE - len;
00616 if (len > buf_size)
00617 len = buf_size;
00618 if (len > 0) {
00619 memcpy(s->inbuf_ptr, buf_ptr, len);
00620 buf_ptr += len;
00621 buf_size -= len;
00622 s->inbuf_ptr += len;
00623 }
00624 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
00625 got_header:
00626 sr= avctx->sample_rate;
00627 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
00628 (s->inbuf[2] << 8) | s->inbuf[3];
00629
00630 ret = mpa_decode_header(avctx, header);
00631 if (ret < 0) {
00632 s->header_count= -2;
00633
00634 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
00635 s->inbuf_ptr--;
00636 dprintf("skip %x\n", header);
00637
00638
00639 s->free_format_frame_size = 0;
00640 } else {
00641 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
00642 s->header_count= -3;
00643 s->header= header;
00644 s->header_count++;
00645 s->frame_size = ret;
00646
00647 #if 0
00648
00649 if (decode_header(s, header) == 1) {
00650 s->frame_size = -1;
00651 }
00652 #endif
00653 }
00654 if(s->header_count <= 0)
00655 avctx->sample_rate= sr;
00656 }
00657 } else
00658 #if 0
00659 if (s->frame_size == -1) {
00660
00661 len = MPA_MAX_CODED_FRAME_SIZE - len;
00662 if (len > buf_size)
00663 len = buf_size;
00664 if (len == 0) {
00665
00666 s->frame_size = 0;
00667 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
00668 s->inbuf_ptr--;
00669 } else {
00670 uint8_t *p, *pend;
00671 uint32_t header1;
00672 int padding;
00673
00674 memcpy(s->inbuf_ptr, buf_ptr, len);
00675
00676 p = s->inbuf_ptr - 3;
00677 pend = s->inbuf_ptr + len - 4;
00678 while (p <= pend) {
00679 header = (p[0] << 24) | (p[1] << 16) |
00680 (p[2] << 8) | p[3];
00681 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
00682 (s->inbuf[2] << 8) | s->inbuf[3];
00683
00684
00685 if ((header & SAME_HEADER_MASK) ==
00686 (header1 & SAME_HEADER_MASK)) {
00687
00688 len = (p + 4) - s->inbuf_ptr;
00689 buf_ptr += len;
00690 buf_size -= len;
00691 s->inbuf_ptr = p;
00692
00693 s->free_format_next_header = header;
00694 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
00695 padding = (header1 >> 9) & 1;
00696 if (s->layer == 1)
00697 s->free_format_frame_size -= padding * 4;
00698 else
00699 s->free_format_frame_size -= padding;
00700 dprintf("free frame size=%d padding=%d\n",
00701 s->free_format_frame_size, padding);
00702 decode_header(s, header1);
00703 goto next_data;
00704 }
00705 p++;
00706 }
00707
00708 buf_ptr += len;
00709 s->inbuf_ptr += len;
00710 buf_size -= len;
00711 }
00712 } else
00713 #endif
00714 if (len < s->frame_size) {
00715 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
00716 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
00717 len = s->frame_size - len;
00718 if (len > buf_size)
00719 len = buf_size;
00720 memcpy(s->inbuf_ptr, buf_ptr, len);
00721 buf_ptr += len;
00722 s->inbuf_ptr += len;
00723 buf_size -= len;
00724 }
00725
00726 if (s->frame_size > 0 &&
00727 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
00728 if(s->header_count > 0){
00729 *poutbuf = s->inbuf;
00730 *poutbuf_size = s->inbuf_ptr - s->inbuf;
00731 }
00732 s->inbuf_ptr = s->inbuf;
00733 s->frame_size = 0;
00734 break;
00735 }
00736 }
00737 return buf_ptr - buf;
00738 }
00739
00740 #ifdef CONFIG_AC3
00741 extern int a52_syncinfo (const uint8_t * buf, int * flags,
00742 int * sample_rate, int * bit_rate);
00743
00744 typedef struct AC3ParseContext {
00745 uint8_t inbuf[4096];
00746 uint8_t *inbuf_ptr;
00747 int frame_size;
00748 int flags;
00749 } AC3ParseContext;
00750
00751 #define AC3_HEADER_SIZE 7
00752 #define A52_LFE 16
00753
00754 static int ac3_parse_init(AVCodecParserContext *s1)
00755 {
00756 AC3ParseContext *s = s1->priv_data;
00757 s->inbuf_ptr = s->inbuf;
00758 return 0;
00759 }
00760
00761 static int ac3_parse(AVCodecParserContext *s1,
00762 AVCodecContext *avctx,
00763 uint8_t **poutbuf, int *poutbuf_size,
00764 const uint8_t *buf, int buf_size)
00765 {
00766 AC3ParseContext *s = s1->priv_data;
00767 const uint8_t *buf_ptr;
00768 int len, sample_rate, bit_rate;
00769 static const int ac3_channels[8] = {
00770 2, 1, 2, 3, 3, 4, 4, 5
00771 };
00772
00773 *poutbuf = NULL;
00774 *poutbuf_size = 0;
00775
00776 buf_ptr = buf;
00777 while (buf_size > 0) {
00778 len = s->inbuf_ptr - s->inbuf;
00779 if (s->frame_size == 0) {
00780
00781 len = AC3_HEADER_SIZE - len;
00782 if (len > buf_size)
00783 len = buf_size;
00784 memcpy(s->inbuf_ptr, buf_ptr, len);
00785 buf_ptr += len;
00786 s->inbuf_ptr += len;
00787 buf_size -= len;
00788 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
00789 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
00790 if (len == 0) {
00791
00792 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
00793 s->inbuf_ptr--;
00794 } else {
00795 s->frame_size = len;
00796
00797 avctx->sample_rate = sample_rate;
00798
00799 if(avctx->channels!=1 && avctx->channels!=2){
00800 avctx->channels = ac3_channels[s->flags & 7];
00801 if (s->flags & A52_LFE)
00802 avctx->channels++;
00803 }
00804 avctx->bit_rate = bit_rate;
00805 avctx->frame_size = 6 * 256;
00806 }
00807 }
00808 } else if (len < s->frame_size) {
00809 len = s->frame_size - len;
00810 if (len > buf_size)
00811 len = buf_size;
00812
00813 memcpy(s->inbuf_ptr, buf_ptr, len);
00814 buf_ptr += len;
00815 s->inbuf_ptr += len;
00816 buf_size -= len;
00817 } else {
00818 *poutbuf = s->inbuf;
00819 *poutbuf_size = s->frame_size;
00820 s->inbuf_ptr = s->inbuf;
00821 s->frame_size = 0;
00822 break;
00823 }
00824 }
00825 return buf_ptr - buf;
00826 }
00827 #endif
00828
00829 AVCodecParser mpegvideo_parser = {
00830 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
00831 sizeof(ParseContext1),
00832 NULL,
00833 mpegvideo_parse,
00834 parse1_close,
00835 mpegvideo_split,
00836 };
00837
00838 AVCodecParser mpeg4video_parser = {
00839 { CODEC_ID_MPEG4 },
00840 sizeof(ParseContext1),
00841 mpeg4video_parse_init,
00842 mpeg4video_parse,
00843 parse1_close,
00844 mpeg4video_split,
00845 };
00846
00847 AVCodecParser mpegaudio_parser = {
00848 { CODEC_ID_MP2, CODEC_ID_MP3 },
00849 sizeof(MpegAudioParseContext),
00850 mpegaudio_parse_init,
00851 mpegaudio_parse,
00852 NULL,
00853 };
00854
00855 #ifdef CONFIG_AC3
00856 AVCodecParser ac3_parser = {
00857 { CODEC_ID_AC3 },
00858 sizeof(AC3ParseContext),
00859 ac3_parse_init,
00860 ac3_parse,
00861 NULL,
00862 };
00863 #endif