~ubuntu-branches/ubuntu/wily/libde265/wily

« back to all changes in this revision

Viewing changes to libde265/nal-parser.cc

  • Committer: Package Import Robot
  • Author(s): Joachim Bauch
  • Date: 2015-07-16 11:07:46 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150716110746-76vsv24j3yux7tnu
Tags: 1.0.2-1
* Imported Upstream version 1.0.2
* Added new files to copyright information.
* Only export decoder API and update symbols for new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
  skipped_bytes.clear();
59
59
}
60
60
 
61
 
void NAL_unit::resize(int new_size)
 
61
LIBDE265_CHECK_RESULT bool NAL_unit::resize(int new_size)
62
62
{
63
63
  if (capacity < new_size) {
64
64
    unsigned char* newbuffer = (unsigned char*)malloc(new_size);
 
65
    if (newbuffer == NULL) {
 
66
      return false;
 
67
    }
65
68
 
66
69
    if (nal_data != NULL) {
67
70
      memcpy(newbuffer, nal_data, data_size);
71
74
    nal_data = newbuffer;
72
75
    capacity = new_size;
73
76
  }
 
77
  return true;
74
78
}
75
79
 
76
 
void NAL_unit::append(const unsigned char* in_data, int n)
 
80
LIBDE265_CHECK_RESULT bool NAL_unit::append(const unsigned char* in_data, int n)
77
81
{
78
 
  resize(data_size + n);
 
82
  if (!resize(data_size + n)) {
 
83
    return false;
 
84
  }
79
85
  memcpy(nal_data + data_size, in_data, n);
80
86
  data_size += n;
 
87
  return true;
81
88
}
82
89
 
83
 
void NAL_unit::set_data(const unsigned char* in_data, int n)
 
90
bool LIBDE265_CHECK_RESULT NAL_unit::set_data(const unsigned char* in_data, int n)
84
91
{
85
 
  resize(n);
 
92
  if (!resize(n)) {
 
93
    return false;
 
94
  }
86
95
  memcpy(nal_data, in_data, n);
87
96
  data_size = n;
 
97
  return true;
88
98
}
89
99
 
90
100
void NAL_unit::insert_skipped_byte(int pos)
177
187
}
178
188
 
179
189
 
180
 
NAL_unit* NAL_Parser::alloc_NAL_unit(int size)
 
190
LIBDE265_CHECK_RESULT NAL_unit* NAL_Parser::alloc_NAL_unit(int size)
181
191
{
182
192
  NAL_unit* nal;
183
193
 
192
202
  }
193
203
 
194
204
  nal->clear();
195
 
  nal->resize(size);
 
205
  if (!nal->resize(size)) {
 
206
    free_NAL_unit(nal);
 
207
    return NULL;
 
208
  }
196
209
 
197
210
  return nal;
198
211
}
199
212
 
200
213
void NAL_Parser::free_NAL_unit(NAL_unit* nal)
201
214
{
 
215
  if (nal == NULL) {
 
216
    // Allow calling with NULL just like regular "free()"
 
217
    return;
 
218
  }
202
219
  if (NAL_free_list.size() < DE265_NAL_FREE_LIST_SIZE) {
203
220
    NAL_free_list.push_back(nal);
204
221
  }
235
252
 
236
253
  if (pending_input_NAL == NULL) {
237
254
    pending_input_NAL = alloc_NAL_unit(len+3);
 
255
    if (pending_input_NAL == NULL) {
 
256
      return DE265_ERROR_OUT_OF_MEMORY;
 
257
    }
238
258
    pending_input_NAL->pts = pts;
239
259
    pending_input_NAL->user_data = user_data;
240
260
  }
243
263
 
244
264
  // Resize output buffer so that complete input would fit.
245
265
  // We add 3, because in the worst case 3 extra bytes are created for an input byte.
246
 
  nal->resize(nal->size() + len + 3);
 
266
  if (!nal->resize(nal->size() + len + 3)) {
 
267
    return DE265_ERROR_OUT_OF_MEMORY;
 
268
  }
247
269
 
248
270
  unsigned char* out = nal->data() + nal->size();
249
271
 
316
338
        // initialize new, empty NAL unit
317
339
 
318
340
        pending_input_NAL = alloc_NAL_unit(len+3);
 
341
        if (pending_input_NAL == NULL) {
 
342
          return DE265_ERROR_OUT_OF_MEMORY;
 
343
        }
319
344
        pending_input_NAL->pts = pts;
320
345
        pending_input_NAL->user_data = user_data;
321
346
        nal = pending_input_NAL;
352
377
  end_of_frame = false;
353
378
 
354
379
  NAL_unit* nal = alloc_NAL_unit(len);
355
 
  nal->set_data(data, len);
 
380
  if (nal == NULL || !nal->set_data(data, len)) {
 
381
    free_NAL_unit(nal);
 
382
    return DE265_ERROR_OUT_OF_MEMORY;
 
383
  }
356
384
  nal->pts = pts;
357
385
  nal->user_data = user_data;
358
386
 
372
400
 
373
401
    // append bytes that are implied by the push state
374
402
 
375
 
    if (input_push_state==6) { nal->append(null,1); }
376
 
    if (input_push_state==7) { nal->append(null,2); }
 
403
    if (input_push_state==6) {
 
404
      if (!nal->append(null,1)) {
 
405
        return DE265_ERROR_OUT_OF_MEMORY;
 
406
      }
 
407
    }
 
408
    if (input_push_state==7) {
 
409
      if (!nal->append(null,2)) {
 
410
        return DE265_ERROR_OUT_OF_MEMORY;
 
411
      }
 
412
    }
377
413
 
378
414
 
379
415
    // only push the NAL if it contains at least the NAL header