[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / utils / BitstreamConverter.cpp
1 /*
2  *      Copyright (C) 2010-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #ifndef UINT16_MAX
22 #define UINT16_MAX             (65535U)
23 #endif
24
25 #include "BitstreamConverter.h"
26
27 void CBitstreamConverter::bits_reader_set( bits_reader_t *br, uint8_t *buf, int len )
28 {
29   br->buffer = br->start = buf;
30   br->offbits = 0;
31   br->length = len;
32   br->oflow = 0;
33 }
34
35 uint32_t CBitstreamConverter::read_bits( bits_reader_t *br, int nbits )
36 {
37   int i, nbytes;
38   uint32_t ret = 0;
39   uint8_t *buf;
40
41   buf = br->buffer;
42   nbytes = (br->offbits + nbits)/8;
43   if ( ((br->offbits + nbits) %8 ) > 0 )
44     nbytes++;
45   if ( (buf + nbytes) > (br->start + br->length) ) {
46     br->oflow = 1;
47     return 0;
48   }
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);
53
54   br->offbits += nbits;
55   br->buffer += br->offbits / 8;
56   br->offbits %= 8;
57
58   return ret;
59 }
60
61 void CBitstreamConverter::skip_bits( bits_reader_t *br, int nbits )
62 {
63   br->offbits += nbits;
64   br->buffer += br->offbits / 8;
65   br->offbits %= 8;
66   if ( br->buffer > (br->start + br->length) ) {
67     br->oflow = 1;
68   }
69 }
70
71 uint32_t CBitstreamConverter::get_bits( bits_reader_t *br, int nbits )
72 {
73   int i, nbytes;
74   uint32_t ret = 0;
75   uint8_t *buf;
76
77   buf = br->buffer;
78   nbytes = (br->offbits + nbits)/8;
79   if ( ((br->offbits + nbits) %8 ) > 0 )
80     nbytes++;
81   if ( (buf + nbytes) > (br->start + br->length) ) {
82     br->oflow = 1;
83     return 0;
84   }
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);
89
90   return ret;
91 }
92
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>
98 // gsth264parse.c:
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)
102 {
103   bs->data = data;
104   bs->end  = data + size;
105   bs->head = 0;
106   // fill with something other than 0 to detect
107   //  emulation prevention bytes
108   bs->cache = 0xffffffff;
109 }
110
111 uint32_t CBitstreamConverter::nal_bs_read(nal_bitstream *bs, int n)
112 {
113   uint32_t res = 0;
114   int shift;
115
116   if (n == 0)
117     return res;
118
119   // fill up the cache if we need to
120   while (bs->head < n)
121   {
122     uint8_t a_byte;
123     bool check_three_byte;
124
125     check_three_byte = true;
126 next_byte:
127     if (bs->data >= bs->end)
128     {
129       // we're at the end, can't produce more than head number of bits
130       n = bs->head;
131       break;
132     }
133     // get the byte, this can be an emulation_prevention_three_byte that we need
134     // to ignore.
135     a_byte = *bs->data++;
136     if (check_three_byte && a_byte == 0x03 && ((bs->cache & 0xffff) == 0))
137     {
138       // next byte goes unconditionally to the cache, even if it's 0x03
139       check_three_byte = false;
140       goto next_byte;
141     }
142     // shift bytes in cache, moving the head bits of the cache left
143     bs->cache = (bs->cache << 8) | a_byte;
144     bs->head += 8;
145   }
146
147   // bring the required bits down and truncate
148   if ((shift = bs->head - n) > 0)
149     res = bs->cache >> shift;
150   else
151     res = bs->cache;
152
153   // mask out required bits
154   if (n < 32)
155     res &= (1 << n) - 1;
156   bs->head = shift;
157
158   return res;
159 }
160
161 bool CBitstreamConverter::nal_bs_eos(nal_bitstream *bs)
162 {
163   return (bs->data >= bs->end) && (bs->head == 0);
164 }
165
166 // read unsigned Exp-Golomb code
167 int CBitstreamConverter::nal_bs_read_ue(nal_bitstream *bs)
168 {
169   int i = 0;
170
171   while (nal_bs_read(bs, 1) == 0 && !nal_bs_eos(bs) && i < 32)
172     i++;
173
174   return ((1 << i) - 1 + nal_bs_read(bs, i));
175 }
176
177 void CBitstreamConverter::parseh264_sps(const uint8_t *sps, const uint32_t sps_size, bool *interlaced, int32_t *max_ref_frames)
178 {
179   nal_bitstream bs;
180   sps_info_struct sps_info;
181
182   nal_bs_init(&bs, sps, sps_size);
183
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);
192
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)
200   {
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);
207
208     sps_info.seq_scaling_matrix_present_flag = nal_bs_read (&bs, 1);
209     if (sps_info.seq_scaling_matrix_present_flag)
210     {
211       /* TODO: unfinished */
212     }
213   }
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
217     return;
218   }
219   sps_info.pic_order_cnt_type = nal_bs_read_ue(&bs);
220   if (sps_info.pic_order_cnt_type == 0)
221   {
222     sps_info.log2_max_pic_order_cnt_lsb_minus4 = nal_bs_read_ue(&bs);
223   }
224   else if (sps_info.pic_order_cnt_type == 1)
225   { // TODO: unfinished
226     /*
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);
230
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);
234     */
235   }
236
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);
241
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);
245
246   sps_info.direct_8x8_inference_flag      = nal_bs_read(&bs, 1);
247
248   sps_info.frame_cropping_flag            = nal_bs_read(&bs, 1);
249   if (sps_info.frame_cropping_flag)
250   {
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);
255   }
256
257   *interlaced = !sps_info.frame_mbs_only_flag;
258   *max_ref_frames = sps_info.max_num_ref_frames;
259 }
260
261 const uint8_t *CBitstreamConverter::avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
262 {
263   const uint8_t *a = p + 4 - ((intptr_t)p & 3);
264
265   for (end -= 3; p < a && p < end; p++)
266   {
267     if (p[0] == 0 && p[1] == 0 && p[2] == 1)
268       return p;
269   }
270
271   for (end -= 3; p < end; p += 4)
272   {
273     uint32_t x = *(const uint32_t*)p;
274     if ((x - 0x01010101) & (~x) & 0x80808080) // generic
275     {
276       if (p[1] == 0)
277       {
278         if (p[0] == 0 && p[2] == 1)
279           return p;
280         if (p[2] == 0 && p[3] == 1)
281           return p+1;
282       }
283       if (p[3] == 0)
284       {
285         if (p[2] == 0 && p[4] == 1)
286           return p+2;
287         if (p[4] == 0 && p[5] == 1)
288           return p+3;
289       }
290     }
291   }
292
293   for (end += 3; p < end; p++)
294   {
295     if (p[0] == 0 && p[1] == 0 && p[2] == 1)
296       return p;
297   }
298
299   return end + 3;
300 }
301
302 const uint8_t *CBitstreamConverter::avc_find_startcode(const uint8_t *p, const uint8_t *end)
303 {
304   const uint8_t *out= avc_find_startcode_internal(p, end);
305   if (p<out && out<end && !out[-1])
306     out--;
307   return out;
308 }
309
310 const int CBitstreamConverter::avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
311 {
312   const uint8_t *p = buf_in;
313   const uint8_t *end = p + size;
314   const uint8_t *nal_start, *nal_end;
315
316   size = 0;
317   nal_start = avc_find_startcode(p, end);
318
319   for (;;) {
320     while (nal_start < end && !*(nal_start++));
321     if (nal_start == end)
322       break;
323
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;
328     nal_start = nal_end;
329   }
330   return size;
331 }
332
333 const int CBitstreamConverter::avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
334 {
335   AVIOContext *pb;
336   int ret = m_dllAvFormat->avio_open_dyn_buf(&pb);
337   if (ret < 0)
338     return ret;
339
340   avc_parse_nal_units(pb, buf_in, *size);
341
342   m_dllAvUtil->av_freep(buf);
343   *size = m_dllAvFormat->avio_close_dyn_buf(pb, buf);
344   return 0;
345 }
346
347 const int CBitstreamConverter::isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
348 {
349   // extradata from bytestream h264, convert to avcC atom data for bitstream
350   if (len > 6)
351   {
352     /* check for h264 start code */
353     if (BS_RB32(data) == 0x00000001 || BS_RB24(data) == 0x000001)
354     {
355       uint8_t *buf=NULL, *end, *start;
356       uint32_t sps_size=0, pps_size=0;
357       uint8_t *sps=0, *pps=0;
358
359       int ret = avc_parse_nal_units_buf(data, &buf, &len);
360       if (ret < 0)
361         return ret;
362       start = buf;
363       end = buf + len;
364
365       /* look for sps and pps */
366       while (end - buf > 4)
367       {
368         uint32_t size;
369         uint8_t  nal_type;
370         size = FFMIN(BS_RB32(buf), end - buf - 4);
371         buf += 4;
372         nal_type = buf[0] & 0x1f;
373         if (nal_type == 7) /* SPS */
374         {
375           sps = buf;
376           sps_size = size;
377         }
378         else if (nal_type == 8) /* PPS */
379         {
380           pps = buf;
381           pps_size = size;
382         }
383         buf += size;
384       }
385       if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
386         assert(0);
387
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) */
394
395       m_dllAvFormat->avio_wb16(pb, sps_size);
396       m_dllAvFormat->avio_write(pb, sps, sps_size);
397       if (pps)
398       {
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);
402       }
403       m_dllAvUtil->av_free(start);
404     }
405     else
406     {
407       m_dllAvFormat->avio_write(pb, data, len);
408     }
409   }
410   return 0;
411 }
412
413 CBitstreamConverter::CBitstreamConverter()
414 {
415   m_convert_bitstream = false;
416   m_convertBuffer     = NULL;
417   m_convertSize       = 0;
418   m_inputBuffer       = NULL;
419   m_inputSize         = 0;
420   m_to_annexb         = false;
421   m_extradata         = NULL;
422   m_extrasize         = 0;
423   m_convert_3byteTo4byteNALSize = false;
424   m_dllAvUtil         = NULL;
425   m_dllAvFormat       = NULL;
426   m_convert_bytestream = false;
427 }
428
429 CBitstreamConverter::~CBitstreamConverter()
430 {
431   Close();
432 }
433
434 bool CBitstreamConverter::Open(enum CodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb)
435 {
436   m_to_annexb = to_annexb;
437
438   m_codec = codec;
439
440   switch(codec)
441   {
442     case CODEC_ID_H264:
443       if (in_extrasize < 7 || in_extradata == NULL)
444       {
445         CLog::Log(LOGERROR, "CBitstreamConverter::Open avcC data too small or missing\n");
446         return false;
447       }
448       // valid avcC data (bitstream) always starts with the value 1 (version)
449       if(m_to_annexb)
450       {
451         if ( *(char*)in_extradata == 1 )
452         {
453           CLog::Log(LOGINFO, "CBitstreamConverter::Open bitstream to annexb init\n");
454           m_convert_bitstream = BitstreamConvertInit(in_extradata, in_extrasize);
455           return true;
456         }
457       }
458       else
459       {
460         // valid avcC atom data always starts with the value 1 (version)
461         if ( *in_extradata != 1 )
462         {
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) )
465           {
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())
472               return false;
473
474             AVIOContext *pb;
475             if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
476               return false;
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
481             in_extradata = NULL;
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);
490             return true;
491           }
492           else
493           {
494             CLog::Log(LOGNOTICE, "CBitstreamConverter::Open invalid avcC atom data");
495             return false;
496           }
497         }
498         else
499         {
500           if (in_extradata[4] == 0xFE)
501           {
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())
508               return false;
509
510             in_extradata[4] = 0xFF;
511             m_convert_3byteTo4byteNALSize = true;
512            
513             m_extradata = (uint8_t *)malloc(in_extrasize);
514             memcpy(m_extradata, in_extradata, in_extrasize);
515             m_extrasize = in_extrasize;
516             return true;
517           }
518         }
519       }
520       return false;
521       break;
522     default:
523       return false;
524       break;
525   }
526   return false;
527 }
528
529 void CBitstreamConverter::Close(void)
530 {
531   if (m_convert_bitstream)
532   {
533     if (m_sps_pps_context.sps_pps_data)
534     {
535       free(m_sps_pps_context.sps_pps_data);
536       m_sps_pps_context.sps_pps_data = NULL;
537     }
538     if(m_convertBuffer)
539     {
540       free(m_convertBuffer);
541       m_convertBuffer = NULL;
542     }
543     m_convertSize       = 0;
544   }
545
546   if (m_convert_bytestream)
547   {
548     if(m_convertBuffer)
549     {
550       m_dllAvUtil->av_free(m_convertBuffer);
551       m_convertBuffer = NULL;
552     }
553     m_convertSize = 0;
554   }
555
556   if(m_extradata)
557     free(m_extradata);
558   m_extradata = NULL;
559   m_extrasize = 0;
560
561   m_inputBuffer       = NULL;
562   m_inputSize         = 0;
563   m_convert_3byteTo4byteNALSize = false;
564
565   m_convert_bitstream = false;
566
567   if (m_dllAvUtil)
568   {
569     delete m_dllAvUtil;
570     m_dllAvUtil = NULL;
571   }
572   if (m_dllAvFormat)
573   {
574     delete m_dllAvFormat;
575     m_dllAvFormat = NULL;
576   }
577 }
578
579 bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
580 {
581   if(m_convertBuffer)
582     free(m_convertBuffer);
583   m_convertBuffer = NULL;
584   m_convertSize   = 0;
585   m_inputBuffer   = NULL;
586   m_inputSize     = 0;
587
588   if (pData)
589   {
590     if(m_codec == CODEC_ID_H264)
591     {
592       if(m_to_annexb)
593       {
594         int demuxer_bytes = iSize;
595   
596         uint8_t *demuxer_content = pData;
597
598         if (m_convert_bitstream)
599         {
600           // convert demuxer packet from bitstream to bytestream (AnnexB)
601           int bytestream_size = 0;
602           uint8_t *bytestream_buff = NULL;
603
604           BitstreamConvert(demuxer_content, demuxer_bytes, &bytestream_buff, &bytestream_size);
605           if (bytestream_buff && (bytestream_size > 0))
606           {
607             m_convertSize   = bytestream_size;
608             m_convertBuffer = bytestream_buff;
609           }
610           else
611           {
612             Close();
613             m_inputBuffer = pData;
614             m_inputSize   = iSize;
615             CLog::Log(LOGERROR, "CBitstreamConverter::Convert error converting. disable converter\n");
616           }
617         }
618         else
619         {
620           m_inputBuffer = pData;
621           m_inputSize   = iSize;
622         }
623
624         return true;
625       }
626       else
627       {
628         m_inputBuffer = pData;
629         m_inputSize   = iSize;
630   
631         if (m_convert_bytestream)
632         {
633           if(m_convertBuffer)
634           {
635             m_dllAvUtil->av_free(m_convertBuffer);
636             m_convertBuffer = NULL;
637           }
638           m_convertSize = 0;
639
640           // convert demuxer packet from bytestream (AnnexB) to bitstream
641           AVIOContext *pb;
642   
643           if(m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
644           {
645             return false;
646           }
647           m_convertSize = avc_parse_nal_units(pb, pData, iSize);
648           m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
649         }
650         else if (m_convert_3byteTo4byteNALSize)
651         {
652           if(m_convertBuffer)
653           {
654             m_dllAvUtil->av_free(m_convertBuffer);
655             m_convertBuffer = NULL;
656           }
657           m_convertSize = 0;
658
659           // convert demuxer packet from 3 byte NAL sizes to 4 byte
660           AVIOContext *pb;
661           if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
662             return false;
663
664           uint32_t nal_size;
665           uint8_t *end = pData + iSize;
666           uint8_t *nal_start = pData;
667           while (nal_start < end)
668           {
669             nal_size = BS_RB24(nal_start);
670             m_dllAvFormat->avio_wb16(pb, nal_size);
671             nal_start += 3;
672             m_dllAvFormat->avio_write(pb, nal_start, nal_size);
673             nal_start += nal_size;
674           }
675   
676           m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
677         }
678         return true;
679       }
680     }
681   }
682
683   return false;
684 }
685
686
687 uint8_t *CBitstreamConverter::GetConvertBuffer()
688 {
689   if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
690     return m_convertBuffer;
691   else
692     return m_inputBuffer;
693 }
694
695 int CBitstreamConverter::GetConvertSize()
696 {
697   if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
698     return m_convertSize;
699   else
700     return m_inputSize; 
701 }
702
703 uint8_t *CBitstreamConverter::GetExtraData()
704 {
705   if(m_convert_bitstream)
706     return m_sps_pps_context.sps_pps_data;
707   else
708     return m_extradata;
709 }
710 int CBitstreamConverter::GetExtraSize()
711 {
712   if(m_convert_bitstream)
713     return m_sps_pps_context.size;
714   else
715     return m_extrasize;
716 }
717
718 bool CBitstreamConverter::BitstreamConvertInit(void *in_extradata, int in_extrasize)
719 {
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
723
724   m_sps_pps_size = 0;
725   m_sps_pps_context.sps_pps_data = NULL;
726
727   // nothing to filter
728   if (!in_extradata || in_extrasize < 6)
729     return false;
730
731   uint16_t unit_size;
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};
736
737   // retrieve length coded size
738   m_sps_pps_context.length_size = (*extradata++ & 0x3) + 1;
739   if (m_sps_pps_context.length_size == 3)
740     return false;
741
742   // retrieve sps and pps unit(s)
743   unit_nb = *extradata++ & 0x1f;  // number of sps unit(s)
744   if (!unit_nb)
745   {
746     unit_nb = *extradata++;       // number of pps unit(s)
747     sps_done++;
748   }
749   while (unit_nb--)
750   {
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) )
754     {
755       free(out);
756       return false;
757     }
758     out = (uint8_t*)realloc(out, total_size);
759     if (!out)
760       return false;
761
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;
765
766     if (!unit_nb && !sps_done++)
767       unit_nb = *extradata++;     // number of pps unit(s)
768   }
769
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;
773
774   return true;
775 }
776
777 bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **poutbuf, int *poutbuf_size)
778 {
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
782
783
784   uint8_t *buf = pData;
785   uint32_t buf_size = iSize;
786   uint8_t  unit_type;
787   int32_t  nal_size;
788   uint32_t cumul_size = 0;
789   const uint8_t *buf_end = buf + buf_size;
790
791   do
792   {
793     if (buf + m_sps_pps_context.length_size > buf_end)
794       goto fail;
795
796     if (m_sps_pps_context.length_size == 1)
797       nal_size = buf[0];
798     else if (m_sps_pps_context.length_size == 2)
799       nal_size = buf[0] << 8 | buf[1];
800     else
801       nal_size = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
802
803     buf += m_sps_pps_context.length_size;
804     unit_type = *buf & 0x1f;
805
806     if (buf + nal_size > buf_end || nal_size < 0)
807       goto fail;
808
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)
811     {
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;
815     }
816     else
817     {
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;
821     }
822
823     buf += nal_size;
824     cumul_size += nal_size + m_sps_pps_context.length_size;
825   } while (cumul_size < buf_size);
826
827   return true;
828
829 fail:
830   free(*poutbuf);
831   *poutbuf = NULL;
832   *poutbuf_size = 0;
833   return false;
834 }
835
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)
838 {
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
842
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; }
848
849   uint32_t offset = *poutbuf_size;
850   uint8_t nal_header_size = offset ? 3 : 4;
851
852   *poutbuf_size += sps_pps_size + in_size + nal_header_size;
853   *poutbuf = (uint8_t*)realloc(*poutbuf, *poutbuf_size);
854   if (sps_pps)
855     memcpy(*poutbuf + offset, sps_pps, sps_pps_size);
856
857   memcpy(*poutbuf + sps_pps_size + nal_header_size + offset, in, in_size);
858   if (!offset)
859   {
860     CHD_WB32(*poutbuf + sps_pps_size, 1);
861   }
862   else
863   {
864     (*poutbuf + offset + sps_pps_size)[0] = 0;
865     (*poutbuf + offset + sps_pps_size)[1] = 0;
866     (*poutbuf + offset + sps_pps_size)[2] = 1;
867   }
868 }
869
870