Revision e4cb187d

View differences:

libavcodec/h263dec.c
306 306
 * finds the end of the current frame in the bitstream.
307 307
 * @return the position of the first byte of the next frame, or -1
308 308
 */
309
static int mpeg4_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
310
    ParseContext *pc= &s->parse_context;
309
int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
311 310
    int vop_found, i;
312 311
    uint32_t state;
313 312
    
......
326 325
        }
327 326
    }
328 327

  
329
    if(vop_found){    
330
      for(; i<buf_size; i++){
331
        state= (state<<8) | buf[i];
332
        if((state&0xFFFFFF00) == 0x100){
333
            pc->frame_start_found=0;
334
            pc->state=-1; 
335
            return i-3;
328
    if(vop_found){
329
        /* EOF considered as end of frame */
330
        if (buf_size == 0)
331
            return 0;
332
        for(; i<buf_size; i++){
333
            state= (state<<8) | buf[i];
334
            if((state&0xFFFFFF00) == 0x100){
335
                pc->frame_start_found=0;
336
                pc->state=-1; 
337
                return i-3;
338
            }
336 339
        }
337
      }
338 340
    }
339 341
    pc->frame_start_found= vop_found;
340 342
    pc->state= state;
341 343
    return END_NOT_FOUND;
342 344
}
343 345

  
344
static int h263_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
345
    ParseContext *pc= &s->parse_context;
346
static int h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
346 347
    int vop_found, i;
347 348
    uint32_t state;
348 349
    
......
377 378
    return END_NOT_FOUND;
378 379
}
379 380

  
381
static int h263_parse(AVCodecParserContext *s,
382
                           AVCodecContext *avctx,
383
                           uint8_t **poutbuf, int *poutbuf_size, 
384
                           const uint8_t *buf, int buf_size)
385
{
386
    ParseContext *pc = s->priv_data;
387
    int next;
388
    
389
    next= h263_find_frame_end(pc, buf, buf_size);
390

  
391
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
392
        *poutbuf = NULL;
393
        *poutbuf_size = 0;
394
        return buf_size;
395
    }
396

  
397
    *poutbuf = (uint8_t *)buf;
398
    *poutbuf_size = buf_size;
399
    return next;
400
}
401

  
380 402
int ff_h263_decode_frame(AVCodecContext *avctx, 
381 403
                             void *data, int *data_size,
382 404
                             uint8_t *buf, int buf_size)
......
414 436
        int next;
415 437
        
416 438
        if(s->codec_id==CODEC_ID_MPEG4){
417
            next= mpeg4_find_frame_end(s, buf, buf_size);
439
            next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
418 440
        }else if(s->codec_id==CODEC_ID_H263){
419
            next= h263_find_frame_end(s, buf, buf_size);
441
            next= h263_find_frame_end(&s->parse_context, buf, buf_size);
420 442
        }else{
421 443
            av_log(s->avctx, AV_LOG_ERROR, "this codec doesnt support truncated bitstreams\n");
422 444
            return -1;
423 445
        }
424 446
        
425
        if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
447
        if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
426 448
            return buf_size;
427 449
    }
428 450

  
......
843 865
    ff_h263_decode_frame,
844 866
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
845 867
};
868

  
869
AVCodecParser h263_parser = {
870
    { CODEC_ID_H263 },
871
    sizeof(ParseContext),
872
    NULL,
873
    h263_parse,
874
    ff_parse_close,
875
};
libavcodec/h264.c
5520 5520
 * finds the end of the current frame in the bitstream.
5521 5521
 * @return the position of the first byte of the next frame, or -1
5522 5522
 */
5523
static int find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
5524
    ParseContext *pc= &s->parse_context;
5523
static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
5525 5524
    int i;
5526 5525
    uint32_t state;
5527 5526
//printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
......
5544 5543
    return END_NOT_FOUND;
5545 5544
}
5546 5545

  
5546
static int h264_parse(AVCodecParserContext *s,
5547
                      AVCodecContext *avctx,
5548
                      uint8_t **poutbuf, int *poutbuf_size, 
5549
                      const uint8_t *buf, int buf_size)
5550
{
5551
    ParseContext *pc = s->priv_data;
5552
    int next;
5553
    
5554
    next= find_frame_end(pc, buf, buf_size);
5555

  
5556
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
5557
        *poutbuf = NULL;
5558
        *poutbuf_size = 0;
5559
        return buf_size;
5560
    }
5561

  
5562
    *poutbuf = (uint8_t *)buf;
5563
    *poutbuf_size = buf_size;
5564
    return next;
5565
}
5566

  
5547 5567
static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
5548 5568
    MpegEncContext * const s = &h->s;
5549 5569
    AVCodecContext * const avctx= s->avctx;
