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/>.
21 #ifndef _BITSTREAMCONVERTER_H_
22 #define _BITSTREAMCONVERTER_H_
25 #include "DllAvUtil.h"
26 #include "DllAvFormat.h"
27 #include "DllAvFilter.h"
28 #include "DllAvCodec.h"
31 uint8_t *buffer, *start;
32 int offbits, length, oflow;
35 ////////////////////////////////////////////////////////////////////////////////////////////
36 // TODO: refactor this so as not to need these ffmpeg routines.
37 // These are not exposed in ffmpeg's API so we dupe them here.
38 // AVC helper functions for muxers,
39 // * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
40 // This is part of FFmpeg
41 // * License as published by the Free Software Foundation; either
42 // * version 2.1 of the License, or (at your option) any later version.
44 ((((const uint8_t*)(x))[0] << 8) | \
45 ((const uint8_t*)(x)) [1])
48 ((((const uint8_t*)(x))[0] << 16) | \
49 (((const uint8_t*)(x))[1] << 8) | \
50 ((const uint8_t*)(x))[2])
53 ((((const uint8_t*)(x))[0] << 24) | \
54 (((const uint8_t*)(x))[1] << 16) | \
55 (((const uint8_t*)(x))[2] << 8) | \
56 ((const uint8_t*)(x))[3])
58 #define BS_WB32(p, d) { \
59 ((uint8_t*)(p))[3] = (d); \
60 ((uint8_t*)(p))[2] = (d) >> 8; \
61 ((uint8_t*)(p))[1] = (d) >> 16; \
62 ((uint8_t*)(p))[0] = (d) >> 24; }
72 typedef struct mpeg2_sequence
88 int chroma_format_idc;
89 int separate_colour_plane_flag;
90 int bit_depth_luma_minus8;
91 int bit_depth_chroma_minus8;
92 int qpprime_y_zero_transform_bypass_flag;
93 int seq_scaling_matrix_present_flag;
95 int log2_max_frame_num_minus4;
96 int pic_order_cnt_type;
97 int log2_max_pic_order_cnt_lsb_minus4;
99 int max_num_ref_frames;
100 int gaps_in_frame_num_value_allowed_flag;
101 int pic_width_in_mbs_minus1;
102 int pic_height_in_map_units_minus1;
104 int frame_mbs_only_flag;
105 int mb_adaptive_frame_field_flag;
107 int direct_8x8_inference_flag;
109 int frame_cropping_flag;
110 int frame_crop_left_offset;
111 int frame_crop_right_offset;
112 int frame_crop_top_offset;
113 int frame_crop_bottom_offset;
116 class CBitstreamParser
124 bool FindIdrSlice(const uint8_t *buf, int buf_size);
127 const uint8_t* find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state);
130 class CBitstreamConverter
133 CBitstreamConverter();
134 ~CBitstreamConverter();
136 bool Open(enum AVCodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb);
138 bool NeedConvert(void) { return m_convert_bitstream; };
139 bool Convert(uint8_t *pData, int iSize);
140 uint8_t* GetConvertBuffer(void);
141 int GetConvertSize();
142 uint8_t* GetExtraData(void);
145 static void bits_reader_set( bits_reader_t *br, uint8_t *buf, int len );
146 static uint32_t read_bits( bits_reader_t *br, int nbits );
147 static void skip_bits( bits_reader_t *br, int nbits );
148 static uint32_t get_bits( bits_reader_t *br, int nbits );
150 static void parseh264_sps(const uint8_t *sps, const uint32_t sps_size, bool *interlaced, int32_t *max_ref_frames);
151 static bool mpeg2_sequence_header(const uint8_t *data, const uint32_t size, mpeg2_sequence *sequence);
154 const int avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size);
155 const int avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size);
156 const int isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len);
157 // bitstream to bytestream (Annex B) conversion support.
158 bool BitstreamConvertInit(void *in_extradata, int in_extrasize);
159 bool BitstreamConvert(uint8_t* pData, int iSize, uint8_t **poutbuf, int *poutbuf_size);
160 void BitstreamAllocAndCopy(uint8_t **poutbuf, int *poutbuf_size,
161 const uint8_t *sps_pps, uint32_t sps_pps_size, const uint8_t *in, uint32_t in_size);
163 typedef struct omx_bitstream_ctx {
166 uint8_t *sps_pps_data;
170 uint8_t *m_convertBuffer;
172 uint8_t *m_inputBuffer;
175 uint32_t m_sps_pps_size;
176 omx_bitstream_ctx m_sps_pps_context;
177 bool m_convert_bitstream;
180 uint8_t *m_extradata;
182 bool m_convert_3byteTo4byteNALSize;
183 bool m_convert_bytestream;
184 DllAvUtil *m_dllAvUtil;
185 DllAvFormat *m_dllAvFormat;