Skip to content
This repository
Browse code

refactor BitstreamConverter

  • Loading branch information...
commit 6191e523c53ce671e9a20e63bf2ba72eab1ee337 1 parent 309e015
authored August 22, 2013
1,047  xbmc/utils/BitstreamConverter.cpp
@@ -24,71 +24,22 @@
24 24
 
25 25
 #include "BitstreamConverter.h"
26 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  
-}
  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
  42
+};
92 43
 
93 44
 ////////////////////////////////////////////////////////////////////////////////////////////
94 45
 /////////////////////////////////////////////////////////////////////////////////////////////
@@ -98,7 +49,7 @@ uint32_t CBitstreamConverter::get_bits(bits_reader_t *br, int nbits)
98 49
 // gsth264parse.c:
99 50
 //  * License as published by the Free Software Foundation; either
100 51
 //  * 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)
  52
+static void nal_bs_init(nal_bitstream *bs, const uint8_t *data, size_t size)
102 53
 {
103 54
   bs->data = data;
104 55
   bs->end  = data + size;
@@ -108,7 +59,7 @@ void CBitstreamConverter::nal_bs_init(nal_bitstream *bs, const uint8_t *data, si
108 59
   bs->cache = 0xffffffff;
109 60
 }
110 61
 
111  
-uint32_t CBitstreamConverter::nal_bs_read(nal_bitstream *bs, int n)
  62
+static uint32_t nal_bs_read(nal_bitstream *bs, int n)
112 63
 {
113 64
   uint32_t res = 0;
114 65
   int shift;
@@ -158,13 +109,13 @@ uint32_t CBitstreamConverter::nal_bs_read(nal_bitstream *bs, int n)
158 109
   return res;
159 110
 }
160 111
 
161  
-bool CBitstreamConverter::nal_bs_eos(nal_bitstream *bs)
  112
+static bool nal_bs_eos(nal_bitstream *bs)
162 113
 {
163 114
   return (bs->data >= bs->end) && (bs->head == 0);
164 115
 }
165 116
 
166 117
 // read unsigned Exp-Golomb code
167  
-int CBitstreamConverter::nal_bs_read_ue(nal_bitstream *bs)
  118
+static int nal_bs_read_ue(nal_bitstream *bs)
168 119
 {
169 120
   int i = 0;
170 121
 
@@ -174,367 +125,155 @@ int CBitstreamConverter::nal_bs_read_ue(nal_bitstream *bs)
174 125
   return ((1 << i) - 1 + nal_bs_read(bs, i));
175 126
 }
176 127
 
177  
-bool CBitstreamConverter::mpeg2_sequence_header(const uint8_t *data, const uint32_t size, mpeg2_sequence *sequence)
  128
+static const uint8_t* avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
178 129
 {
179  
-  // parse nal's until a sequence_header_code is found
180  
-  // and return the width, height, aspect ratio and frame rate if changed.
181  
-  bool changed = false;
182  
-
183  
-  if (!data)
184  
-    return changed;
  130
+  const uint8_t *a = p + 4 - ((intptr_t)p & 3);
185 131
 
186  
-  const uint8_t *p = data;
187  
-  const uint8_t *end = p + size;
188  
-  const uint8_t *nal_start, *nal_end;
  132
+  for (end -= 3; p < a && p < end; p++)
  133
+  {
  134
+    if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  135
+      return p;
  136
+  }
189 137
 
190  
-  nal_start = avc_find_startcode(p, end);
191  
-  while (nal_start < end)
  138
+  for (end -= 3; p < end; p += 4)
192 139
   {
193  
-    while (!*(nal_start++));
194  
-    nal_end = avc_find_startcode(nal_start, end);
195  
-    if (*nal_start == 0xB3)
  140
+    uint32_t x = *(const uint32_t*)p;
  141
+    if ((x - 0x01010101) & (~x) & 0x80808080) // generic
196 142
     {
197  
-      nal_bitstream bs;
198  
-      nal_bs_init(&bs, nal_start, end - nal_start);
199  
-
200  
-      // sequence_header_code
201  
-      nal_bs_read(&bs, 8);
202  
-
203  
-      // width
204  
-      // nal_start + 12 bits == horizontal_size_value
205  
-      uint32_t width = nal_bs_read(&bs, 12);
206  
-      if (width != sequence->width)
207  
-      {
208  
-        changed = true;
209  
-        sequence->width = width;
210  
-      }
211  
-      // height
212  
-      // nal_start + 24 bits == vertical_size_value
213  
-      uint32_t height = nal_bs_read(&bs, 12);
214  
-      if (height != sequence->height)
215  
-      {
216  
-        changed = true;
217  
-        sequence->height = height;
218  
-      }
219  
-
220  
-      // aspect ratio
221  
-      // nal_start + 28 bits == aspect_ratio_information
222  
-      float ratio = sequence->ratio;
223  
-      uint32_t ratio_info = nal_bs_read(&bs, 4);
224  
-      switch(ratio_info)
225  
-      {
226  
-        case 0x01:
227  
-          ratio = 1.0;
228  
-          break;
229  
-        default:
230  
-        case 0x02:
231  
-          ratio = 4.0/3.0;
232  
-          break;
233  
-        case 0x03:
234  
-          ratio = 16.0/9.0;
235  
-          break;
236  
-        case 0x04:
237  
-          ratio = 2.21;
238  
-          break;
239  
-      }
240  
-      if (ratio_info != sequence->ratio_info)
241  
-      {
242  
-        changed = true;
243  
-        sequence->ratio = ratio;
244  
-        sequence->ratio_info = ratio_info;
245  
-      }
246  
-
247  
-      // frame rate
248  
-      // nal_start + 32 bits == frame_rate_code
249  
-      float rate = sequence->rate;
250  
-      uint32_t rate_info = nal_bs_read(&bs, 4);
251  
-      switch(rate_info)
252  
-      {
253  
-        default:
254  
-        case 0x01:
255  
-          rate = 24000.0 / 1001.0;
256  
-          break;
257  
-        case 0x02:
258  
-          rate = 24000.0 / 1000.0;
259  
-          break;
260  
-        case 0x03:
261  
-          rate = 25000.0 / 1000.0;
262  
-          break;
263  
-        case 0x04:
264  
-          rate = 30000.0 / 1001.0;
265  
-          break;
266  
-        case 0x05:
267  
-          rate = 30000.0 / 1000.0;
268  
-          break;
269  
-        case 0x06:
270  
-          rate = 50000.0 / 1000.0;
271  
-          break;
272  
-        case 0x07:
273  
-          rate = 60000.0 / 1001.0;
274  
-          break;
275  
-        case 0x08:
276  
-          rate = 60000.0 / 1000.0;
277  
-          break;
278  
-      }
279  
-      if (rate_info != sequence->rate_info)
  143
+      if (p[1] == 0)
280 144
       {
281  
-        changed = true;
282  
-        sequence->rate = rate;
283  
-        sequence->rate_info = rate_info;
  145
+        if (p[0] == 0 && p[2] == 1)
  146
+          return p;
  147
+        if (p[2] == 0 && p[3] == 1)
  148
+          return p+1;
284 149
       }
285  
-      /*
286  
-      if (changed)
  150
+      if (p[3] == 0)
287 151
       {
288  
-        CLog::Log(LOGDEBUG, "CBitstreamConverter::mpeg2_sequence_header: "
289  
-          "width(%d), height(%d), ratio(%f), rate(%f)", width, height, ratio, rate);
  152
+        if (p[2] == 0 && p[4] == 1)
  153
+          return p+2;
  154
+        if (p[4] == 0 && p[5] == 1)
  155
+          return p+3;
290 156
       }
291  
-      */
292 157
     }
293  
-    nal_start = nal_end;
294 158
   }
295 159
 
296  
-  return changed;
  160
+  for (end += 3; p < end; p++)
  161
+  {
  162
+    if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  163
+      return p;
  164
+  }
  165
+
  166
+  return end + 3;
297 167
 }
298 168
 
299  
-void CBitstreamConverter::parseh264_sps(const uint8_t *sps, const uint32_t sps_size, bool *interlaced, int32_t *max_ref_frames)
  169
+static const uint8_t* avc_find_startcode(const uint8_t *p, const uint8_t *end)
300 170
 {
301  
-  nal_bitstream bs;
302  
-  sps_info_struct sps_info;
  171
+  const uint8_t *out = avc_find_startcode_internal(p, end);
  172
+  if (p<out && out<end && !out[-1])
  173
+    out--;
  174
+  return out;
  175
+}
303 176
 
304  
-  nal_bs_init(&bs, sps, sps_size);
  177
+////////////////////////////////////////////////////////////////////////////////////////////
  178
+/////////////////////////////////////////////////////////////////////////////////////////////
  179
+CBitstreamParser::CBitstreamParser()
  180
+{
  181
+}
305 182
 
306  
-  sps_info.profile_idc  = nal_bs_read(&bs, 8);
307  
-  nal_bs_read(&bs, 1);  // constraint_set0_flag
308  
-  nal_bs_read(&bs, 1);  // constraint_set1_flag
309  
-  nal_bs_read(&bs, 1);  // constraint_set2_flag
310  
-  nal_bs_read(&bs, 1);  // constraint_set3_flag
311  
-  nal_bs_read(&bs, 4);  // reserved
312  
-  sps_info.level_idc    = nal_bs_read(&bs, 8);
313  
-  sps_info.sps_id       = nal_bs_read_ue(&bs);
  183
+CBitstreamParser::~CBitstreamParser()
  184
+{
  185
+  Close();
  186
+}
314 187
 
315  
-  if (sps_info.profile_idc == 100 ||
316  
-      sps_info.profile_idc == 110 ||
317  
-      sps_info.profile_idc == 122 ||
318  
-      sps_info.profile_idc == 244 ||
319  
-      sps_info.profile_idc == 44  ||
320  
-      sps_info.profile_idc == 83  ||
321  
-      sps_info.profile_idc == 86)
322  
-  {
323  
-    sps_info.chroma_format_idc                    = nal_bs_read_ue(&bs);
324  
-    if (sps_info.chroma_format_idc == 3)
325  
-      sps_info.separate_colour_plane_flag         = nal_bs_read(&bs, 1);
326  
-    sps_info.bit_depth_luma_minus8                = nal_bs_read_ue(&bs);
327  
-    sps_info.bit_depth_chroma_minus8              = nal_bs_read_ue(&bs);
328  
-    sps_info.qpprime_y_zero_transform_bypass_flag = nal_bs_read(&bs, 1);
  188
+bool CBitstreamParser::Open()
  189
+{
  190
+  return true;
  191
+}
329 192
 
330  
-    sps_info.seq_scaling_matrix_present_flag = nal_bs_read (&bs, 1);
331  
-    if (sps_info.seq_scaling_matrix_present_flag)
332  
-    {
333  
-      /* TODO: unfinished */
334  
-    }
335  
-  }
336  
-  sps_info.log2_max_frame_num_minus4 = nal_bs_read_ue(&bs);
337  
-  if (sps_info.log2_max_frame_num_minus4 > 12)
338  
-  { // must be between 0 and 12
339  
-    // don't early return here - the bits we are using (profile/level/interlaced/ref frames)
340  
-    // might still be valid - let the parser go on and pray.
341  
-    //return;
  193
+void CBitstreamParser::Close()
  194
+{
  195
+}
  196
+
  197
+const uint8_t* CBitstreamParser::find_start_code(const uint8_t *p,
  198
+  const uint8_t *end, uint32_t *state)
  199
+{
  200
+  assert(p <= end);
  201
+  if (p >= end)
  202
+    return end;
  203
+
  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;
342 209
   }
343  
-  sps_info.pic_order_cnt_type = nal_bs_read_ue(&bs);
344  
-  if (sps_info.pic_order_cnt_type == 0)
345  
-  {
346  
-    sps_info.log2_max_pic_order_cnt_lsb_minus4 = nal_bs_read_ue(&bs);
  210
+
  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;
  218
+    }
347 219
   }
348  
-  else if (sps_info.pic_order_cnt_type == 1)
349  
-  { // TODO: unfinished
350  
-    /*
351  
-    delta_pic_order_always_zero_flag = gst_nal_bs_read (bs, 1);
352  
-    offset_for_non_ref_pic = gst_nal_bs_read_se (bs);
353  
-    offset_for_top_to_bottom_field = gst_nal_bs_read_se (bs);
354  
-
355  
-    num_ref_frames_in_pic_order_cnt_cycle = gst_nal_bs_read_ue (bs);
356  
-    for ( i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
357  
-    offset_for_ref_frame[i] = gst_nal_bs_read_se (bs);
358  
-    */
359  
-  }
360  
-
361  
-  sps_info.max_num_ref_frames             = nal_bs_read_ue(&bs);
362  
-  sps_info.gaps_in_frame_num_value_allowed_flag = nal_bs_read(&bs, 1);
363  
-  sps_info.pic_width_in_mbs_minus1        = nal_bs_read_ue(&bs);
364  
-  sps_info.pic_height_in_map_units_minus1 = nal_bs_read_ue(&bs);
365 220
 
366  
-  sps_info.frame_mbs_only_flag            = nal_bs_read(&bs, 1);
367  
-  if (!sps_info.frame_mbs_only_flag)
368  
-    sps_info.mb_adaptive_frame_field_flag = nal_bs_read(&bs, 1);
369  
-
370  
-  sps_info.direct_8x8_inference_flag      = nal_bs_read(&bs, 1);
371  
-
372  
-  sps_info.frame_cropping_flag            = nal_bs_read(&bs, 1);
373  
-  if (sps_info.frame_cropping_flag)
374  
-  {
375  
-    sps_info.frame_crop_left_offset       = nal_bs_read_ue(&bs);
376  
-    sps_info.frame_crop_right_offset      = nal_bs_read_ue(&bs);
377  
-    sps_info.frame_crop_top_offset        = nal_bs_read_ue(&bs);
378  
-    sps_info.frame_crop_bottom_offset     = nal_bs_read_ue(&bs);
379  
-  }
  221
+  p = FFMIN(p, end) - 4;
  222
+  *state = BS_RB32(p);
380 223
 
381  
-  *interlaced = !sps_info.frame_mbs_only_flag;
382  
-  *max_ref_frames = sps_info.max_num_ref_frames;
  224
+  return p + 4;
383 225
 }