......
5701 5721
    }
5702 5722
    
5703 5723
    if(s->flags&CODEC_FLAG_TRUNCATED){
5704
        int next= find_frame_end(s, buf, buf_size);
5724
        int next= find_frame_end(&s->parse_context, buf, buf_size);
5705 5725
        
5706
        if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
5726
        if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
5707 5727
            return buf_size;
5708 5728
//printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
5709 5729
    }
......
5970 5990
    /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
5971 5991
};
5972 5992

  
5993
AVCodecParser h264_parser = {
5994
    { CODEC_ID_H264 },
5995
    sizeof(ParseContext),
5996
    NULL,
5997
    h264_parse,
5998
    ff_parse_close,
5999
};
6000

  
5973 6001
#include "svq3.c"
libavcodec/mpeg12.c
2715 2715
 * finds the end of the current frame in the bitstream.
2716 2716
 * @return the position of the first byte of the next frame, or -1
2717 2717
 */
2718
static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
2719
    ParseContext *pc= &s->parse_context;
2718
int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
2719
{
2720 2720
    int i;
2721 2721
    uint32_t state;
2722 2722
    
......
2735 2735
    }
2736 2736
    
2737 2737
    if(pc->frame_start_found){
2738
        /* EOF considered as end of frame */
2739
        if (buf_size == 0)
2740
            return 0;
2738 2741
        for(; i<buf_size; i++){
2739 2742
            state= (state<<8) | buf[i];
2740 2743
            if((state&0xFFFFFF00) == 0x100){
......
2775 2778
    }
2776 2779

  
2777 2780
    if(s2->flags&CODEC_FLAG_TRUNCATED){
2778
        int next= mpeg1_find_frame_end(s2, buf, buf_size);
2781
        int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
2779 2782
        
2780
        if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
2783
        if( ff_combine_frame(&s2->parse_context, next, &buf, &buf_size) < 0 )
2781 2784
            return buf_size;
2782 2785
    }    
2783 2786
    
libavcodec/mpegvideo.c
3698 3698

  
3699 3699
#endif //CONFIG_ENCODERS
3700 3700

  
3701
/**
3702
 * combines the (truncated) bitstream to a complete frame
3703
 * @returns -1 if no complete frame could be created
3704
 */
3705
int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
3706
    ParseContext *pc= &s->parse_context;
3707

  
3708
#if 0
3709
    if(pc->overread){
3710
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3711
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3712
    }
3713
#endif
3714

  
3715
    /* copy overreaded byes from last frame into buffer */
3716
    for(; pc->overread>0; pc->overread--){
3717
        pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
3718
    }
3719
    
3720
    pc->last_index= pc->index;
3721

  
3722
    /* copy into buffer end return */
3723
    if(next == END_NOT_FOUND){
3724
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3725

  
3726
        memcpy(&pc->buffer[pc->index], *buf, *buf_size);
3727
        pc->index += *buf_size;
3728
        return -1;
3729
    }
3730

  
3731
    *buf_size=
3732
    pc->overread_index= pc->index + next;
3733
    
3734
    /* append to buffer */
3735
    if(pc->index){
3736
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
3737

  
3738
        memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
3739
        pc->index = 0;
3740
        *buf= pc->buffer;
3741
    }
3742

  
3743
    /* store overread bytes */
3744
    for(;next < 0; next++){
3745
        pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
3746
        pc->overread++;
3747
    }
3748

  
3749
#if 0
3750
    if(pc->overread){
3751
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
3752
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
3753
    }
3754
#endif
3755

  
3756
    return 0;
3757
}
3758

  
3759 3701
void ff_mpeg_flush(AVCodecContext *avctx){
3760 3702
    int i;
3761 3703
    MpegEncContext *s = avctx->priv_data;
libavcodec/mpegvideo.h
749 749
void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h, 
750 750
                                    int src_x, int src_y, int w, int h);
751 751
#define END_NOT_FOUND -100
752
int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size);
752
int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size);
753
void ff_parse_close(AVCodecParserContext *s);
753 754
void ff_mpeg_flush(AVCodecContext *avctx);
754 755
void ff_print_debug_info(MpegEncContext *s, AVFrame *pict);
755 756
void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix);
libavcodec/parser.c
144 144
#define SLICE_MAX_START_CODE	0x000001af
145 145

  
146 146
typedef struct ParseContext1{
147
    uint8_t *buffer;
148
    int index;
149
    int last_index;
150
    int buffer_size;
151
    uint32_t state;             ///< contains the last few bytes in MSB order
152
    int frame_start_found;
153
    int overread;               ///< the number of bytes which where irreversibly read from the next frame
154
    int overread_index;         ///< the index into ParseContext1.buffer of the overreaded bytes
155

  
147
    ParseContext pc;
148
/* XXX/FIXME PC1 vs. PC */
156 149
    /* MPEG2 specific */
157 150
    int frame_rate;
158 151
    int progressive_sequence;
......
167 160
 * combines the (truncated) bitstream to a complete frame
168 161
 * @returns -1 if no complete frame could be created
169 162
 */
170
static int ff_combine_frame1(ParseContext1 *pc, int next, uint8_t **buf, int *buf_size)
163
int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
171 164
{
172 165
#if 0
173 166
    if(pc->overread){
......
220 213
    return 0;
221 214
}
222 215

  
223
/**
224
 * finds the end of the current frame in the bitstream.
225
 * @return the position of the first byte of the next frame, or -1
226
 */
227
static int mpeg1_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
228
{
229
    int i;
230
    uint32_t state;
231
    
232
    state= pc->state;
233
    
234
    i=0;
235
    if(!pc->frame_start_found){
236
        for(i=0; i<buf_size; i++){
237
            state= (state<<8) | buf[i];
238
            if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
239
                i++;
240
                pc->frame_start_found=1;
241
                break;
242
            }
243
        }
244
    }
245
    
246
    if(pc->frame_start_found){
247
        /* EOF considered as end of frame */
248
        if (buf_size == 0)
249
            return 0;
250
        for(; i<buf_size; i++){
251
            state= (state<<8) | buf[i];
252
            if((state&0xFFFFFF00) == 0x100){
253
                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
254
                    pc->frame_start_found=0;
255
                    pc->state=-1; 
256
                    return i-3;
257
                }
258
            }
259
        }
260
    }        
261
    pc->state= state;
262
    return END_NOT_FOUND;
263
}
264

  
265 216
static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
266 217
{
267 218
    const uint8_t *buf_ptr;
......
404 355
                           uint8_t **poutbuf, int *poutbuf_size, 
405 356
                           const uint8_t *buf, int buf_size)
