comparison h264.c @ 11248:926dde10e50d libavcodec

Move extradata reading code into codec init instead of doing it in read frame.
author michael
date Tue, 23 Feb 2010 01:07:39 +0000
parents 9df548187a80
children 6f17564ec228
comparison
equal deleted inserted replaced
11247:b48dd9213016 11248:926dde10e50d
819 return 0; 819 return 0;
820 fail: 820 fail:
821 return -1; // free_tables will clean up for us 821 return -1; // free_tables will clean up for us
822 } 822 }
823 823
824 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
825
824 static av_cold void common_init(H264Context *h){ 826 static av_cold void common_init(H264Context *h){
825 MpegEncContext * const s = &h->s; 827 MpegEncContext * const s = &h->s;
826 828
827 s->width = s->avctx->width; 829 s->width = s->avctx->width;
828 s->height = s->avctx->height; 830 s->height = s->avctx->height;
859 s->low_delay= 1; 861 s->low_delay= 1;
860 862
861 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; 863 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
862 864
863 ff_h264_decode_init_vlc(); 865 ff_h264_decode_init_vlc();
864
865 if(avctx->extradata_size > 0 && avctx->extradata &&
866 *(char *)avctx->extradata == 1){
867 h->is_avc = 1;
868 h->got_avcC = 0;
869 } else {
870 h->is_avc = 0;
871 }
872 866
873 h->thread_context[0] = h; 867 h->thread_context[0] = h;
874 h->outputed_poc = INT_MIN; 868 h->outputed_poc = INT_MIN;
875 h->prev_poc_msb= 1<<16; 869 h->prev_poc_msb= 1<<16;
876 h->x264_build = -1; 870 h->x264_build = -1;
879 if(avctx->ticks_per_frame == 1){ 873 if(avctx->ticks_per_frame == 1){
880 s->avctx->time_base.den *=2; 874 s->avctx->time_base.den *=2;
881 } 875 }
882 avctx->ticks_per_frame = 2; 876 avctx->ticks_per_frame = 2;
883 } 877 }
878
879 if(avctx->extradata_size > 0 && avctx->extradata && *(char *)avctx->extradata == 1){
880 int i, cnt, nalsize;
881 unsigned char *p = avctx->extradata;
882
883 h->is_avc = 1;
884
885 if(avctx->extradata_size < 7) {
886 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
887 return -1;
888 }
889 /* sps and pps in the avcC always have length coded with 2 bytes,
890 so put a fake nal_length_size = 2 while parsing them */
891 h->nal_length_size = 2;
892 // Decode sps from avcC
893 cnt = *(p+5) & 0x1f; // Number of sps
894 p += 6;
895 for (i = 0; i < cnt; i++) {
896 nalsize = AV_RB16(p) + 2;
897 if(decode_nal_units(h, p, nalsize) < 0) {
898 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
899 return -1;
900 }
901 p += nalsize;
902 }
903 // Decode pps from avcC
904 cnt = *(p++); // Number of pps
905 for (i = 0; i < cnt; i++) {
906 nalsize = AV_RB16(p) + 2;
907 if(decode_nal_units(h, p, nalsize) != nalsize) {
908 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
909 return -1;
910 }
911 p += nalsize;
912 }
913 // Now store right nal length size, that will be use to parse all other nals
914 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
915 } else {
916 h->is_avc = 0;
917 if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
918 return -1;
919 }
920
884 return 0; 921 return 0;
885 } 922 }
886 923
887 int ff_h264_frame_start(H264Context *h){ 924 int ff_h264_frame_start(H264Context *h){
888 MpegEncContext * const s = &h->s; 925 MpegEncContext * const s = &h->s;
2695 } 2732 }
2696 2733
2697 return 0; 2734 return 0;
2698 } 2735 }
2699 2736
2700 if(h->is_avc && !h->got_avcC) {
2701 int i, cnt, nalsize;
2702 unsigned char *p = avctx->extradata;
2703 if(avctx->extradata_size < 7) {
2704 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
2705 return -1;
2706 }
2707 if(*p != 1) {
2708 av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
2709 return -1;
2710 }
2711 /* sps and pps in the avcC always have length coded with 2 bytes,
2712 so put a fake nal_length_size = 2 while parsing them */
2713 h->nal_length_size = 2;
2714 // Decode sps from avcC
2715 cnt = *(p+5) & 0x1f; // Number of sps
2716 p += 6;
2717 for (i = 0; i < cnt; i++) {
2718 nalsize = AV_RB16(p) + 2;
2719 if(decode_nal_units(h, p, nalsize) < 0) {
2720 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
2721 return -1;
2722 }
2723 p += nalsize;
2724 }
2725 // Decode pps from avcC
2726 cnt = *(p++); // Number of pps
2727 for (i = 0; i < cnt; i++) {
2728 nalsize = AV_RB16(p) + 2;
2729 if(decode_nal_units(h, p, nalsize) != nalsize) {
2730 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
2731 return -1;
2732 }
2733 p += nalsize;
2734 }
2735 // Now store right nal length size, that will be use to parse all other nals
2736 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
2737 // Do not reparse avcC
2738 h->got_avcC = 1;
2739 }
2740
2741 if(!h->got_avcC && !h->is_avc && s->avctx->extradata_size){
2742 if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
2743 return -1;
2744 h->got_avcC = 1;
2745 }
2746
2747 buf_index=decode_nal_units(h, buf, buf_size); 2737 buf_index=decode_nal_units(h, buf, buf_size);
2748 if(buf_index < 0) 2738 if(buf_index < 0)
2749 return -1; 2739 return -1;
2750 2740
2751 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){ 2741 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){