384 226
 
385  
-const uint8_t *CBitstreamConverter::avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
  227
+bool CBitstreamParser::FindIdrSlice(const uint8_t *buf, int buf_size)
386 228
 {
387  
-  const uint8_t *a = p + 4 - ((intptr_t)p & 3);
  229
+  if (!buf)
  230
+    return false;
388 231
 
389  
-  for (end -= 3; p < a && p < end; p++)
390  
-  {
391  
-    if (p[0] == 0 && p[1] == 0 && p[2] == 1)
392  
-      return p;
393  
-  }
  232
+  bool rtn = false;
  233
+  uint32_t state = -1;
  234
+  const uint8_t *buf_end = buf + buf_size;
394 235
 
395  
-  for (end -= 3; p < end; p += 4)
  236
+  for(;;)
396 237
   {
397  
-    uint32_t x = *(const uint32_t*)p;
398  
-    if ((x - 0x01010101) & (~x) & 0x80808080) // generic
  238
+    buf = find_start_code(buf, buf_end, &state);
  239
+    if (buf >= buf_end)
399 240
     {
400  
-      if (p[1] == 0)
401  
-      {
402  
-        if (p[0] == 0 && p[2] == 1)
403  
-          return p;
404  
-        if (p[2] == 0 && p[3] == 1)
405  
-          return p+1;
406  
-      }
407  
-      if (p[3] == 0)
408  
-      {
409  
-        if (p[2] == 0 && p[4] == 1)
410  
-          return p+2;
411  
-        if (p[4] == 0 && p[5] == 1)
412  
-          return p+3;
413  
-      }
414  
-    }
415  
-  }
416  
-
417  
-  for (end += 3; p < end; p++)
418  
-  {
419  
-    if (p[0] == 0 && p[1] == 0 && p[2] == 1)
420  
-      return p;
421  
-  }
422  
-
423  
-  return end + 3;
424  
-}
425  
-
426  
-const uint8_t *CBitstreamConverter::avc_find_startcode(const uint8_t *p, const uint8_t *end)
427  
-{
428  
-  const uint8_t *out= avc_find_startcode_internal(p, end);
429  
-  if (p<out && out<end && !out[-1])
430  
-    out--;
431  
-  return out;
432  
-}
433  
-
434  
-const int CBitstreamConverter::avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
435  
-{
436  
-  const uint8_t *p = buf_in;
437  
-  const uint8_t *end = p + size;
438  
-  const uint8_t *nal_start, *nal_end;
439  
-
440  
-  size = 0;
441  
-  nal_start = avc_find_startcode(p, end);
442  
-
443  
-  for (;;)
444  
-  {
445  
-    while (nal_start < end && !*(nal_start++));
446  
-    if (nal_start == end)
  241
+      //CLog::Log(LOGDEBUG, "FindIdrSlice: buf(%p), buf_end(%p)", buf, buf_end);
447 242
       break;
448  
-
449  
-    nal_end = avc_find_startcode(nal_start, end);
450  
-    m_dllAvFormat->avio_wb32(pb, nal_end - nal_start);
451  
-    m_dllAvFormat->avio_write(pb, nal_start, nal_end - nal_start);
452  
-    size += 4 + nal_end - nal_start;
453  
-    nal_start = nal_end;
454  
-  }
455  
-  return size;
456  
-}
457  
-
458  
-const int CBitstreamConverter::avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
459  
-{
460  
-  AVIOContext *pb;
461  
-  int ret = m_dllAvFormat->avio_open_dyn_buf(&pb);
462  
-  if (ret < 0)
463  
-    return ret;
464  
-
465  
-  avc_parse_nal_units(pb, buf_in, *size);
466  
-
467  
-  m_dllAvUtil->av_freep(buf);
468  
-  *size = m_dllAvFormat->avio_close_dyn_buf(pb, buf);
469  
-  return 0;
470  
-}
471  
-
472  
-const int CBitstreamConverter::isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
473  
-{
474  
-  // extradata from bytestream h264, convert to avcC atom data for bitstream
475  
-  if (len > 6)
476  
-  {
477  
-    /* check for h264 start code */
478  
-    if (BS_RB32(data) == 0x00000001 || BS_RB24(data) == 0x000001)
479  
-    {
480  
-      uint8_t *buf=NULL, *end, *start;
481  
-      uint32_t sps_size=0, pps_size=0;
482  
-      uint8_t *sps=0, *pps=0;
483  
-
484  
-      int ret = avc_parse_nal_units_buf(data, &buf, &len);
485  
-      if (ret < 0)
486  
-        return ret;
487  
-      start = buf;
488  
-      end = buf + len;
489  
-
490  
-      /* look for sps and pps */
491  
-      while (end - buf > 4)
492  
-      {
493  
-        uint32_t size;
494  
-        uint8_t  nal_type;
495  
-        size = FFMIN(BS_RB32(buf), end - buf - 4);
496  
-        buf += 4;
497  
-        nal_type = buf[0] & 0x1f;
498  
-        if (nal_type == 7) /* SPS */
499  
-        {
500  
-          sps = buf;
501  
-          sps_size = size;
502  
-        }
503  
-        else if (nal_type == 8) /* PPS */
504  
-        {
505  
-          pps = buf;
506  
-          pps_size = size;
507  
-        }
508  
-        buf += size;
509  
-      }
510  
-      if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
511  
-        assert(0);
512  
-
513  
-      m_dllAvFormat->avio_w8(pb, 1); /* version */
514  
-      m_dllAvFormat->avio_w8(pb, sps[1]); /* profile */
515  
-      m_dllAvFormat->avio_w8(pb, sps[2]); /* profile compat */
516  
-      m_dllAvFormat->avio_w8(pb, sps[3]); /* level */
517  
-      m_dllAvFormat->avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
518  
-      m_dllAvFormat->avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
519  
-
520  
-      m_dllAvFormat->avio_wb16(pb, sps_size);
521  
-      m_dllAvFormat->avio_write(pb, sps, sps_size);
522  
-      if (pps)
523  
-      {
524  
-        m_dllAvFormat->avio_w8(pb, 1); /* number of pps */
525  
-        m_dllAvFormat->avio_wb16(pb, pps_size);
526  
-        m_dllAvFormat->avio_write(pb, pps, pps_size);
527  
-      }
528  
-      m_dllAvUtil->av_free(start);
529 243
     }
530  
-    else
  244
+
  245
+    --buf;
  246
+    int src_length = buf_end - buf;
  247
+    switch (state & 0x1f)
531 248
     {
532  
-      m_dllAvFormat->avio_write(pb, data, len);
  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;
533 268
     }
  269
+    buf += src_length;
534 270
   }
535  
-  return 0;
  271
+
  272
+  return rtn;
536 273
 }