406 357
{
407
    ParseContext1 *pc = s->priv_data;
358
    ParseContext1 *pc1 = s->priv_data;
359
    ParseContext *pc= &pc1->pc;
408 360
    int next;
409 361
    
410
    next= mpeg1_find_frame_end(pc, buf, buf_size);
362
    next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
411 363
    
412
    if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
364
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
413 365
        *poutbuf = NULL;
414 366
        *poutbuf_size = 0;
415 367
        return buf_size;
......
428 380
    return next;
429 381
}
430 382

  
431
static void mpegvideo_parse_close(AVCodecParserContext *s)
383
void ff_parse_close(AVCodecParserContext *s)
432 384
{
433
    ParseContext1 *pc = s->priv_data;
385
    ParseContext *pc = s->priv_data;
434 386

  
435 387
    av_free(pc->buffer);
436
    av_free(pc->enc);
437 388
}
438 389

  
439
/*************************/
440

  
441
/**
442
 * finds the end of the current frame in the bitstream.
443
 * @return the position of the first byte of the next frame, or -1
444
 */
445
static int mpeg4_find_frame_end(ParseContext1 *pc, 
446
                                const uint8_t *buf, int buf_size)
390
static void parse1_close(AVCodecParserContext *s)
447 391
{
448
    int vop_found, i;
449
    uint32_t state;
450
    
451
    vop_found= pc->frame_start_found;
452
    state= pc->state;
453
    
454
    i=0;
455
    if(!vop_found){
456
        for(i=0; i<buf_size; i++){
457
            state= (state<<8) | buf[i];
458
            if(state == 0x1B6){
459
                i++;
460
                vop_found=1;
461
                break;
462
            }
463
        }
464
    }
392
    ParseContext1 *pc1 = s->priv_data;
465 393

  
466
    if(vop_found){    
467
        /* EOF considered as end of frame */
468
        if (buf_size == 0)
469
            return 0;
470
        for(; i<buf_size; i++){
471
            state= (state<<8) | buf[i];
472
            if((state&0xFFFFFF00) == 0x100){
473
                pc->frame_start_found=0;
474
                pc->state=-1; 
475
                return i-3;
476
            }
477
        }
478
    }
479
    pc->frame_start_found= vop_found;
480
    pc->state= state;
481
    return END_NOT_FOUND;
394
    av_free(pc1->pc.buffer);
395
    av_free(pc1->enc);
482 396
}
483 397

  
398
/*************************/
399

  
484 400
/* used by parser */
485 401
/* XXX: make it use less memory */
486 402
static int av_mpeg4_decode_header(AVCodecParserContext *s1, 
......
526 442
                           uint8_t **poutbuf, int *poutbuf_size, 
527 443
                           const uint8_t *buf, int buf_size)
