2 * Copyright (C) 2010-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #define UINT16_MAX (65535U)
25 #include "BitstreamConverter.h"
27 void CBitstreamConverter::bits_reader_set( bits_reader_t *br, uint8_t *buf, int len )
29 br->buffer = br->start = buf;
35 uint32_t CBitstreamConverter::read_bits( bits_reader_t *br, int nbits )
42 nbytes = (br->offbits + nbits)/8;
43 if ( ((br->offbits + nbits) %8 ) > 0 )
45 if ( (buf + nbytes) > (br->start + br->length) ) {
49 for ( i=0; i<nbytes; i++ )
50 ret += buf[i]<<((nbytes-i-1)*8);
51 i = (4-nbytes)*8+br->offbits;
52 ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-br->offbits);
55 br->buffer += br->offbits / 8;
61 void CBitstreamConverter::skip_bits( bits_reader_t *br, int nbits )
64 br->buffer += br->offbits / 8;
66 if ( br->buffer > (br->start + br->length) ) {
71 uint32_t CBitstreamConverter::get_bits( bits_reader_t *br, int nbits )
78 nbytes = (br->offbits + nbits)/8;
79 if ( ((br->offbits + nbits) %8 ) > 0 )
81 if ( (buf + nbytes) > (br->start + br->length) ) {
85 for ( i=0; i<nbytes; i++ )
86 ret += buf[i]<<((nbytes-i-1)*8);
87 i = (4-nbytes)*8+br->offbits;
88 ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-br->offbits);
93 ////////////////////////////////////////////////////////////////////////////////////////////
94 /////////////////////////////////////////////////////////////////////////////////////////////
95 // GStreamer h264 parser
96 // Copyright (C) 2005 Michal Benes <michal.benes@itonis.tv>
97 // (C) 2008 Wim Taymans <wim.taymans@gmail.com>
99 // * License as published by the Free Software Foundation; either
100 // * version 2.1 of the License, or (at your option) any later version.
101 void CBitstreamConverter::nal_bs_init(nal_bitstream *bs, const uint8_t *data, size_t size)
104 bs->end = data + size;
106 // fill with something other than 0 to detect
107 // emulation prevention bytes
108 bs->cache = 0xffffffff;
111 uint32_t CBitstreamConverter::nal_bs_read(nal_bitstream *bs, int n)
119 // fill up the cache if we need to
123 bool check_three_byte;
125 check_three_byte = true;
127 if (bs->data >= bs->end)
129 // we're at the end, can't produce more than head number of bits
133 // get the byte, this can be an emulation_prevention_three_byte that we need
135 a_byte = *bs->data++;
136 if (check_three_byte && a_byte == 0x03 && ((bs->cache & 0xffff) == 0))
138 // next byte goes unconditionally to the cache, even if it's 0x03
139 check_three_byte = false;
142 // shift bytes in cache, moving the head bits of the cache left
143 bs->cache = (bs->cache << 8) | a_byte;
147 // bring the required bits down and truncate
148 if ((shift = bs->head - n) > 0)
149 res = bs->cache >> shift;
153 // mask out required bits
161 bool CBitstreamConverter::nal_bs_eos(nal_bitstream *bs)
163 return (bs->data >= bs->end) && (bs->head == 0);
166 // read unsigned Exp-Golomb code
167 int CBitstreamConverter::nal_bs_read_ue(nal_bitstream *bs)
171 while (nal_bs_read(bs, 1) == 0 && !nal_bs_eos(bs) && i < 32)
174 return ((1 << i) - 1 + nal_bs_read(bs, i));
177 void CBitstreamConverter::parseh264_sps(const uint8_t *sps, const uint32_t sps_size, bool *interlaced, int32_t *max_ref_frames)
180 sps_info_struct sps_info;
182 nal_bs_init(&bs, sps, sps_size);
184 sps_info.profile_idc = nal_bs_read(&bs, 8);
185 nal_bs_read(&bs, 1); // constraint_set0_flag
186 nal_bs_read(&bs, 1); // constraint_set1_flag
187 nal_bs_read(&bs, 1); // constraint_set2_flag
188 nal_bs_read(&bs, 1); // constraint_set3_flag
189 nal_bs_read(&bs, 4); // reserved
190 sps_info.level_idc = nal_bs_read(&bs, 8);
191 sps_info.sps_id = nal_bs_read_ue(&bs);
193 if (sps_info.profile_idc == 100 ||
194 sps_info.profile_idc == 110 ||
195 sps_info.profile_idc == 122 ||
196 sps_info.profile_idc == 244 ||
197 sps_info.profile_idc == 44 ||
198 sps_info.profile_idc == 83 ||
199 sps_info.profile_idc == 86)
201 sps_info.chroma_format_idc = nal_bs_read_ue(&bs);
202 if (sps_info.chroma_format_idc == 3)
203 sps_info.separate_colour_plane_flag = nal_bs_read(&bs, 1);
204 sps_info.bit_depth_luma_minus8 = nal_bs_read_ue(&bs);
205 sps_info.bit_depth_chroma_minus8 = nal_bs_read_ue(&bs);
206 sps_info.qpprime_y_zero_transform_bypass_flag = nal_bs_read(&bs, 1);
208 sps_info.seq_scaling_matrix_present_flag = nal_bs_read (&bs, 1);
209 if (sps_info.seq_scaling_matrix_present_flag)
211 /* TODO: unfinished */
214 sps_info.log2_max_frame_num_minus4 = nal_bs_read_ue(&bs);
215 if (sps_info.log2_max_frame_num_minus4 > 12)
216 { // must be between 0 and 12
219 sps_info.pic_order_cnt_type = nal_bs_read_ue(&bs);
220 if (sps_info.pic_order_cnt_type == 0)
222 sps_info.log2_max_pic_order_cnt_lsb_minus4 = nal_bs_read_ue(&bs);
224 else if (sps_info.pic_order_cnt_type == 1)
225 { // TODO: unfinished
227 delta_pic_order_always_zero_flag = gst_nal_bs_read (bs, 1);
228 offset_for_non_ref_pic = gst_nal_bs_read_se (bs);
229 offset_for_top_to_bottom_field = gst_nal_bs_read_se (bs);
231 num_ref_frames_in_pic_order_cnt_cycle = gst_nal_bs_read_ue (bs);
232 for( i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
233 offset_for_ref_frame[i] = gst_nal_bs_read_se (bs);
237 sps_info.max_num_ref_frames = nal_bs_read_ue(&bs);
238 sps_info.gaps_in_frame_num_value_allowed_flag = nal_bs_read(&bs, 1);
239 sps_info.pic_width_in_mbs_minus1 = nal_bs_read_ue(&bs);
240 sps_info.pic_height_in_map_units_minus1 = nal_bs_read_ue(&bs);
242 sps_info.frame_mbs_only_flag = nal_bs_read(&bs, 1);
243 if (!sps_info.frame_mbs_only_flag)
244 sps_info.mb_adaptive_frame_field_flag = nal_bs_read(&bs, 1);
246 sps_info.direct_8x8_inference_flag = nal_bs_read(&bs, 1);
248 sps_info.frame_cropping_flag = nal_bs_read(&bs, 1);
249 if (sps_info.frame_cropping_flag)
251 sps_info.frame_crop_left_offset = nal_bs_read_ue(&bs);
252 sps_info.frame_crop_right_offset = nal_bs_read_ue(&bs);
253 sps_info.frame_crop_top_offset = nal_bs_read_ue(&bs);
254 sps_info.frame_crop_bottom_offset = nal_bs_read_ue(&bs);
257 *interlaced = !sps_info.frame_mbs_only_flag;
258 *max_ref_frames = sps_info.max_num_ref_frames;
261 const uint8_t *CBitstreamConverter::avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
263 const uint8_t *a = p + 4 - ((intptr_t)p & 3);
265 for (end -= 3; p < a && p < end; p++)
267 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
271 for (end -= 3; p < end; p += 4)
273 uint32_t x = *(const uint32_t*)p;
274 if ((x - 0x01010101) & (~x) & 0x80808080) // generic
278 if (p[0] == 0 && p[2] == 1)
280 if (p[2] == 0 && p[3] == 1)
285 if (p[2] == 0 && p[4] == 1)
287 if (p[4] == 0 && p[5] == 1)
293 for (end += 3; p < end; p++)
295 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
302 const uint8_t *CBitstreamConverter::avc_find_startcode(const uint8_t *p, const uint8_t *end)
304 const uint8_t *out= avc_find_startcode_internal(p, end);
305 if (p<out && out<end && !out[-1])
310 const int CBitstreamConverter::avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
312 const uint8_t *p = buf_in;
313 const uint8_t *end = p + size;
314 const uint8_t *nal_start, *nal_end;
317 nal_start = avc_find_startcode(p, end);
320 while (nal_start < end && !*(nal_start++));
321 if (nal_start == end)
324 nal_end = avc_find_startcode(nal_start, end);
325 m_dllAvFormat->avio_wb32(pb, nal_end - nal_start);
326 m_dllAvFormat->avio_write(pb, nal_start, nal_end - nal_start);
327 size += 4 + nal_end - nal_start;
333 const int CBitstreamConverter::avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
336 int ret = m_dllAvFormat->avio_open_dyn_buf(&pb);
340 avc_parse_nal_units(pb, buf_in, *size);
342 m_dllAvUtil->av_freep(buf);
343 *size = m_dllAvFormat->avio_close_dyn_buf(pb, buf);
347 const int CBitstreamConverter::isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
349 // extradata from bytestream h264, convert to avcC atom data for bitstream
352 /* check for h264 start code */
353 if (BS_RB32(data) == 0x00000001 || BS_RB24(data) == 0x000001)
355 uint8_t *buf=NULL, *end, *start;
356 uint32_t sps_size=0, pps_size=0;
357 uint8_t *sps=0, *pps=0;
359 int ret = avc_parse_nal_units_buf(data, &buf, &len);
365 /* look for sps and pps */
366 while (end - buf > 4)
370 size = FFMIN(BS_RB32(buf), end - buf - 4);
372 nal_type = buf[0] & 0x1f;
373 if (nal_type == 7) /* SPS */
378 else if (nal_type == 8) /* PPS */
385 if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
388 m_dllAvFormat->avio_w8(pb, 1); /* version */
389 m_dllAvFormat->avio_w8(pb, sps[1]); /* profile */
390 m_dllAvFormat->avio_w8(pb, sps[2]); /* profile compat */
391 m_dllAvFormat->avio_w8(pb, sps[3]); /* level */
392 m_dllAvFormat->avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
393 m_dllAvFormat->avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
395 m_dllAvFormat->avio_wb16(pb, sps_size);
396 m_dllAvFormat->avio_write(pb, sps, sps_size);
399 m_dllAvFormat->avio_w8(pb, 1); /* number of pps */
400 m_dllAvFormat->avio_wb16(pb, pps_size);
401 m_dllAvFormat->avio_write(pb, pps, pps_size);
403 m_dllAvUtil->av_free(start);
407 m_dllAvFormat->avio_write(pb, data, len);
413 CBitstreamConverter::CBitstreamConverter()
415 m_convert_bitstream = false;
416 m_convertBuffer = NULL;
418 m_inputBuffer = NULL;
423 m_convert_3byteTo4byteNALSize = false;
425 m_dllAvFormat = NULL;
426 m_convert_bytestream = false;
429 CBitstreamConverter::~CBitstreamConverter()
434 bool CBitstreamConverter::Open(enum CodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb)
436 m_to_annexb = to_annexb;
443 if (in_extrasize < 7 || in_extradata == NULL)
445 CLog::Log(LOGERROR, "CBitstreamConverter::Open avcC data too small or missing\n");
448 // valid avcC data (bitstream) always starts with the value 1 (version)
451 if ( *(char*)in_extradata == 1 )
453 CLog::Log(LOGINFO, "CBitstreamConverter::Open bitstream to annexb init\n");
454 m_convert_bitstream = BitstreamConvertInit(in_extradata, in_extrasize);
460 // valid avcC atom data always starts with the value 1 (version)
461 if ( *in_extradata != 1 )
463 if ( (in_extradata[0] == 0 && in_extradata[1] == 0 && in_extradata[2] == 0 && in_extradata[3] == 1) ||
464 (in_extradata[0] == 0 && in_extradata[1] == 0 && in_extradata[2] == 1) )
466 CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init\n");
467 // video content is from x264 or from bytestream h264 (AnnexB format)
468 // NAL reformating to bitstream format needed
469 m_dllAvUtil = new DllAvUtil;
470 m_dllAvFormat = new DllAvFormat;
471 if (!m_dllAvUtil->Load() || !m_dllAvFormat->Load())
475 if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
477 m_convert_bytestream = true;
478 // create a valid avcC atom data from ffmpeg's extradata
479 isom_write_avcc(pb, in_extradata, in_extrasize);
480 // unhook from ffmpeg's extradata
482 // extract the avcC atom data into extradata then write it into avcCData for VDADecoder
483 in_extrasize = m_dllAvFormat->avio_close_dyn_buf(pb, &in_extradata);
484 // make a copy of extradata contents
485 m_extradata = (uint8_t *)malloc(in_extrasize);
486 memcpy(m_extradata, in_extradata, in_extrasize);
487 m_extrasize = in_extrasize;
488 // done with the converted extradata, we MUST free using av_free
489 m_dllAvUtil->av_free(in_extradata);
494 CLog::Log(LOGNOTICE, "CBitstreamConverter::Open invalid avcC atom data");
500 if (in_extradata[4] == 0xFE)
502 CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init 3 byte to 4 byte nal\n");
503 // video content is from so silly encoder that think 3 byte NAL sizes
504 // are valid, setup to convert 3 byte NAL sizes to 4 byte.
505 m_dllAvUtil = new DllAvUtil;
506 m_dllAvFormat = new DllAvFormat;
507 if (!m_dllAvUtil->Load() || !m_dllAvFormat->Load())
510 in_extradata[4] = 0xFF;
511 m_convert_3byteTo4byteNALSize = true;
513 m_extradata = (uint8_t *)malloc(in_extrasize);
514 memcpy(m_extradata, in_extradata, in_extrasize);
515 m_extrasize = in_extrasize;
529 void CBitstreamConverter::Close(void)
531 if (m_convert_bitstream)
533 if (m_sps_pps_context.sps_pps_data)
535 free(m_sps_pps_context.sps_pps_data);
536 m_sps_pps_context.sps_pps_data = NULL;
540 free(m_convertBuffer);
541 m_convertBuffer = NULL;
546 if (m_convert_bytestream)
550 m_dllAvUtil->av_free(m_convertBuffer);
551 m_convertBuffer = NULL;
561 m_inputBuffer = NULL;
563 m_convert_3byteTo4byteNALSize = false;
565 m_convert_bitstream = false;
574 delete m_dllAvFormat;
575 m_dllAvFormat = NULL;
579 bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
582 free(m_convertBuffer);
583 m_convertBuffer = NULL;
585 m_inputBuffer = NULL;
590 if(m_codec == CODEC_ID_H264)
594 int demuxer_bytes = iSize;
596 uint8_t *demuxer_content = pData;
598 if (m_convert_bitstream)
600 // convert demuxer packet from bitstream to bytestream (AnnexB)
601 int bytestream_size = 0;
602 uint8_t *bytestream_buff = NULL;
604 BitstreamConvert(demuxer_content, demuxer_bytes, &bytestream_buff, &bytestream_size);
605 if (bytestream_buff && (bytestream_size > 0))
607 m_convertSize = bytestream_size;
608 m_convertBuffer = bytestream_buff;
613 m_inputBuffer = pData;
615 CLog::Log(LOGERROR, "CBitstreamConverter::Convert error converting. disable converter\n");
620 m_inputBuffer = pData;
628 m_inputBuffer = pData;
631 if (m_convert_bytestream)
635 m_dllAvUtil->av_free(m_convertBuffer);
636 m_convertBuffer = NULL;
640 // convert demuxer packet from bytestream (AnnexB) to bitstream
643 if(m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
647 m_convertSize = avc_parse_nal_units(pb, pData, iSize);
648 m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
650 else if (m_convert_3byteTo4byteNALSize)
654 m_dllAvUtil->av_free(m_convertBuffer);
655 m_convertBuffer = NULL;
659 // convert demuxer packet from 3 byte NAL sizes to 4 byte
661 if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
665 uint8_t *end = pData + iSize;
666 uint8_t *nal_start = pData;
667 while (nal_start < end)
669 nal_size = BS_RB24(nal_start);
670 m_dllAvFormat->avio_wb16(pb, nal_size);
672 m_dllAvFormat->avio_write(pb, nal_start, nal_size);
673 nal_start += nal_size;
676 m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
687 uint8_t *CBitstreamConverter::GetConvertBuffer()
689 if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
690 return m_convertBuffer;
692 return m_inputBuffer;
695 int CBitstreamConverter::GetConvertSize()
697 if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
698 return m_convertSize;
703 uint8_t *CBitstreamConverter::GetExtraData()
705 if(m_convert_bitstream)
706 return m_sps_pps_context.sps_pps_data;
710 int CBitstreamConverter::GetExtraSize()
712 if(m_convert_bitstream)
713 return m_sps_pps_context.size;
718 bool CBitstreamConverter::BitstreamConvertInit(void *in_extradata, int in_extrasize)
720 // based on h264_mp4toannexb_bsf.c (ffmpeg)
721 // which is Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
722 // and Licensed GPL 2.1 or greater
725 m_sps_pps_context.sps_pps_data = NULL;
728 if (!in_extradata || in_extrasize < 6)
732 uint32_t total_size = 0;
733 uint8_t *out = NULL, unit_nb, sps_done = 0;
734 const uint8_t *extradata = (uint8_t*)in_extradata + 4;
735 static const uint8_t nalu_header[4] = {0, 0, 0, 1};
737 // retrieve length coded size
738 m_sps_pps_context.length_size = (*extradata++ & 0x3) + 1;
739 if (m_sps_pps_context.length_size == 3)
742 // retrieve sps and pps unit(s)
743 unit_nb = *extradata++ & 0x1f; // number of sps unit(s)
746 unit_nb = *extradata++; // number of pps unit(s)
751 unit_size = extradata[0] << 8 | extradata[1];
752 total_size += unit_size + 4;
753 if ( (extradata + 2 + unit_size) > ((uint8_t*)in_extradata + in_extrasize) )
758 out = (uint8_t*)realloc(out, total_size);
762 memcpy(out + total_size - unit_size - 4, nalu_header, 4);
763 memcpy(out + total_size - unit_size, extradata + 2, unit_size);
764 extradata += 2 + unit_size;
766 if (!unit_nb && !sps_done++)
767 unit_nb = *extradata++; // number of pps unit(s)
770 m_sps_pps_context.sps_pps_data = out;
771 m_sps_pps_context.size = total_size;
772 m_sps_pps_context.first_idr = 1;
777 bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **poutbuf, int *poutbuf_size)
779 // based on h264_mp4toannexb_bsf.c (ffmpeg)
780 // which is Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
781 // and Licensed GPL 2.1 or greater
784 uint8_t *buf = pData;
785 uint32_t buf_size = iSize;
788 uint32_t cumul_size = 0;
789 const uint8_t *buf_end = buf + buf_size;
793 if (buf + m_sps_pps_context.length_size > buf_end)
796 if (m_sps_pps_context.length_size == 1)
798 else if (m_sps_pps_context.length_size == 2)
799 nal_size = buf[0] << 8 | buf[1];
801 nal_size = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
803 buf += m_sps_pps_context.length_size;
804 unit_type = *buf & 0x1f;
806 if (buf + nal_size > buf_end || nal_size < 0)
809 // prepend only to the first type 5 NAL unit of an IDR picture
810 if (m_sps_pps_context.first_idr && unit_type == 5)
812 BitstreamAllocAndCopy(poutbuf, poutbuf_size,
813 m_sps_pps_context.sps_pps_data, m_sps_pps_context.size, buf, nal_size);
814 m_sps_pps_context.first_idr = 0;
818 BitstreamAllocAndCopy(poutbuf, poutbuf_size, NULL, 0, buf, nal_size);
819 if (!m_sps_pps_context.first_idr && unit_type == 1)
820 m_sps_pps_context.first_idr = 1;
824 cumul_size += nal_size + m_sps_pps_context.length_size;
825 } while (cumul_size < buf_size);
836 void CBitstreamConverter::BitstreamAllocAndCopy( uint8_t **poutbuf, int *poutbuf_size,
837 const uint8_t *sps_pps, uint32_t sps_pps_size, const uint8_t *in, uint32_t in_size)
839 // based on h264_mp4toannexb_bsf.c (ffmpeg)
840 // which is Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
841 // and Licensed GPL 2.1 or greater
843 #define CHD_WB32(p, d) { \
844 ((uint8_t*)(p))[3] = (d); \
845 ((uint8_t*)(p))[2] = (d) >> 8; \
846 ((uint8_t*)(p))[1] = (d) >> 16; \
847 ((uint8_t*)(p))[0] = (d) >> 24; }
849 uint32_t offset = *poutbuf_size;
850 uint8_t nal_header_size = offset ? 3 : 4;
852 *poutbuf_size += sps_pps_size + in_size + nal_header_size;
853 *poutbuf = (uint8_t*)realloc(*poutbuf, *poutbuf_size);
855 memcpy(*poutbuf + offset, sps_pps, sps_pps_size);
857 memcpy(*poutbuf + sps_pps_size + nal_header_size + offset, in, in_size);
860 CHD_WB32(*poutbuf + sps_pps_size, 1);
864 (*poutbuf + offset + sps_pps_size)[0] = 0;
865 (*poutbuf + offset + sps_pps_size)[1] = 0;
866 (*poutbuf + offset + sps_pps_size)[2] = 1;