537 274
 
  275
+////////////////////////////////////////////////////////////////////////////////////////////
  276
+/////////////////////////////////////////////////////////////////////////////////////////////
538 277
 CBitstreamConverter::CBitstreamConverter()
539 278
 {
540 279
   m_convert_bitstream = false;
@@ -549,6 +288,7 @@ CBitstreamConverter::CBitstreamConverter()
549 288
   m_dllAvUtil         = NULL;
550 289
   m_dllAvFormat       = NULL;
551 290
   m_convert_bytestream = false;
  291
+  m_sps_pps_context.sps_pps_data = NULL;
552 292
 }
553 293
 
554 294
 CBitstreamConverter::~CBitstreamConverter()
@@ -556,43 +296,53 @@ CBitstreamConverter::~CBitstreamConverter()
556 296
   Close();
557 297
 }
558 298
 
559  
-bool CBitstreamConverter::Open(enum AVCodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb)
  299
+bool CBitstreamConverter::Open(enum CodecID codec, uint8_t *in_extradata, int in_extrasize, bool to_annexb)
560 300
 {
561  
-  m_codec = codec;
562 301
   m_to_annexb = to_annexb;
563 302
 
564  
-  switch(codec)
  303
+  m_codec = codec;
  304
+  switch(m_codec)
565 305
   {
566 306
     case AV_CODEC_ID_H264:
567 307
       if (in_extrasize < 7 || in_extradata == NULL)
568 308
       {
569  
-        CLog::Log(LOGERROR, "CBitstreamConverter::Open avcC data too small or missing\n");
  309
+        CLog::Log(LOGERROR, "CBitstreamConverter::Open avcC data too small or missing");
570 310
         return false;
571 311
       }
572 312
       // valid avcC data (bitstream) always starts with the value 1 (version)
573  
-      if (m_to_annexb)
  313
+      if(m_to_annexb)
574 314
       {
575  
-        if ( *(char*)in_extradata == 1 )
  315
+        if ( in_extradata[0] == 1 )
576 316
         {
577  
-          CLog::Log(LOGINFO, "CBitstreamConverter::Open bitstream to annexb init\n");
578  
-          m_convert_bitstream = BitstreamConvertInit(in_extradata, in_extrasize);
  317
+          CLog::Log(LOGINFO, "CBitstreamConverter::Open bitstream to annexb init");
  318
+          m_dllAvUtil = new DllAvUtil;
  319
+          if (!m_dllAvUtil->Load())
  320
+              return false;
  321
+
  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);
579 326
           return true;
580 327
         }
581 328
       }
582 329
       else
583 330
       {
  331
+        m_dllAvUtil = new DllAvUtil;
  332
+        if (!m_dllAvUtil->Load())
  333
+            return false;
  334
+
584 335
         // valid avcC atom data always starts with the value 1 (version)
585  
-        if ( *in_extradata != 1 )
  336
+        if ( in_extradata[0] != 1 )
586 337
         {
587 338
           if ( (in_extradata[0] == 0 && in_extradata[1] == 0 && in_extradata[2] == 0 && in_extradata[3] == 1) ||
588 339
                (in_extradata[0] == 0 && in_extradata[1] == 0 && in_extradata[2] == 1) )
589 340
           {
590  
-            CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init\n");
  341
+            CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init");
591 342
             // video content is from x264 or from bytestream h264 (AnnexB format)
592 343
             // NAL reformating to bitstream format needed
593  
-            m_dllAvUtil = new DllAvUtil;
594 344
             m_dllAvFormat = new DllAvFormat;
595  
-            if (!m_dllAvUtil->Load() || !m_dllAvFormat->Load())
  345
+            if (!m_dllAvFormat->Load())
596 346
               return false;
597 347
 
598 348
             AVIOContext *pb;
@@ -606,7 +356,7 @@ bool CBitstreamConverter::Open(enum AVCodecID codec, uint8_t *in_extradata, int
606 356
             // extract the avcC atom data into extradata then write it into avcCData for VDADecoder
607 357
             in_extrasize = m_dllAvFormat->avio_close_dyn_buf(pb, &in_extradata);
608 358
             // make a copy of extradata contents
609  
-            m_extradata = (uint8_t *)malloc(in_extrasize);
  359
+            m_extradata = (uint8_t *)m_dllAvUtil->av_malloc(in_extrasize);
610 360
             memcpy(m_extradata, in_extradata, in_extrasize);
611 361
             m_extrasize = in_extrasize;
612 362
             // done with the converted extradata, we MUST free using av_free
@@ -623,31 +373,32 @@ bool CBitstreamConverter::Open(enum AVCodecID codec, uint8_t *in_extradata, int
623 373
         {
624 374
           if (in_extradata[4] == 0xFE)
625 375
           {
626  
-            CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init 3 byte to 4 byte nal\n");
  376
+            CLog::Log(LOGINFO, "CBitstreamConverter::Open annexb to bitstream init 3 byte to 4 byte nal");
627 377
             // video content is from so silly encoder that think 3 byte NAL sizes
628 378
             // are valid, setup to convert 3 byte NAL sizes to 4 byte.
629  
-            m_dllAvUtil = new DllAvUtil;
630 379
             m_dllAvFormat = new DllAvFormat;
631  
-            if (!m_dllAvUtil->Load() || !m_dllAvFormat->Load())
  380
+            if (!m_dllAvFormat->Load())
632 381
               return false;
633 382
 
634 383
             in_extradata[4] = 0xFF;
635 384
             m_convert_3byteTo4byteNALSize = true;
636 385
            
637  
-            m_extradata = (uint8_t *)malloc(in_extrasize);
  386
+            m_extradata = (uint8_t *)m_dllAvUtil->av_malloc(in_extrasize);
638 387
             memcpy(m_extradata, in_extradata, in_extrasize);
639 388
             m_extrasize = in_extrasize;
640 389
             return true;
641 390
           }
642 391
         }
643 392
         // valid avcC atom 
644  
-        m_extradata = (uint8_t*)malloc(in_extrasize);
  393
+        m_extradata = (uint8_t*)m_dllAvUtil->av_malloc(in_extrasize);
645 394
         memcpy(m_extradata, in_extradata, in_extrasize);
646 395
         m_extrasize = in_extrasize;
647 396
         return true;
648 397
       }
  398
+      return false;
649 399
       break;
650 400
     default:
  401
+      return false;
651 402
       break;
652 403
   }
653 404
   return false;
@@ -655,29 +406,22 @@ bool CBitstreamConverter::Open(enum AVCodecID codec, uint8_t *in_extradata, int
655 406
 
656 407
 void CBitstreamConverter::Close(void)
657 408
 {
658  
-  if (m_convert_bitstream)
659  
-  {
660  
-    if (m_sps_pps_context.sps_pps_data)
661  
-      free(m_sps_pps_context.sps_pps_data), m_sps_pps_context.sps_pps_data = NULL;
662  
-    if (m_convertBuffer)
663  
-      free(m_convertBuffer), m_convertBuffer = NULL;
664  
-    m_convertSize = 0;
665  
-  }
  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;
666 411
 
667  
-  if (m_convert_bytestream)
668  
-  {
669  
-    if (m_convertBuffer)
670  
-      m_dllAvUtil->av_free(m_convertBuffer), m_convertBuffer = NULL;
671  
-    m_convertSize = 0;
672  
-  }
  412
+  if (m_convertBuffer)
  413
+    m_dllAvUtil->av_free(m_convertBuffer), m_convertBuffer = NULL;
  414
+  m_convertSize = 0;
673 415
 
674 416
   if (m_extradata)
675  
-    free(m_extradata), m_extradata = NULL;
  417
+    m_dllAvUtil->av_free(m_extradata), m_extradata = NULL;
676 418
   m_extrasize = 0;
677  
-  
  419
+
678 420
   m_inputSize = 0;
679 421
   m_inputBuffer = NULL;
  422
+
680 423
   m_convert_bitstream = false;
  424
+  m_convert_bytestream = false;
681 425
   m_convert_3byteTo4byteNALSize = false;
682 426
 
683 427
   if (m_dllAvUtil)
@@ -690,9 +434,9 @@ bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
690 434
 {
691 435
   if (m_convertBuffer)
692 436
     free(m_convertBuffer), m_convertBuffer = NULL;
  437
+  m_inputSize = 0;
693 438
   m_convertSize = 0;
694 439
   m_inputBuffer = NULL;
695  
-  m_inputSize = 0;
696 440
 
697 441
   if (pData)
698 442
   {
@@ -701,7 +445,6 @@ bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
701 445
       if (m_to_annexb)
702 446
       {
703 447
         int demuxer_bytes = iSize;
704  
-  
705 448
         uint8_t *demuxer_content = pData;
706 449
 
707 450
         if (m_convert_bitstream)
@@ -715,41 +458,56 @@ bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
715 458
           {
716 459
             m_convertSize   = bytestream_size;
717 460
             m_convertBuffer = bytestream_buff;
  461
+            return true;
718 462
           }
719 463
           else
720 464
           {
721  
-            m_inputBuffer = pData;
722  
-            m_inputSize   = iSize;
723  
-            CLog::Log(LOGERROR, "CBitstreamConverter::Convert error converting.\n");
  465
+            m_convertSize = 0;
  466
+            m_convertBuffer = NULL;
  467
+            CLog::Log(LOGERROR, "CBitstreamConverter::Convert: error converting.");
  468
+            return false;
724 469
           }
725 470
         }
726 471
         else
727 472
         {
  473
+          m_inputSize = iSize;
728 474
           m_inputBuffer = pData;
729  
-          m_inputSize   = iSize;
  475
+          return true;
730 476
         }
731  
-        return true;
732 477
       }
733 478
       else
734 479
       {
  480
+        m_inputSize = iSize;
735 481
         m_inputBuffer = pData;
736  
-        m_inputSize   = iSize;
737 482
   
738  
-        if (m_convertBuffer)
739  
-          m_dllAvUtil->av_free(m_convertBuffer), m_convertBuffer = NULL;
740  
-        m_convertSize = 0;
741  
-
742 483
         if (m_convert_bytestream)
743 484
         {
  485
+          if(m_convertBuffer)
  486
+          {
  487
+            m_dllAvUtil->av_free(m_convertBuffer);
  488
+            m_convertBuffer = NULL;
  489
+          }
  490
+          m_convertSize = 0;
  491
+
744 492
           // convert demuxer packet from bytestream (AnnexB) to bitstream
745 493
           AVIOContext *pb;
746  
-          if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
  494
+
  495
+          if(m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
  496
+          {
747 497
             return false;
  498
+          }
748 499
           m_convertSize = avc_parse_nal_units(pb, pData, iSize);
749 500
           m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
750 501
         }
751 502
         else if (m_convert_3byteTo4byteNALSize)
752 503
         {
  504
+          if(m_convertBuffer)
  505
+          {
  506
+            m_dllAvUtil->av_free(m_convertBuffer);
  507
+            m_convertBuffer = NULL;
  508
+          }
  509
+          m_convertSize = 0;
  510
+
753 511
           // convert demuxer packet from 3 byte NAL sizes to 4 byte
754 512
           AVIOContext *pb;
755 513
           if (m_dllAvFormat->avio_open_dyn_buf(&pb) < 0)
@@ -766,6 +524,7 @@ bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
766 524
             m_dllAvFormat->avio_write(pb, nal_start, nal_size);
767 525
             nal_start += nal_size;
768 526
           }
  527
+
769 528
           m_convertSize = m_dllAvFormat->avio_close_dyn_buf(pb, &m_convertBuffer);
770 529
         }
771 530
         return true;
@@ -779,35 +538,33 @@ bool CBitstreamConverter::Convert(uint8_t *pData, int iSize)
779 538
 
780 539
 uint8_t *CBitstreamConverter::GetConvertBuffer()
781 540
 {
782  
-  if ((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize)
783  
-    && m_convertBuffer != NULL)
  541
+  if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
784 542
     return m_convertBuffer;
785  
-
786  
-  return m_inputBuffer;
  543
+  else
  544
+    return m_inputBuffer;
787 545
 }
788 546
 
789 547
 int CBitstreamConverter::GetConvertSize()
790 548
 {
791  
-  if ((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize)
792  
-    && m_convertBuffer != NULL)
  549
+  if((m_convert_bitstream || m_convert_bytestream || m_convert_3byteTo4byteNALSize) && m_convertBuffer != NULL)
793 550
     return m_convertSize;
794  
-
795  
-  return m_inputSize; 
  551
+  else
  552
+    return m_inputSize;
796 553
 }
797 554
 
798 555
 uint8_t *CBitstreamConverter::GetExtraData()
799 556
 {
800  
-  if (m_convert_bitstream)
  557
+  if(m_convert_bitstream)
801 558
     return m_sps_pps_context.sps_pps_data;
802  
-
803  
-  return m_extradata;
  559
+  else
  560
+    return m_extradata;
804 561
 }
805 562
 int CBitstreamConverter::GetExtraSize()
806 563
 {
807  
-  if (m_convert_bitstream)
  564
+  if(m_convert_bitstream)
808 565
     return m_sps_pps_context.size;
809  
-
810  
-  return m_extrasize;
  566
+  else
  567
+    return m_extrasize;
811 568
 }
812 569
 
813 570
 bool CBitstreamConverter::BitstreamConvertInit(void *in_extradata, int in_extrasize)
@@ -847,10 +604,10 @@ bool CBitstreamConverter::BitstreamConvertInit(void *in_extradata, int in_extras
847 604
     total_size += unit_size + 4;
848 605
     if ( (extradata + 2 + unit_size) > ((uint8_t*)in_extradata + in_extrasize) )
849 606
     {
850  
-      free(out);
  607
+      m_dllAvUtil->av_free(out);
851 608
       return false;
852 609
     }
853  
-    out = (uint8_t*)realloc(out, total_size);
  610
+    out = (uint8_t*)m_dllAvUtil->av_realloc(out, total_size);
854 611
     if (!out)
855 612
       return false;
856 613
 
@@ -922,8 +679,7 @@ bool CBitstreamConverter::BitstreamConvert(uint8_t* pData, int iSize, uint8_t **
922 679
   return true;
923 680
 
924 681
 fail:
925  
-  free(*poutbuf);
926  
-  *poutbuf = NULL;
  682
+  m_dllAvUtil->av_free(*poutbuf), *poutbuf = NULL;
927 683
   *poutbuf_size = 0;
928 684
   return false;
929 685
 }
@@ -945,7 +701,7 @@ void CBitstreamConverter::BitstreamAllocAndCopy( uint8_t **poutbuf, int *poutbuf
945 701
   uint8_t nal_header_size = offset ? 3 : 4;
946 702
 
947 703
   *poutbuf_size += sps_pps_size + in_size + nal_header_size;
948  
-  *poutbuf = (uint8_t*)realloc(*poutbuf, *poutbuf_size);
  704
+  *poutbuf = (uint8_t*)m_dllAvUtil->av_realloc(*poutbuf, *poutbuf_size);
949 705
   if (sps_pps)
950 706
     memcpy(*poutbuf + offset, sps_pps, sps_pps_size);
951 707
 
@@ -962,4 +718,377 @@ void CBitstreamConverter::BitstreamAllocAndCopy( uint8_t **poutbuf, int *poutbuf
962 718
   }
963 719
 }
964 720
 
  721
+const int CBitstreamConverter::avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
  722
+{
  723
+  const uint8_t *p = buf_in;
  724
+  const uint8_t *end = p + size;
  725
+  const uint8_t *nal_start, *nal_end;
  726
+
  727
+  size = 0;
  728
+  nal_start = avc_find_startcode(p, end);
  729
+
  730
+  for (;;) {
  731
+    while (nal_start < end && !*(nal_start++));
  732
+    if (nal_start == end)
  733
+      break;
  734
+
  735
+    nal_end = avc_find_startcode(nal_start, end);
  736
+    m_dllAvFormat->avio_wb32(pb, nal_end - nal_start);
  737
+    m_dllAvFormat->avio_write(pb, nal_start, nal_end - nal_start);
  738
+    size += 4 + nal_end - nal_start;
  739
+    nal_start = nal_end;
  740
+  }
  741
+  return size;
  742
+}
  743
+
  744
+const int CBitstreamConverter::avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
  745
+{
  746
+  AVIOContext *pb;
  747
+  int ret = m_dllAvFormat->avio_open_dyn_buf(&pb);
  748
+  if (ret < 0)
  749
+    return ret;
  750
+
  751
+  avc_parse_nal_units(pb, buf_in, *size);
965 752
 
  753
+  m_dllAvUtil->av_freep(buf);
  754
+  *size = m_dllAvFormat->avio_close_dyn_buf(pb, buf);
  755
+  return 0;
  756
+}
  757
+
  758
+const int CBitstreamConverter::isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
  759
+{
  760
+  // extradata from bytestream h264, convert to avcC atom data for bitstream
  761
+  if (len > 6)
  762
+  {
  763
+    /* check for h264 start code */
  764
+    if (BS_RB32(data) == 0x00000001 || BS_RB24(data) == 0x000001)
  765
+    {
  766
+      uint8_t *buf=NULL, *end, *start;
  767
+      uint32_t sps_size=0, pps_size=0;
  768
+      uint8_t *sps=0, *pps=0;
  769
+
  770
+      int ret = avc_parse_nal_units_buf(data, &buf, &len);
  771
+      if (ret < 0)
  772
+        return ret;
  773
+      start = buf;
  774
+      end = buf + len;
  775
+
  776
+      /* look for sps and pps */
  777
+      while (end - buf > 4)
  778
+      {
  779
+        uint32_t size;
  780
+        uint8_t  nal_type;
  781
+        size = FFMIN(BS_RB32(buf), end - buf - 4);
  782
+        buf += 4;
  783
+        nal_type = buf[0] & 0x1f;
  784
+        if (nal_type == 7) /* SPS */
  785
+        {
  786
+          sps = buf;
  787
+          sps_size = size;
  788
+        }
  789
+        else if (nal_type == 8) /* PPS */
  790
+        {
  791
+          pps = buf;
  792
+          pps_size = size;
  793
+        }
  794
+        buf += size;
  795
+      }
  796
+      if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
  797
+        assert(0);
  798
+
  799
+      m_dllAvFormat->avio_w8(pb, 1); /* version */
  800
+      m_dllAvFormat->avio_w8(pb, sps[1]); /* profile */
  801
+      m_dllAvFormat->avio_w8(pb, sps[2]); /* profile compat */
  802
+      m_dllAvFormat->avio_w8(pb, sps[3]); /* level */
  803
+      m_dllAvFormat->avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
  804
+      m_dllAvFormat->avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
  805
+
  806
+      m_dllAvFormat->avio_wb16(pb, sps_size);
  807
+      m_dllAvFormat->avio_write(pb, sps, sps_size);
  808
+      if (pps)
  809
+      {
  810
+        m_dllAvFormat->avio_w8(pb, 1); /* number of pps */
  811
+        m_dllAvFormat->avio_wb16(pb, pps_size);
  812
+        m_dllAvFormat->avio_write(pb, pps, pps_size);
  813
+      }
  814
+      m_dllAvUtil->av_free(start);
  815
+    }
  816
+    else
  817
+    {
  818
+      m_dllAvFormat->avio_write(pb, data, len);
  819
+    }
  820
+  }
  821
+  return 0;
  822
+}
  823
+
  824
+void CBitstreamConverter::bits_reader_set( bits_reader_t *br, uint8_t *buf, int len )
  825
+{
  826
+  br->buffer = br->start = buf;
  827
+  br->offbits = 0;
  828
+  br->length = len;
  829
+  br->oflow = 0;
  830
+}
  831
+
  832
+uint32_t CBitstreamConverter::read_bits( bits_reader_t *br, int nbits )
  833
+{
  834
+  int i, nbytes;
  835
+  uint32_t ret = 0;
  836
+  uint8_t *buf;
  837
+
  838
+  buf = br->buffer;
  839
+  nbytes = (br->offbits + nbits)/8;
  840
+  if ( ((br->offbits + nbits) %8 ) > 0 )
  841
+    nbytes++;
  842
+  if ( (buf + nbytes) > (br->start + br->length) ) {
  843
+    br->oflow = 1;
  844
+    return 0;
  845
+  }
  846
+  for ( i=0; i<nbytes; i++ )
  847
+    ret += buf[i]<<((nbytes-i-1)*8);
  848
+  i = (4-nbytes)*8+br->offbits;
  849
+  ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-br->offbits);
  850
+
  851
+  br->offbits += nbits;
  852
+  br->buffer += br->offbits / 8;
  853
+  br->offbits %= 8;
  854
+
  855
+  return ret;
  856
+}
  857
+
  858
+void CBitstreamConverter::skip_bits( bits_reader_t *br, int nbits )
  859
+{
  860
+  br->offbits += nbits;
  861
+  br->buffer += br->offbits / 8;
  862
+  br->offbits %= 8;
  863
+  if ( br->buffer > (br->start + br->length) ) {
  864
+    br->oflow = 1;
  865
+  }
  866
+}
  867
+
  868
+uint32_t CBitstreamConverter::get_bits( bits_reader_t *br, int nbits )
  869
+{