528 444
{
529
    ParseContext1 *pc = s->priv_data;
445
    ParseContext *pc = s->priv_data;
530 446
    int next;
531 447
    
532
    next= mpeg4_find_frame_end(pc, buf, buf_size);
448
    next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
533 449

  
534
    if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
450
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
535 451
        *poutbuf = NULL;
536 452
        *poutbuf_size = 0;
537 453
        return buf_size;
......
545 461

  
546 462
/*************************/
547 463

  
548
static int h263_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
549
{
550
    int vop_found, i;
551
    uint32_t state;
552
    
553
    vop_found= pc->frame_start_found;
554
    state= pc->state;
555
    
556
    i=0;
557
    if(!vop_found){
558
        for(i=0; i<buf_size; i++){
559
            state= (state<<8) | buf[i];
560
            if(state>>(32-22) == 0x20){
561
                i++;
562
                vop_found=1;
563
                break;
564
            }
565
        }
566
    }
567

  
568
    if(vop_found){    
569
      for(; i<buf_size; i++){
570
        state= (state<<8) | buf[i];
571
        if(state>>(32-22) == 0x20){
572
            pc->frame_start_found=0;
573
            pc->state=-1; 
574
            return i-3;
575
        }
576
      }
577
    }
578
    pc->frame_start_found= vop_found;
579
    pc->state= state;
580
    
581
    return END_NOT_FOUND;
582
}
583

  
584
static int h263_parse(AVCodecParserContext *s,
585
                           AVCodecContext *avctx,
586
                           uint8_t **poutbuf, int *poutbuf_size, 
587
                           const uint8_t *buf, int buf_size)
588
{
589
    ParseContext1 *pc = s->priv_data;
590
    int next;
591
    
592
    next= h263_find_frame_end(pc, buf, buf_size);
593

  
594
    if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
595
        *poutbuf = NULL;
596
        *poutbuf_size = 0;
597
        return buf_size;
598
    }
599

  
600
    *poutbuf = (uint8_t *)buf;
601
    *poutbuf_size = buf_size;
602
    return next;
603
}
604

  
605
/*************************/
606

  
607
/**
608
 * finds the end of the current frame in the bitstream.
609
 * @return the position of the first byte of the next frame, or -1
610
 */
611
static int h264_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
612
{
613
    int i;
614
    uint32_t state;
615
//printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
616
//    mb_addr= pc->mb_addr - 1;
617
    state= pc->state;
618
    //FIXME this will fail with slices
619
    for(i=0; i<buf_size; i++){
620
        state= (state<<8) | buf[i];
621
        if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
622
            if(pc->frame_start_found){
623
                pc->state=-1; 
624
                pc->frame_start_found= 0;
625
                return i-3;
626
            }
627
            pc->frame_start_found= 1;
628
        }
629
    }
630
    
631
    pc->state= state;
632
    return END_NOT_FOUND;
633
}
634

  
635
static int h264_parse(AVCodecParserContext *s,
636
                      AVCodecContext *avctx,
637
                      uint8_t **poutbuf, int *poutbuf_size, 
638
                      const uint8_t *buf, int buf_size)
639
{
640
    ParseContext1 *pc = s->priv_data;
641
    int next;
642
    
643
    next= h264_find_frame_end(pc, buf, buf_size);
644

  
645
    if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
646
        *poutbuf = NULL;
647
        *poutbuf_size = 0;
648
        return buf_size;
649
    }
650

  
651
    *poutbuf = (uint8_t *)buf;
652
    *poutbuf_size = buf_size;
653
    return next;
654
}
655

  
656
/*************************/
657

  
658 464
typedef struct MpegAudioParseContext {
659 465
    uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];	/* input buffer */
660 466
    uint8_t *inbuf_ptr;
......
913 719
    sizeof(ParseContext1),
914 720
    NULL,
915 721
    mpegvideo_parse,
916
    mpegvideo_parse_close,
722
    parse1_close,
917 723
};
918 724

  
919 725
AVCodecParser mpeg4video_parser = {
......
921 727
    sizeof(ParseContext1),
922 728
    mpeg4video_parse_init,
923 729
    mpeg4video_parse,
924
    mpegvideo_parse_close,
925
};
926

  
927
AVCodecParser h263_parser = {
928
    { CODEC_ID_H263 },
929
    sizeof(ParseContext1),
930
    NULL,
931
    h263_parse,
932
    mpegvideo_parse_close,
933
};
934

  
935
AVCodecParser h264_parser = {
936
    { CODEC_ID_H264 },
937
    sizeof(ParseContext1),
938
    NULL,
939
    h264_parse,
940
    mpegvideo_parse_close,
730
    parse1_close,
941 731
};
942 732

  
943 733
AVCodecParser mpegaudio_parser = {

Also available in: Unified diff