comparison h264.c @ 2227:bf414a07af2e libavcodec

AVC (H264 in mp4 files, fourcc avc1) support
author rtognimp
date Mon, 13 Sep 2004 21:20:55 +0000
parents 733c60a6e30c
children 0dfe4e32b19c
comparison
equal deleted inserted replaced
2226:4306aa6f8f16 2227:bf414a07af2e
148 #define NAL_PPS 8 148 #define NAL_PPS 8
149 #define NAL_PICTURE_DELIMITER 9 149 #define NAL_PICTURE_DELIMITER 9
150 #define NAL_FILTER_DATA 10 150 #define NAL_FILTER_DATA 10
151 uint8_t *rbsp_buffer; 151 uint8_t *rbsp_buffer;
152 int rbsp_buffer_size; 152 int rbsp_buffer_size;
153
154 /**
155 * Used to parse AVC variant of h264
156 */
157 int is_avc; ///< this flag is != 0 if codec is avc1
158 int got_avcC; ///< flag used to parse avcC data only once
159 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
153 160
154 int chroma_qp; //QPc 161 int chroma_qp; //QPc
155 162
156 int prev_mb_skiped; //FIXME remove (IMHO not used) 163 int prev_mb_skiped; //FIXME remove (IMHO not used)
157 164
2262 s->low_delay= 1; 2269 s->low_delay= 1;
2263 avctx->pix_fmt= PIX_FMT_YUV420P; 2270 avctx->pix_fmt= PIX_FMT_YUV420P;
2264 2271
2265 decode_init_vlc(h); 2272 decode_init_vlc(h);
2266 2273
2274 if(avctx->codec_tag != 0x31637661) // avc1
2275 h->is_avc = 0;
2276 else {
2277 if((avctx->extradata_size == 0) || (avctx->extradata == NULL)) {
2278 av_log(avctx, AV_LOG_ERROR, "AVC codec requires avcC data\n");
2279 return -1;
2280 }
2281 h->is_avc = 1;
2282 h->got_avcC = 0;
2283 }
2284
2267 return 0; 2285 return 0;
2268 } 2286 }
2269 2287
2270 static void frame_start(H264Context *h){ 2288 static void frame_start(H264Context *h){
2271 MpegEncContext * const s = &h->s; 2289 MpegEncContext * const s = &h->s;
5599 for(;;){ 5617 for(;;){
5600 int consumed; 5618 int consumed;
5601 int dst_length; 5619 int dst_length;
5602 int bit_length; 5620 int bit_length;
5603 uint8_t *ptr; 5621 uint8_t *ptr;
5604 5622 int i, nalsize = 0;
5623
5624 if(h->is_avc) {
5625 if(buf_index >= buf_size) break;
5626 nalsize = 0;
5627 for(i = 0; i < h->nal_length_size; i++)
5628 nalsize = (nalsize << 8) | buf[buf_index++];
5629 } else {
5605 // start code prefix search 5630 // start code prefix search
5606 for(; buf_index + 3 < buf_size; buf_index++){ 5631 for(; buf_index + 3 < buf_size; buf_index++){
5607 // this should allways succeed in the first iteration 5632 // this should allways succeed in the first iteration
5608 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1) 5633 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
5609 break; 5634 break;
5610 } 5635 }
5611 5636
5612 if(buf_index+3 >= buf_size) break; 5637 if(buf_index+3 >= buf_size) break;
5613 5638
5614 buf_index+=3; 5639 buf_index+=3;
5640 }
5615 5641
5616 ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index); 5642 ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index);
5617 if(ptr[dst_length - 1] == 0) dst_length--; 5643 if(ptr[dst_length - 1] == 0) dst_length--;
5618 bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1); 5644 bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
5619 5645
5620 if(s->avctx->debug&FF_DEBUG_STARTCODE){ 5646 if(s->avctx->debug&FF_DEBUG_STARTCODE){
5621 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length); 5647 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length);
5622 } 5648 }
5623 5649
5650 if (h->is_avc && (nalsize != consumed))
5651 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
5652
5624 buf_index += consumed; 5653 buf_index += consumed;
5625 5654
5626 if( s->hurry_up == 1 && h->nal_ref_idc == 0 ) 5655 if( s->hurry_up == 1 && h->nal_ref_idc == 0 )
5627 continue; 5656 continue;
5628 5657
5748 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 ) 5777 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
5749 return buf_size; 5778 return buf_size;
5750 //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index); 5779 //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
5751 } 5780 }
5752 5781
5753 if(s->avctx->extradata_size && s->picture_number==0){ 5782 if(h->is_avc && !h->got_avcC) {
5783 int i, cnt, nalsize;
5784 unsigned char *p = avctx->extradata;
5785 if(avctx->extradata_size < 7) {
5786 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
5787 return -1;
5788 }
5789 if(*p != 1) {
5790 av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
5791 return -1;
5792 }
5793 /* sps and pps in the avcC always have length coded with 2 bytes,
5794 so put a fake nal_length_size = 2 while parsing them */
5795 h->nal_length_size = 2;
5796 // Decode sps from avcC
5797 cnt = *(p+5) & 0x1f; // Number of sps
5798 p += 6;
5799 for (i = 0; i < cnt; i++) {
5800 nalsize = BE_16(p) + 2;
5801 if(decode_nal_units(h, p, nalsize) != nalsize) {
5802 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
5803 return -1;
5804 }
5805 p += nalsize;
5806 }
5807 // Decode pps from avcC
5808 cnt = *(p++); // Number of pps
5809 for (i = 0; i < cnt; i++) {
5810 nalsize = BE_16(p) + 2;
5811 if(decode_nal_units(h, p, nalsize) != nalsize) {
5812 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
5813 return -1;
5814 }
5815 p += nalsize;
5816 }
5817 // Now store right nal length size, that will be use to parse all other nals
5818 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
5819 // Do not reparse avcC
5820 h->got_avcC = 1;
5821 }
5822
5823 if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
5754 if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) ) 5824 if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
5755 return -1; 5825 return -1;
5756 } 5826 }
5757 5827
5758 buf_index=decode_nal_units(h, buf, buf_size); 5828 buf_index=decode_nal_units(h, buf, buf_size);