dc4b24b9baee204887cb8ea5b037f29b36854596
[vuplus_xbmc] / xbmc / utils / BitstreamConverter.h
1 /*
2  *      Copyright (C) 2010-2013 Team XBMC
3  *      http://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 _BITSTREAMCONVERTER_H_
22 #define _BITSTREAMCONVERTER_H_
23
24 #include <stdint.h>
25 #include "DllAvUtil.h"
26 #include "DllAvFormat.h"
27 #include "DllAvFilter.h"
28 #include "DllAvCodec.h"
29
30 typedef struct {
31   uint8_t *buffer, *start;
32   int      offbits, length, oflow;
33 } bits_reader_t;
34
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.
43 #define BS_RB16(x)                          \
44   ((((const uint8_t*)(x))[0] <<  8) |        \
45    ((const uint8_t*)(x)) [1])
46
47 #define BS_RB24(x)                          \
48   ((((const uint8_t*)(x))[0] << 16) |        \
49    (((const uint8_t*)(x))[1] <<  8) |        \
50    ((const uint8_t*)(x))[2])
51
52 #define BS_RB32(x)                          \
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])
57
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; }
63
64 typedef struct
65 {
66   const uint8_t *data;
67   const uint8_t *end;
68   int head;
69   uint64_t cache;
70 } nal_bitstream;
71
72 typedef struct mpeg2_sequence
73 {
74   uint32_t  width;
75   uint32_t  height;
76   float     rate;
77   uint32_t  rate_info;
78   float     ratio;
79   uint32_t  ratio_info;
80 } mpeg2_sequence;
81
82 typedef struct
83 {
84   int profile_idc;
85   int level_idc;
86   int sps_id;
87
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;
94
95   int log2_max_frame_num_minus4;
96   int pic_order_cnt_type;
97   int log2_max_pic_order_cnt_lsb_minus4;
98
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;
103
104   int frame_mbs_only_flag;
105   int mb_adaptive_frame_field_flag;
106
107   int direct_8x8_inference_flag;
108
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;
114 } sps_info_struct;
115
116 class CBitstreamParser
117 {
118 public:
119   CBitstreamParser();
120   ~CBitstreamParser();
121
122   bool              Open();
123   void              Close();
124   bool              FindIdrSlice(const uint8_t *buf, int buf_size);
125
126 protected:
127   const uint8_t*    find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state);
128 };
129
130 class CBitstreamConverter
131 {
132 public:
133   CBitstreamConverter();
134   ~CBitstreamConverter();
135
136   bool              Open(enum AVCodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb);
137   void              Close(void);
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);
143   int               GetExtraSize();
144
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 );
149
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);
152
153 protected:
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);
162
163   typedef struct omx_bitstream_ctx {
164       uint8_t  length_size;
165       uint8_t  first_idr;
166       uint8_t *sps_pps_data;
167       uint32_t size;
168   } omx_bitstream_ctx;
169
170   uint8_t          *m_convertBuffer;
171   int               m_convertSize;
172   uint8_t          *m_inputBuffer;
173   int               m_inputSize;
174
175   uint32_t          m_sps_pps_size;
176   omx_bitstream_ctx m_sps_pps_context;
177   bool              m_convert_bitstream;
178   bool              m_to_annexb;
179
180   uint8_t          *m_extradata;
181   int               m_extrasize;
182   bool              m_convert_3byteTo4byteNALSize;
183   bool              m_convert_bytestream;
184   DllAvUtil        *m_dllAvUtil;
185   DllAvFormat      *m_dllAvFormat;
186   AVCodecID         m_codec;
187 };
188
189 #endif