Merge pull request #5042 from koying/fixbitstream
[xbmc.git] / xbmc / utils / BitstreamConverter.cpp
blob96516c7a148aca345ebecf2f5789caec64b982ff
1 /*
2 * Copyright (C) 2010-2013 Team XBMC
3 * http://xbmc.org
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.
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/>.
21 #ifndef UINT16_MAX
22 #define UINT16_MAX (65535U)
23 #endif
25 #include "BitstreamConverter.h"
27 enum {
28 NAL_SLICE=1,
29 NAL_DPA,
30 NAL_DPB,
31 NAL_DPC,
32 NAL_IDR_SLICE,
33 NAL_SEI,
34 NAL_SPS,
35 NAL_PPS,
36 NAL_AUD,
37 NAL_END_SEQUENCE,
38 NAL_END_STREAM,
39 NAL_FILLER_DATA,
40 NAL_SPS_EXT,
41 NAL_AUXILIARY_SLICE=19
44 ////////////////////////////////////////////////////////////////////////////////////////////
45 /////////////////////////////////////////////////////////////////////////////////////////////
46 // GStreamer h264 parser
47 // Copyright (C) 2005 Michal Benes <michal.benes@itonis.tv>
48 // (C) 2008 Wim Taymans <wim.taymans@gmail.com>
49 // gsth264parse.c:
50 // * License as published by the Free Software Foundation; either
51 // * version 2.1 of the License, or (at your option) any later version.
52 static void nal_bs_init(nal_bitstream *bs, const uint8_t *data, size_t size)
54 bs->data = data;
55 bs->end = data + size;
56 bs->head = 0;
57 // fill with something other than 0 to detect
58 // emulation prevention bytes
59 bs->cache = 0xffffffff;
62 static uint32_t nal_bs_read(nal_bitstream *bs, int n)
64 uint32_t res = 0;
65 int shift;
67 if (n == 0)
68 return res;
70 // fill up the cache if we need to
71 while (bs->head < n)
73 uint8_t a_byte;
74 bool check_three_byte;
76 check_three_byte = true;
77 next_byte:
78 if (bs->data >= bs->end)
80 // we're at the end, can't produce more than head number of bits
81 n = bs->head;
82 break;
84 // get the byte, this can be an emulation_prevention_three_byte that we need
85 // to ignore.
86 a_byte = *bs->data++;
87 if (check_three_byte && a_byte == 0x03 && ((bs->cache & 0xffff) == 0))
89 // next byte goes unconditionally to the cache, even if it's 0x03
90 check_three_byte = false;
91 goto next_byte;
93 // shift bytes in cache, moving the head bits of the cache left
94 bs->cache = (bs->cache << 8) | a_byte;
95 bs->head += 8;
98 // bring the required bits down and truncate
99 if ((shift = bs->head - n) > 0)
100 res = bs->cache >> shift;
101 else
102 res = bs->cache;
104 // mask out required bits
105 if (n < 32)
106 res &= (1 << n) - 1;
107 bs->head = shift;
109 return res;
112 static bool nal_bs_eos(nal_bitstream *bs)
114 return (bs->data >= bs->end) && (bs->head == 0);
117 // read unsigned Exp-Golomb code
118 static int nal_bs_read_ue(nal_bitstream *bs)
120 int i = 0;
122 while (nal_bs_read(bs, 1) == 0 && !nal_bs_eos(bs) && i < 32)
123 i++;
125 return ((1 << i) - 1 + nal_bs_read(bs, i));
128 static const uint8_t* avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
130 const uint8_t *a = p + 4 - ((intptr_t)p & 3);
132 for (end -= 3; p < a && p < end; p++)
134 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
135 return p;
138 for (end -= 3; p < end; p += 4)
140 uint32_t x = *(const uint32_t*)p;
141 if ((x - 0x01010101) & (~x) & 0x80808080) // generic
143 if (p[1] == 0)
145 if (p[0] == 0 && p[2] == 1)
146 return p;
147 if (p[2] == 0 && p[3] == 1)
148 return p+1;
150 if (p[3] == 0)
152 if (p[2] == 0 && p[4] == 1)
153 return p+2;
154 if (p[4] == 0 && p[5] == 1)
155 return p+3;
160 for (end += 3; p < end; p++)
162 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
163 return p;
166 return end + 3;
169 static const uint8_t* avc_find_startcode(const uint8_t *p, const uint8_t *end)
171 const uint8_t *out = avc_find_startcode_internal(p, end);
172 if (p<out && out<end && !out[-1])
173 out--;
174 return out;
177 ////////////////////////////////////////////////////////////////////////////////////////////
178 /////////////////////////////////////////////////////////////////////////////////////////////
179 CBitstreamParser::CBitstreamParser()
183 CBitstreamParser::~CBitstreamParser()
185 Close();
188 bool CBitstreamParser::Open()
190 return true;
193 void CBitstreamParser::Close()
197 const uint8_t* CBitstreamParser::find_start_code(const uint8_t *p,
198 const uint8_t *end, uint32_t *state)
200 assert(p <= end);
201 if (p >= end)
202 return end;
204 for (int i = 0; i < 3; i++) {
205 uint32_t tmp = *state << 8;
206 *state = tmp + *(p++);
207 if (tmp == 0x100 || p == end)
208 return p;
211 while (p < end) {
212 if (p[-1] > 1 ) p += 3;
213 else if (p[-2] ) p += 2;
214 else if (p[-3]|(p[-1]-1)) p++;
215 else {
216 p++;
217 break;
221 p = FFMIN(p, end) - 4;
222 *state = BS_RB32(p);
224 return p + 4;
227 bool CBitstreamParser::FindIdrSlice(const uint8_t *buf, int buf_size)
229 if (!buf)
230 return false;
232 bool rtn = false;
233 uint32_t state = -1;
234 const uint8_t *buf_end = buf + buf_size;
236 for(;;)
238 buf = find_start_code(buf, buf_end, &state);
239 if (buf >= buf_end)
241 //CLog::Log(LOGDEBUG, "FindIdrSlice: buf(%p), buf_end(%p)", buf, buf_end);
242 break;
245 --buf;
246 int src_length = buf_end - buf;
247 switch (state & 0x1f)
249 default:
250 CLog::Log(LOGDEBUG, "FindIdrSlice: found nal_type(%d)", state & 0x1f);
251 break;
252 case NAL_SLICE:
253 CLog::Log(LOGDEBUG, "FindIdrSlice: found NAL_SLICE");
254 break;
255 case NAL_IDR_SLICE:
256 CLog::Log(LOGDEBUG, "FindIdrSlice: found NAL_IDR_SLICE");
257 rtn = true;
258 break;
259 case NAL_SEI:
260 CLog::Log(LOGDEBUG, "FindIdrSlice: found NAL_SEI");
261 break;
262 case NAL_SPS:
263 CLog::Log(LOGDEBUG, "FindIdrSlice: found NAL_SPS");
264 break;
265 case NAL_PPS:
266 CLog::Log(LOGDEBUG, "FindIdrSlice: found NAL_PPS");
267 break;
269 buf += src_length;
272 return rtn;
275 ////////////////////////////////////////////////////////////////////////////////////////////
276 /////////////////////////////////////////////////////////////////////////////////////////////
277 CBitstreamConverter::CBitstreamConverter()
279 m_convert_bitstream = false;
280 m_convertBuffer = NULL;
281 m_convertSize = 0;
282 m_inputBuffer = NULL;
283 m_inputSize = 0;
284 m_to_annexb = false;
285 m_extradata = NULL;
286 m_extrasize = 0;
287 m_convert_3byteTo4byteNALSize = false;
288 m_dllAvUtil = NULL;
289 m_dllAvFormat = NULL;
290 m_convert_bytestream = false;
291 m_sps_pps_context.sps_pps_data = NULL;
294 CBitstreamConverter::~CBitstreamConverter()
296 Close();
299 bool CBitstreamConverter::Open(enum AVCodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb)
301 m_to_annexb = to_annexb;
303 m_codec = codec;
304 switch(m_codec)
306 case AV_CODEC_ID_H264:
307 if (in_extrasize < 7 || in_extradata == NULL)
309 CLog::Log(LOGERROR, "CBitstreamConverter::Open avcC data too small or missing");
310 return false;
312 // valid avcC data (bitstream) always starts with the value 1 (version)
313 if(m_to_annexb)
315 if ( in_extradata[0] == 1 )
317 CLog::Log(LOGINFO, "CBitstreamConverter::Open bitstream to annexb init");
318 m_dllAvUtil = new DllAvUtil;
319 if (!m_dllAvUtil->Load())
320 return false;
322 m_extrasize = in_extrasize;
323 m_extradata = (uint8_t*)m_dllAvUtil->av_malloc(in_extrasize);
324 memcpy(m_extradata, in_extradata, in_extrasize);
325 m_convert_bitstream = BitstreamConvertInit(m_extradata, m_extrasize);
326 return true;
329 else
331 m_dllAvUtil = new DllAvUtil;
332 if (!m_dllAvUtil->Load())
333 return false;
335 // valid avcC atom data always starts with the value 1 (version)
336 if ( in_extradata[0] != 1 )
338 if ( (in_extradata[0] == 0 && in_extradata[1] == 0 && in_extradata[2] == 0 && in_extradata[3] == 1) ||
339 (in_extradata[0] == 0 && in_extradata[1] == 0 && in_extradata[2] == 1) )
341 CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init");
342 // video content is from x264 or from bytestream h264 (AnnexB format)
343 // NAL reformating to bitstream format needed
344 m_dllAvFormat = new DllAvFormat;
345 if (!m_dllAvFormat->Load())
346 return false;
348 AVIOContext *pb;
349 if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
350 return false;
351 m_convert_bytestream = true;
352 // create a valid avcC atom data from ffmpeg's extradata
353 isom_write_avcc(pb, in_extradata, in_extrasize);
354 // unhook from ffmpeg's extradata
355 in_extradata = NULL;
356 // extract the avcC atom data into extradata then write it into avcCData for VDADecoder
357 in_extrasize = m_dllAvFormat->avio_close_dyn_buf(pb, &in_extradata);
358 // make a copy of extradata contents
359 m_extradata = (uint8_t *)m_dllAvUtil->av_malloc(in_extrasize);
360 memcpy(m_extradata, in_extradata, in_extrasize);
361 m_extrasize = in_extrasize;
362 // done with the converted extradata, we MUST free using av_free
363 m_dllAvUtil->av_free(in_extradata);
364 return true;
366 else
368 CLog::Log(LOGNOTICE, "CBitstreamConverter::Open invalid avcC atom data");
369 return false;
372 else
374 if (in_extradata[4] == 0xFE)
376 CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init 3 byte to 4 byte nal");
377 // video content is from so silly encoder that think 3 byte NAL sizes
378 // are valid, setup to convert 3 byte NAL sizes to 4 byte.
379 m_dllAvFormat = new DllAvFormat;
380 if (!m_dllAvFormat->Load())
381 return false;
383 in_extradata[4] = 0xFF;
384 m_convert_3byteTo4byteNALSize = true;
386 m_extradata = (uint8_t *)m_dllAvUtil->av_malloc(in_extrasize);
387 memcpy(m_extradata, in_extradata, in_extrasize);
388 m_extrasize = in_extrasize;
389 return true;
392 // valid avcC atom
393 m_extradata = (uint8_t*)m_dllAvUtil->av_malloc(in_extrasize);
394 memcpy(m_extradata, in_extradata, in_extrasize);
395 m_extrasize = in_extrasize;
396 return true;
398 return false;
399 break;
400 default:
401 return false;
402 break;
404 return false;
407 void CBitstreamConverter::Close(void)
409 if (m_sps_pps_context.sps_pps_data)
410 m_dllAvUtil->av_free(m_sps_pps_context.sps_pps_data), m_sps_pps_context.sps_pps_data = NULL;
412 if (m_convertBuffer)
413 m_dllAvUtil->av_free(m_convertBuffer), m_convertBuffer = NULL;
414 m_convertSize = 0;
416 if (m_extradata)
417 m_dllAvUtil->av_free(m_extradata), m_extradata = NULL;
418 m_extrasize = 0;
420 m_inputSize = 0;
421 m_inputBuffer = NULL;
423 m_convert_bitstream = false;
424 m_convert_bytestream = false;
425 m_convert_3byteTo4byteNALSize = false;
427 if (m_dllAvUtil)
428 delete m_dllAvUtil, m_dllAvUtil = NULL;
429 if (m_dllAvFormat)
430 delete m_dllAvFormat, m_dllAvFormat = NULL;
433 bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
435 if (m_convertBuffer)
437 m_dllAvUtil->av_free(m_convertBuffer);
438 m_convertBuffer = NULL;
440 m_inputSize = 0;
441 m_convertSize = 0;
442 m_inputBuffer = NULL;
444 if (pData)
446 if (m_codec == AV_CODEC_ID_H264)
448 if (m_to_annexb)
450 int demuxer_bytes = iSize;
451 uint8_t *demuxer_content = pData;
453 if (m_convert_bitstream)
455 // convert demuxer packet from bitstream to bytestream (AnnexB)
456 int bytestream_size = 0;
457 uint8_t *bytestream_buff = NULL;
459 BitstreamConvert(demuxer_content, demuxer_bytes, &bytestream_buff, &bytestream_size);
460 if (bytestream_buff && (bytestream_size > 0))
462 m_convertSize = bytestream_size;
463 m_convertBuffer = bytestream_buff;
464 return true;
466 else
468 m_convertSize = 0;
469 m_convertBuffer = NULL;
470 CLog::Log(LOGERROR, "CBitstreamConverter::Convert: error converting.");
471 return false;
474 else
476 m_inputSize = iSize;
477 m_inputBuffer = pData;
478 return true;
481 else
483 m_inputSize = iSize;
484 m_inputBuffer = pData;
486 if (m_convert_bytestream)
488 if(m_convertBuffer)
490 m_dllAvUtil->av_free(m_convertBuffer);
491 m_convertBuffer = NULL;
493 m_convertSize = 0;
495 // convert demuxer packet from bytestream (AnnexB) to bitstream
496 AVIOContext *pb;
498 if(m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
500 return false;
502 m_convertSize = avc_parse_nal_units(pb, pData, iSize);
503 m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
505 else if (m_convert_3byteTo4byteNALSize)
507 if(m_convertBuffer)
509 m_dllAvUtil->av_free(m_convertBuffer);
510 m_convertBuffer = NULL;
512 m_convertSize = 0;
514 // convert demuxer packet from 3 byte NAL sizes to 4 byte
515 AVIOContext *pb;
516 if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
517 return false;
519 uint32_t nal_size;
520 uint8_t *end = pData + iSize;
521 uint8_t *nal_start = pData;
522 while (nal_start < end)
524 nal_size = BS_RB24(nal_start);
525 m_dllAvFormat->avio_wb16(pb, nal_size);
526 nal_start += 3;
527 m_dllAvFormat->avio_write(pb, nal_start, nal_size);
528 nal_start += nal_size;
531 m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
533 return true;
538 return false;
542 uint8_t *CBitstreamConverter::GetConvertBuffer()
544 if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
545 return m_convertBuffer;
546 else
547 return m_inputBuffer;
550 int CBitstreamConverter::GetConvertSize()
552 if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
553 return m_convertSize;
554 else
555 return m_inputSize;
558 uint8_t *CBitstreamConverter::GetExtraData()
560 if(m_convert_bitstream)
561 return m_sps_pps_context.sps_pps_data;
562 else
563 return m_extradata;
565 int CBitstreamConverter::GetExtraSize()
567 if(m_convert_bitstream)
568 return m_sps_pps_context.size;
569 else
570 return m_extrasize;
573 bool CBitstreamConverter::BitstreamConvertInit(void *in_extradata, int in_extrasize)
575 // based on h264_mp4toannexb_bsf.c (ffmpeg)
576 // which is Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
577 // and Licensed GPL 2.1 or greater
579 m_sps_pps_size = 0;
580 m_sps_pps_context.sps_pps_data = NULL;
582 // nothing to filter
583 if (!in_extradata || in_extrasize < 6)
584 return false;
586 uint16_t unit_size;
587 uint32_t total_size = 0;
588 uint8_t *out = NULL, unit_nb, sps_done = 0, sps_seen = 0, pps_seen = 0;
589 const uint8_t *extradata = (uint8_t*)in_extradata + 4;
590 static const uint8_t nalu_header[4] = {0, 0, 0, 1};
592 // retrieve length coded size
593 m_sps_pps_context.length_size = (*extradata++ & 0x3) + 1;
595 // retrieve sps and pps unit(s)
596 unit_nb = *extradata++ & 0x1f; // number of sps unit(s)
597 if (!unit_nb)
599 goto pps;
601 else
603 sps_seen = 1;
606 while (unit_nb--)
608 void *tmp;
610 unit_size = extradata[0] << 8 | extradata[1];
611 total_size += unit_size + 4;
613 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE ||
614 (extradata + 2 + unit_size) > ((uint8_t*)in_extradata + in_extrasize)) {
615 m_dllAvUtil->av_free(out);
616 return false;
618 tmp = m_dllAvUtil->av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE);
619 if (!tmp) {
620 m_dllAvUtil->av_free(out);
621 return false;
623 out = (uint8_t*)tmp;
624 memcpy(out + total_size - unit_size - 4, nalu_header, 4);
625 memcpy(out + total_size - unit_size, extradata + 2, unit_size);
626 extradata += 2 + unit_size;
628 pps:
629 if (!unit_nb && !sps_done++)
631 unit_nb = *extradata++; // number of pps unit(s)
632 if (unit_nb)
633 pps_seen = 1;
637 if (out)
638 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
640 if (!sps_seen)
641 CLog::Log(LOGDEBUG, "SPS NALU missing or invalid. The resulting stream may not play");
642 if (!pps_seen)
643 CLog::Log(LOGDEBUG, "PPS NALU missing or invalid. The resulting stream may not play");
645 m_sps_pps_context.sps_pps_data = out;
646 m_sps_pps_context.size = total_size;
647 m_sps_pps_context.first_idr = 1;
648 m_sps_pps_context.idr_sps_pps_seen = 0;
650 return true;
653 bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **poutbuf, int *poutbuf_size)
655 // based on h264_mp4toannexb_bsf.c (ffmpeg)
656 // which is Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
657 // and Licensed GPL 2.1 or greater
659 int i;
660 uint8_t *buf = pData;
661 uint32_t buf_size = iSize;
662 uint8_t unit_type;
663 int32_t nal_size;
664 uint32_t cumul_size = 0;
665 const uint8_t *buf_end = buf + buf_size;
669 if (buf + m_sps_pps_context.length_size > buf_end)
670 goto fail;
672 for (nal_size = 0, i = 0; i < m_sps_pps_context.length_size; i++)
673 nal_size = (nal_size << 8) | buf[i];
675 buf += m_sps_pps_context.length_size;
676 unit_type = *buf & 0x1f;
678 if (buf + nal_size > buf_end || nal_size < 0)
679 goto fail;
681 // Don't add sps/pps if the unit already contain them
682 if (m_sps_pps_context.first_idr && (unit_type == 7 || unit_type == 8))
683 m_sps_pps_context.idr_sps_pps_seen = 1;
685 // prepend only to the first access unit of an IDR picture, if no sps/pps already present
686 if (m_sps_pps_context.first_idr && unit_type == 5 && !m_sps_pps_context.idr_sps_pps_seen)
688 BitstreamAllocAndCopy(poutbuf, poutbuf_size,
689 m_sps_pps_context.sps_pps_data, m_sps_pps_context.size, buf, nal_size);
690 m_sps_pps_context.first_idr = 0;
692 else
694 BitstreamAllocAndCopy(poutbuf, poutbuf_size, NULL, 0, buf, nal_size);
695 if (!m_sps_pps_context.first_idr && unit_type == 1)
697 m_sps_pps_context.first_idr = 1;
698 m_sps_pps_context.idr_sps_pps_seen = 0;
702 buf += nal_size;
703 cumul_size += nal_size + m_sps_pps_context.length_size;
704 } while (cumul_size < buf_size);
706 return true;
708 fail:
709 m_dllAvUtil->av_free(*poutbuf), *poutbuf = NULL;
710 *poutbuf_size = 0;
711 return false;
714 void CBitstreamConverter::BitstreamAllocAndCopy( uint8_t **poutbuf, int *poutbuf_size,
715 const uint8_t *sps_pps, uint32_t sps_pps_size, const uint8_t *in, uint32_t in_size)
717 // based on h264_mp4toannexb_bsf.c (ffmpeg)
718 // which is Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
719 // and Licensed GPL 2.1 or greater
721 uint32_t offset = *poutbuf_size;
722 uint8_t nal_header_size = offset ? 3 : 4;
723 void *tmp;
725 *poutbuf_size += sps_pps_size + in_size + nal_header_size;
726 tmp = m_dllAvUtil->av_realloc(*poutbuf, *poutbuf_size);
727 if (!tmp)
728 return;
729 *poutbuf = (uint8_t*)tmp;
730 if (sps_pps)
731 memcpy(*poutbuf + offset, sps_pps, sps_pps_size);
733 memcpy(*poutbuf + sps_pps_size + nal_header_size + offset, in, in_size);
734 if (!offset)
736 BS_WB32(*poutbuf + sps_pps_size, 1);
738 else
740 (*poutbuf + offset + sps_pps_size)[0] = 0;
741 (*poutbuf + offset + sps_pps_size)[1] = 0;
742 (*poutbuf + offset + sps_pps_size)[2] = 1;
746 const int CBitstreamConverter::avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
748 const uint8_t *p = buf_in;
749 const uint8_t *end = p + size;
750 const uint8_t *nal_start, *nal_end;
752 size = 0;
753 nal_start = avc_find_startcode(p, end);
755 for (;;) {
756 while (nal_start < end && !*(nal_start++));
757 if (nal_start == end)
758 break;
760 nal_end = avc_find_startcode(nal_start, end);
761 m_dllAvFormat->avio_wb32(pb, nal_end - nal_start);
762 m_dllAvFormat->avio_write(pb, nal_start, nal_end - nal_start);
763 size += 4 + nal_end - nal_start;
764 nal_start = nal_end;
766 return size;
769 const int CBitstreamConverter::avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
771 AVIOContext *pb;
772 int ret = m_dllAvFormat->avio_open_dyn_buf(&pb);
773 if (ret < 0)
774 return ret;
776 avc_parse_nal_units(pb, buf_in, *size);
778 m_dllAvUtil->av_freep(buf);
779 *size = m_dllAvFormat->avio_close_dyn_buf(pb, buf);
780 return 0;
783 const int CBitstreamConverter::isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
785 // extradata from bytestream h264, convert to avcC atom data for bitstream
786 if (len > 6)
788 /* check for h264 start code */
789 if (BS_RB32(data) == 0x00000001 || BS_RB24(data) == 0x000001)
791 uint8_t *buf=NULL, *end, *start;
792 uint32_t sps_size=0, pps_size=0;
793 uint8_t *sps=0, *pps=0;
795 int ret = avc_parse_nal_units_buf(data, &buf, &len);
796 if (ret < 0)
797 return ret;
798 start = buf;
799 end = buf + len;
801 /* look for sps and pps */
802 while (end - buf > 4)
804 uint32_t size;
805 uint8_t nal_type;
806 size = FFMIN(BS_RB32(buf), end - buf - 4);
807 buf += 4;
808 nal_type = buf[0] & 0x1f;
809 if (nal_type == 7) /* SPS */
811 sps = buf;
812 sps_size = size;
814 else if (nal_type == 8) /* PPS */
816 pps = buf;
817 pps_size = size;
819 buf += size;
821 if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
822 assert(0);
824 m_dllAvFormat->avio_w8(pb, 1); /* version */
825 m_dllAvFormat->avio_w8(pb, sps[1]); /* profile */
826 m_dllAvFormat->avio_w8(pb, sps[2]); /* profile compat */
827 m_dllAvFormat->avio_w8(pb, sps[3]); /* level */
828 m_dllAvFormat->avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
829 m_dllAvFormat->avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
831 m_dllAvFormat->avio_wb16(pb, sps_size);
832 m_dllAvFormat->avio_write(pb, sps, sps_size);
833 if (pps)
835 m_dllAvFormat->avio_w8(pb, 1); /* number of pps */
836 m_dllAvFormat->avio_wb16(pb, pps_size);
837 m_dllAvFormat->avio_write(pb, pps, pps_size);
839 m_dllAvUtil->av_free(start);
841 else
843 m_dllAvFormat->avio_write(pb, data, len);
846 return 0;
849 void CBitstreamConverter::bits_reader_set( bits_reader_t *br, uint8_t *buf, int len )
851 br->buffer = br->start = buf;
852 br->offbits = 0;
853 br->length = len;
854 br->oflow = 0;
857 uint32_t CBitstreamConverter::read_bits( bits_reader_t *br, int nbits )
859 int i, nbytes;
860 uint32_t ret = 0;
861 uint8_t *buf;
863 buf = br->buffer;
864 nbytes = (br->offbits + nbits)/8;
865 if ( ((br->offbits + nbits) %8 ) > 0 )
866 nbytes++;
867 if ( (buf + nbytes) > (br->start + br->length) ) {
868 br->oflow = 1;
869 return 0;
871 for ( i=0; i<nbytes; i++ )
872 ret += buf[i]<<((nbytes-i-1)*8);
873 i = (4-nbytes)*8+br->offbits;
874 ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-br->offbits);
876 br->offbits += nbits;
877 br->buffer += br->offbits / 8;
878 br->offbits %= 8;
880 return ret;
883 void CBitstreamConverter::skip_bits( bits_reader_t *br, int nbits )
885 br->offbits += nbits;
886 br->buffer += br->offbits / 8;
887 br->offbits %= 8;
888 if ( br->buffer > (br->start + br->length) ) {
889 br->oflow = 1;
893 uint32_t CBitstreamConverter::get_bits( bits_reader_t *br, int nbits )
895 int i, nbytes;
896 uint32_t ret = 0;
897 uint8_t *buf;
899 buf = br->buffer;
900 nbytes = (br->offbits + nbits)/8;
901 if ( ((br->offbits + nbits) %8 ) > 0 )
902 nbytes++;
903 if ( (buf + nbytes) > (br->start + br->length) ) {
904 br->oflow = 1;
905 return 0;
907 for ( i=0; i<nbytes; i++ )
908 ret += buf[i]<<((nbytes-i-1)*8);
909 i = (4-nbytes)*8+br->offbits;
910 ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-br->offbits);
912 return ret;
915 ////////////////////////////////////////////////////////////////////////////////////////////
916 /////////////////////////////////////////////////////////////////////////////////////////////
917 void CBitstreamConverter::init_bits_writer(bits_writer_t *s, uint8_t *buffer, int buffer_size, int writer_le)
919 if (buffer_size < 0)
921 buffer_size = 0;
922 buffer = NULL;
925 s->size_in_bits = 8 * buffer_size;
926 s->buf = buffer;
927 s->buf_end = s->buf + buffer_size;
928 s->buf_ptr = s->buf;
929 s->bit_left = 32;
930 s->bit_buf = 0;
931 s->writer_le = writer_le;
934 void CBitstreamConverter::write_bits(bits_writer_t *s, int n, unsigned int value)
936 // Write up to 32 bits into a bitstream.
937 unsigned int bit_buf;
938 int bit_left;
940 if (n == 32)
942 // Write exactly 32 bits into a bitstream.
943 // danger, recursion in play.
944 int lo = value & 0xffff;
945 int hi = value >> 16;
946 if (s->writer_le)
948 write_bits(s, 16, lo);
949 write_bits(s, 16, hi);
951 else
953 write_bits(s, 16, hi);
954 write_bits(s, 16, lo);
956 return;
959 bit_buf = s->bit_buf;
960 bit_left = s->bit_left;
962 if (s->writer_le)
964 bit_buf |= value << (32 - bit_left);
965 if (n >= bit_left) {
966 BS_WL32(s->buf_ptr, bit_buf);
967 s->buf_ptr += 4;
968 bit_buf = (bit_left == 32) ? 0 : value >> bit_left;
969 bit_left += 32;
971 bit_left -= n;
973 else
975 if (n < bit_left) {
976 bit_buf = (bit_buf << n) | value;
977 bit_left -= n;
978 } else {
979 bit_buf <<= bit_left;
980 bit_buf |= value >> (n - bit_left);
981 BS_WB32(s->buf_ptr, bit_buf);
982 s->buf_ptr += 4;
983 bit_left += 32 - n;
984 bit_buf = value;
988 s->bit_buf = bit_buf;
989 s->bit_left = bit_left;
992 void CBitstreamConverter::skip_bits(bits_writer_t *s, int n)
994 // Skip the given number of bits.
995 // Must only be used if the actual values in the bitstream do not matter.
996 // If n is 0 the behavior is undefined.
997 s->bit_left -= n;
998 s->buf_ptr -= 4 * (s->bit_left >> 5);
999 s->bit_left &= 31;
1002 void CBitstreamConverter::flush_bits(bits_writer_t *s)
1004 if (!s->writer_le)
1006 if (s->bit_left < 32)
1007 s->bit_buf <<= s->bit_left;
1009 while (s->bit_left < 32)
1012 if (s->writer_le)
1014 *s->buf_ptr++ = s->bit_buf;
1015 s->bit_buf >>= 8;
1017 else
1019 *s->buf_ptr++ = s->bit_buf >> 24;
1020 s->bit_buf <<= 8;
1022 s->bit_left += 8;
1024 s->bit_left = 32;
1025 s->bit_buf = 0;
1028 ////////////////////////////////////////////////////////////////////////////////////////////
1029 /////////////////////////////////////////////////////////////////////////////////////////////
1030 bool CBitstreamConverter::mpeg2_sequence_header(const uint8_t *data, const uint32_t size, mpeg2_sequence *sequence)
1032 // parse nal's until a sequence_header_code is found
1033 // and return the width, height, aspect ratio and frame rate if changed.
1034 bool changed = false;
1036 if (!data)
1037 return changed;
1039 const uint8_t *p = data;
1040 const uint8_t *end = p + size;
1041 const uint8_t *nal_start, *nal_end;
1043 nal_start = avc_find_startcode(p, end);
1044 while (nal_start < end)
1046 while (!*(nal_start++));
1047 nal_end = avc_find_startcode(nal_start, end);
1048 if (*nal_start == 0xB3)
1050 nal_bitstream bs;
1051 nal_bs_init(&bs, nal_start, end - nal_start);
1053 // sequence_header_code
1054 nal_bs_read(&bs, 8);
1056 // width
1057 // nal_start + 12 bits == horizontal_size_value
1058 uint32_t width = nal_bs_read(&bs, 12);
1059 if (width != sequence->width)
1061 changed = true;
1062 sequence->width = width;
1064 // height
1065 // nal_start + 24 bits == vertical_size_value
1066 uint32_t height = nal_bs_read(&bs, 12);
1067 if (height != sequence->height)
1069 changed = true;
1070 sequence->height = height;
1073 // aspect ratio
1074 // nal_start + 28 bits == aspect_ratio_information
1075 float ratio = sequence->ratio;
1076 uint32_t ratio_info = nal_bs_read(&bs, 4);
1077 switch(ratio_info)
1079 case 0x01:
1080 ratio = 1.0;
1081 break;
1082 default:
1083 case 0x02:
1084 ratio = 4.0/3.0;
1085 break;
1086 case 0x03:
1087 ratio = 16.0/9.0;
1088 break;
1089 case 0x04:
1090 ratio = 2.21;
1091 break;
1093 if (ratio_info != sequence->ratio_info)
1095 changed = true;
1096 sequence->ratio = ratio;
1097 sequence->ratio_info = ratio_info;
1100 // frame rate
1101 // nal_start + 32 bits == frame_rate_code
1102 float rate = sequence->rate;
1103 uint32_t rate_info = nal_bs_read(&bs, 4);
1104 switch(rate_info)
1106 default:
1107 case 0x01:
1108 rate = 24000.0 / 1001.0;
1109 break;
1110 case 0x02:
1111 rate = 24000.0 / 1000.0;
1112 break;
1113 case 0x03:
1114 rate = 25000.0 / 1000.0;
1115 break;
1116 case 0x04:
1117 rate = 30000.0 / 1001.0;
1118 break;
1119 case 0x05:
1120 rate = 30000.0 / 1000.0;
1121 break;
1122 case 0x06:
1123 rate = 50000.0 / 1000.0;
1124 break;
1125 case 0x07:
1126 rate = 60000.0 / 1001.0;
1127 break;
1128 case 0x08:
1129 rate = 60000.0 / 1000.0;
1130 break;
1132 if (rate_info != sequence->rate_info)
1134 changed = true;
1135 sequence->rate = rate;
1136 sequence->rate_info = rate_info;
1139 if (changed)
1141 CLog::Log(LOGDEBUG, "CBitstreamConverter::mpeg2_sequence_header: "
1142 "width(%d), height(%d), ratio(%f), rate(%f)", width, height, ratio, rate);
1146 nal_start = nal_end;
1149 return changed;
1152 void CBitstreamConverter::parseh264_sps(const uint8_t *sps, const uint32_t sps_size, bool *interlaced, int32_t *max_ref_frames)
1154 nal_bitstream bs;
1155 sps_info_struct sps_info;
1157 nal_bs_init(&bs, sps, sps_size);
1159 sps_info.profile_idc = nal_bs_read(&bs, 8);
1160 nal_bs_read(&bs, 1); // constraint_set0_flag
1161 nal_bs_read(&bs, 1); // constraint_set1_flag
1162 nal_bs_read(&bs, 1); // constraint_set2_flag
1163 nal_bs_read(&bs, 1); // constraint_set3_flag
1164 nal_bs_read(&bs, 4); // reserved
1165 sps_info.level_idc = nal_bs_read(&bs, 8);
1166 sps_info.sps_id = nal_bs_read_ue(&bs);
1168 if (sps_info.profile_idc == 100 ||
1169 sps_info.profile_idc == 110 ||
1170 sps_info.profile_idc == 122 ||
1171 sps_info.profile_idc == 244 ||
1172 sps_info.profile_idc == 44 ||
1173 sps_info.profile_idc == 83 ||
1174 sps_info.profile_idc == 86)
1176 sps_info.chroma_format_idc = nal_bs_read_ue(&bs);
1177 if (sps_info.chroma_format_idc == 3)
1178 sps_info.separate_colour_plane_flag = nal_bs_read(&bs, 1);
1179 sps_info.bit_depth_luma_minus8 = nal_bs_read_ue(&bs);
1180 sps_info.bit_depth_chroma_minus8 = nal_bs_read_ue(&bs);
1181 sps_info.qpprime_y_zero_transform_bypass_flag = nal_bs_read(&bs, 1);
1183 sps_info.seq_scaling_matrix_present_flag = nal_bs_read (&bs, 1);
1184 if (sps_info.seq_scaling_matrix_present_flag)
1186 /* TODO: unfinished */
1189 sps_info.log2_max_frame_num_minus4 = nal_bs_read_ue(&bs);
1190 if (sps_info.log2_max_frame_num_minus4 > 12)
1191 { // must be between 0 and 12
1192 return;
1194 sps_info.pic_order_cnt_type = nal_bs_read_ue(&bs);
1195 if (sps_info.pic_order_cnt_type == 0)
1197 sps_info.log2_max_pic_order_cnt_lsb_minus4 = nal_bs_read_ue(&bs);
1199 else if (sps_info.pic_order_cnt_type == 1)
1200 { // TODO: unfinished
1202 delta_pic_order_always_zero_flag = gst_nal_bs_read (bs, 1);
1203 offset_for_non_ref_pic = gst_nal_bs_read_se (bs);
1204 offset_for_top_to_bottom_field = gst_nal_bs_read_se (bs);
1206 num_ref_frames_in_pic_order_cnt_cycle = gst_nal_bs_read_ue (bs);
1207 for( i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
1208 offset_for_ref_frame[i] = gst_nal_bs_read_se (bs);
1212 sps_info.max_num_ref_frames = nal_bs_read_ue(&bs);
1213 sps_info.gaps_in_frame_num_value_allowed_flag = nal_bs_read(&bs, 1);
1214 sps_info.pic_width_in_mbs_minus1 = nal_bs_read_ue(&bs);
1215 sps_info.pic_height_in_map_units_minus1 = nal_bs_read_ue(&bs);
1217 sps_info.frame_mbs_only_flag = nal_bs_read(&bs, 1);
1218 if (!sps_info.frame_mbs_only_flag)
1219 sps_info.mb_adaptive_frame_field_flag = nal_bs_read(&bs, 1);
1221 sps_info.direct_8x8_inference_flag = nal_bs_read(&bs, 1);
1223 sps_info.frame_cropping_flag = nal_bs_read(&bs, 1);
1224 if (sps_info.frame_cropping_flag)
1226 sps_info.frame_crop_left_offset = nal_bs_read_ue(&bs);
1227 sps_info.frame_crop_right_offset = nal_bs_read_ue(&bs);
1228 sps_info.frame_crop_top_offset = nal_bs_read_ue(&bs);
1229 sps_info.frame_crop_bottom_offset = nal_bs_read_ue(&bs);
1232 *interlaced = !sps_info.frame_mbs_only_flag;
1233 *max_ref_frames = sps_info.max_num_ref_frames;