Coverage Report

Created: 2023-06-07 07:17

/src/libmpeg2/fuzzer/mpeg2_dec_fuzzer.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2019 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
 */
20
21
#include <stddef.h>
22
#include <stdint.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <algorithm>
27
#include <memory>
28
29
#include "iv_datatypedef.h"
30
#include "iv.h"
31
#include "ivd.h"
32
#include "impeg2d.h"
33
34
32.9k
#define ALIGN2(x) ((((x) + 1) >> 1) << 1)
35
12.9k
#define MAX_FRAME_WIDTH 3840
36
12.9k
#define MAX_FRAME_HEIGHT 2160
37
#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
38
122k
#define ivd_api_function impeg2d_api_function
39
const IV_COLOR_FORMAT_T supportedColorFormats[] = {IV_YUV_420P, IV_YUV_420SP_UV,
40
                                                   IV_YUV_420SP_VU};
41
42
/* Decoder ignores invalid arch, i.e. for arm build, if SSSE3 is requested,
43
 * decoder defaults to a supported configuration. So same set of supported
44
 * architectures can be used in arm/arm64/x86 builds */
45
const IVD_ARCH_T supportedArchitectures[] = {
46
    ARCH_ARM_NONEON,  ARCH_ARM_A9Q,   ARCH_ARM_NEONINTR, ARCH_ARMV8_GENERIC,
47
    ARCH_X86_GENERIC, ARCH_X86_SSSE3, ARCH_X86_SSE42};
48
49
enum {
50
  OFFSET_COLOR_FORMAT = 6,
51
  OFFSET_NUM_CORES,
52
  OFFSET_ARCH,
53
  /* Should be the last entry */
54
  OFFSET_MAX,
55
};
56
57
const static int kMaxNumDecodeCalls = 100;
58
const static int kSupportedColorFormats = NELEMENTS(supportedColorFormats);
59
const static int kSupportedArchitectures = NELEMENTS(supportedArchitectures);
60
const static int kMaxCores = 4;
61
62
class Codec {
63
 public:
64
  Codec(IV_COLOR_FORMAT_T colorFormat, size_t numCores);
65
  ~Codec();
66
67
  void createCodec();
68
  void deleteCodec();
69
  void resetCodec();
70
  void setCores();
71
  void allocFrame();
72
  void freeFrame();
73
  void decodeHeader(const uint8_t *data, size_t size);
74
  IV_API_CALL_STATUS_T decodeFrame(const uint8_t *data, size_t size,
75
                                   size_t *bytesConsumed);
76
  void setParams(IVD_VIDEO_DECODE_MODE_T mode);
77
  void setArchitecture(IVD_ARCH_T arch);
78
79
 private:
80
  IV_COLOR_FORMAT_T mColorFormat;
81
  size_t mNumCores;
82
  iv_obj_t *mCodec;
83
  ivd_out_bufdesc_t mOutBufHandle;
84
  uint32_t mWidth;
85
  uint32_t mHeight;
86
  uint32_t mDeinterlace;
87
  iv_mem_rec_t *mMemRecords;
88
};
89
90
6.49k
Codec::Codec(IV_COLOR_FORMAT_T colorFormat, size_t numCores) {
91
6.49k
  mColorFormat = colorFormat;
92
6.49k
  mNumCores = numCores;
93
6.49k
  mCodec = nullptr;
94
6.49k
  mWidth = 0;
95
6.49k
  mHeight = 0;
96
6.49k
  mDeinterlace = 1;
97
6.49k
  memset(&mOutBufHandle, 0, sizeof(mOutBufHandle));
98
6.49k
}
99
100
6.49k
Codec::~Codec() {}
101
102
6.49k
void Codec::createCodec() {
103
6.49k
  IV_API_CALL_STATUS_T ret;
104
6.49k
  UWORD32 numMemRecords;
105
6.49k
  size_t i;
106
6.49k
  void *fxns = (void *)&ivd_api_function;
107
108
6.49k
  iv_num_mem_rec_ip_t get_mem_ip;
109
6.49k
  iv_num_mem_rec_op_t get_mem_op;
110
111
6.49k
  get_mem_ip.u4_size = sizeof(get_mem_ip);
112
6.49k
  get_mem_op.u4_size = sizeof(get_mem_op);
113
6.49k
  get_mem_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC;
114
115
6.49k
  ret = ivd_api_function(NULL, (void *)&get_mem_ip, (void *)&get_mem_op);
116
6.49k
  if (ret != IV_SUCCESS) {
117
0
    return;
118
0
  }
119
120
6.49k
  numMemRecords = get_mem_op.u4_num_mem_rec;
121
122
6.49k
  mMemRecords = (iv_mem_rec_t *)malloc(numMemRecords * sizeof(iv_mem_rec_t));
123
6.49k
  if (mMemRecords == NULL) {
124
0
    return;
125
0
  }
126
127
6.49k
  impeg2d_fill_mem_rec_ip_t fill_mem_ip;
128
6.49k
  impeg2d_fill_mem_rec_op_t fill_mem_op;
129
130
6.49k
  fill_mem_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
131
6.49k
  fill_mem_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location =
132
6.49k
      (iv_mem_rec_t *)mMemRecords;
133
6.49k
  fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd = MAX_FRAME_WIDTH;
134
6.49k
  fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht = MAX_FRAME_HEIGHT;
135
6.49k
  fill_mem_ip.u4_share_disp_buf = 0;
136
6.49k
  fill_mem_ip.u4_deinterlace = mDeinterlace;
137
6.49k
  fill_mem_ip.e_output_format = mColorFormat;
138
139
6.49k
  fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_size =
140
6.49k
      sizeof(impeg2d_fill_mem_rec_ip_t);
141
6.49k
  fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_size =
142
6.49k
      sizeof(impeg2d_fill_mem_rec_op_t);
143
144
194k
  for (i = 0; i < numMemRecords; i++)
145
188k
    mMemRecords[i].u4_size = sizeof(iv_mem_rec_t);
146
147
6.49k
  ret = ivd_api_function(NULL, (void *)&fill_mem_ip, (void *)&fill_mem_op);
148
149
6.49k
  if (ret != IV_SUCCESS) {
150
0
    return;
151
0
  }
152
6.49k
  numMemRecords = fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled;
153
154
6.49k
  iv_mem_rec_t *ps_mem_rec = (iv_mem_rec_t *)mMemRecords;
155
156
194k
  for (i = 0; i < numMemRecords; i++) {
157
188k
    if (0 != posix_memalign(&ps_mem_rec->pv_base, ps_mem_rec->u4_mem_alignment,
158
188k
                            ps_mem_rec->u4_mem_size)) {
159
0
      return;
160
0
    }
161
162
188k
    if (ps_mem_rec->pv_base == NULL) {
163
0
      return;
164
0
    }
165
166
188k
    ps_mem_rec++;
167
188k
  }
168
169
6.49k
  mCodec = (iv_obj_t *)(iv_obj_t *)mMemRecords[0].pv_base;
170
6.49k
  mCodec->pv_fxns = fxns;
171
6.49k
  mCodec->u4_size = sizeof(iv_obj_t);
172
173
6.49k
  impeg2d_init_ip_t init_ip;
174
6.49k
  impeg2d_init_op_t init_op;
175
176
6.49k
  init_ip.s_ivd_init_ip_t.e_cmd = (IVD_API_COMMAND_TYPE_T)IV_CMD_INIT;
177
6.49k
  init_ip.s_ivd_init_ip_t.pv_mem_rec_location = mMemRecords;
178
6.49k
  init_ip.s_ivd_init_ip_t.u4_frm_max_wd = MAX_FRAME_WIDTH;
179
6.49k
  init_ip.s_ivd_init_ip_t.u4_frm_max_ht = MAX_FRAME_HEIGHT;
180
181
6.49k
  init_ip.u4_share_disp_buf = 0;
182
6.49k
  init_ip.u4_deinterlace = mDeinterlace;
183
6.49k
  init_ip.s_ivd_init_ip_t.u4_num_mem_rec = numMemRecords;
184
6.49k
  init_ip.s_ivd_init_ip_t.e_output_format = mColorFormat;
185
6.49k
  init_ip.s_ivd_init_ip_t.u4_size = sizeof(impeg2d_init_ip_t);
186
6.49k
  init_op.s_ivd_init_op_t.u4_size = sizeof(impeg2d_init_op_t);
187
188
6.49k
  ret = ivd_api_function(mCodec, (void *)&init_ip, (void *)&init_op);
189
6.49k
  if (ret != IV_SUCCESS) {
190
0
    return;
191
0
  }
192
6.49k
}
193
194
6.49k
void Codec::deleteCodec() {
195
6.49k
  IV_API_CALL_STATUS_T ret;
196
6.49k
  iv_retrieve_mem_rec_ip_t retrieve_ip;
197
6.49k
  iv_retrieve_mem_rec_op_t retrieve_op;
198
6.49k
  retrieve_ip.pv_mem_rec_location = (iv_mem_rec_t *)mMemRecords;
199
200
6.49k
  retrieve_ip.e_cmd = IV_CMD_RETRIEVE_MEMREC;
201
6.49k
  retrieve_ip.u4_size = sizeof(iv_retrieve_mem_rec_ip_t);
202
6.49k
  retrieve_op.u4_size = sizeof(iv_retrieve_mem_rec_op_t);
203
204
6.49k
  ret = ivd_api_function(mCodec, (void *)&retrieve_ip, (void *)&retrieve_op);
205
206
6.49k
  if (ret != IV_SUCCESS) {
207
0
    return;
208
0
  }
209
210
6.49k
  iv_mem_rec_t *ps_mem_rec = retrieve_ip.pv_mem_rec_location;
211
194k
  for (size_t i = 0; i < retrieve_op.u4_num_mem_rec_filled; i++) {
212
188k
    free(ps_mem_rec->pv_base);
213
188k
    ps_mem_rec++;
214
188k
  }
215
6.49k
  free(retrieve_ip.pv_mem_rec_location);
216
6.49k
}
217
218
7.88k
void Codec::resetCodec() {
219
7.88k
  ivd_ctl_reset_ip_t s_ctl_ip;
220
7.88k
  ivd_ctl_reset_op_t s_ctl_op;
221
222
7.88k
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
223
7.88k
  s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_RESET;
224
7.88k
  s_ctl_ip.u4_size = sizeof(ivd_ctl_reset_ip_t);
225
7.88k
  s_ctl_op.u4_size = sizeof(ivd_ctl_reset_op_t);
226
227
7.88k
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
228
7.88k
}
229
230
6.49k
void Codec::setCores() {
231
6.49k
  impeg2d_ctl_set_num_cores_ip_t s_ctl_ip;
232
6.49k
  impeg2d_ctl_set_num_cores_op_t s_ctl_op;
233
234
6.49k
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
235
6.49k
  s_ctl_ip.e_sub_cmd =
236
6.49k
      (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_SET_NUM_CORES;
237
6.49k
  s_ctl_ip.u4_num_cores = mNumCores;
238
6.49k
  s_ctl_ip.u4_size = sizeof(impeg2d_ctl_set_num_cores_ip_t);
239
6.49k
  s_ctl_op.u4_size = sizeof(impeg2d_ctl_set_num_cores_op_t);
240
241
6.49k
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
242
6.49k
}
243
244
12.9k
void Codec::setParams(IVD_VIDEO_DECODE_MODE_T mode) {
245
12.9k
  ivd_ctl_set_config_ip_t s_ctl_ip;
246
12.9k
  ivd_ctl_set_config_op_t s_ctl_op;
247
248
12.9k
  s_ctl_ip.u4_disp_wd = 0;
249
12.9k
  s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
250
12.9k
  s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
251
12.9k
  s_ctl_ip.e_vid_dec_mode = mode;
252
12.9k
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
253
12.9k
  s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
254
12.9k
  s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
255
12.9k
  s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);
256
257
12.9k
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
258
12.9k
}
259
260
6.49k
void Codec::setArchitecture(IVD_ARCH_T arch) {
261
6.49k
  impeg2d_ctl_set_processor_ip_t s_ctl_ip;
262
6.49k
  impeg2d_ctl_set_processor_op_t s_ctl_op;
263
264
6.49k
  s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
265
6.49k
  s_ctl_ip.e_sub_cmd =
266
6.49k
      (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_SET_PROCESSOR;
267
6.49k
  s_ctl_ip.u4_arch = arch;
268
6.49k
  s_ctl_ip.u4_soc = SOC_GENERIC;
269
6.49k
  s_ctl_ip.u4_size = sizeof(impeg2d_ctl_set_processor_ip_t);
270
6.49k
  s_ctl_op.u4_size = sizeof(impeg2d_ctl_set_processor_op_t);
271
272
6.49k
  ivd_api_function(mCodec, (void *)&s_ctl_ip, (void *)&s_ctl_op);
273
6.49k
}
274
18.1k
void Codec::freeFrame() {
275
46.2k
  for (int i = 0; i < mOutBufHandle.u4_num_bufs; i++) {
276
28.1k
    if (mOutBufHandle.pu1_bufs[i]) {
277
28.1k
      free(mOutBufHandle.pu1_bufs[i]);
278
28.1k
      mOutBufHandle.pu1_bufs[i] = nullptr;
279
28.1k
    }
280
28.1k
  }
281
18.1k
}
282
283
11.6k
void Codec::allocFrame() {
284
11.6k
  size_t sizes[4] = {0};
285
11.6k
  size_t num_bufs = 0;
286
287
11.6k
  freeFrame();
288
289
11.6k
  memset(&mOutBufHandle, 0, sizeof(mOutBufHandle));
290
291
11.6k
  switch (mColorFormat) {
292
3.08k
    case IV_YUV_420SP_UV:
293
3.08k
      [[fallthrough]];
294
6.81k
    case IV_YUV_420SP_VU:
295
6.81k
      sizes[0] = mWidth * mHeight;
296
6.81k
      sizes[1] = ALIGN2(mWidth) * ALIGN2(mHeight) >> 1;
297
6.81k
      num_bufs = 2;
298
6.81k
      break;
299
0
    case IV_YUV_422ILE:
300
0
      sizes[0] = mWidth * mHeight * 2;
301
0
      num_bufs = 1;
302
0
      break;
303
0
    case IV_RGB_565:
304
0
      sizes[0] = mWidth * mHeight * 2;
305
0
      num_bufs = 1;
306
0
      break;
307
0
    case IV_RGBA_8888:
308
0
      sizes[0] = mWidth * mHeight * 4;
309
0
      num_bufs = 1;
310
0
      break;
311
4.82k
    case IV_YUV_420P:
312
4.82k
      [[fallthrough]];
313
4.82k
    default:
314
4.82k
      sizes[0] = mWidth * mHeight;
315
4.82k
      sizes[1] = ALIGN2(mWidth) * ALIGN2(mHeight) >> 2;
316
4.82k
      sizes[2] = ALIGN2(mWidth) * ALIGN2(mHeight) >> 2;
317
4.82k
      num_bufs = 3;
318
4.82k
      break;
319
11.6k
  }
320
11.6k
  mOutBufHandle.u4_num_bufs = num_bufs;
321
39.7k
  for (int i = 0; i < num_bufs; i++) {
322
28.1k
    mOutBufHandle.u4_min_out_buf_size[i] = sizes[i];
323
28.1k
    void *buf = NULL;
324
28.1k
    if (0 != posix_memalign(&buf, 16, sizes[i])) {
325
0
      return;
326
0
    }
327
28.1k
    mOutBufHandle.pu1_bufs[i] = (UWORD8 *)buf;
328
28.1k
  }
329
11.6k
}
330
331
6.49k
void Codec::decodeHeader(const uint8_t *data, size_t size) {
332
6.49k
  setParams(IVD_DECODE_HEADER);
333
334
15.1k
  while (size > 0) {
335
14.8k
    IV_API_CALL_STATUS_T ret;
336
14.8k
    ivd_video_decode_ip_t dec_ip;
337
14.8k
    ivd_video_decode_op_t dec_op;
338
14.8k
    size_t bytes_consumed;
339
340
14.8k
    memset(&dec_ip, 0, sizeof(dec_ip));
341
14.8k
    memset(&dec_op, 0, sizeof(dec_op));
342
343
14.8k
    dec_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
344
14.8k
    dec_ip.u4_ts = 0;
345
14.8k
    dec_ip.pv_stream_buffer = (void *)data;
346
14.8k
    dec_ip.u4_num_Bytes = size;
347
14.8k
    dec_ip.u4_size = sizeof(ivd_video_decode_ip_t);
348
14.8k
    dec_op.u4_size = sizeof(ivd_video_decode_op_t);
349
350
14.8k
    ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
351
352
14.8k
    bytes_consumed = dec_op.u4_num_bytes_consumed;
353
    /* If no bytes are consumed, then consume 4 bytes to ensure fuzzer proceeds
354
     * to feed next data */
355
14.8k
    if (!bytes_consumed) bytes_consumed = 4;
356
357
14.8k
    bytes_consumed = std::min(size, bytes_consumed);
358
359
14.8k
    data += bytes_consumed;
360
14.8k
    size -= bytes_consumed;
361
362
14.8k
    mWidth = std::min(dec_op.u4_pic_wd, (UWORD32)10240);
363
14.8k
    mHeight = std::min(dec_op.u4_pic_ht, (UWORD32)10240);
364
365
    /* Break after successful header decode */
366
14.8k
    if (mWidth && mHeight) {
367
6.21k
      break;
368
6.21k
    }
369
14.8k
  }
370
  /* if width / height are invalid, set them to defaults */
371
6.49k
  if (!mWidth) mWidth = 1920;
372
6.49k
  if (!mHeight) mHeight = 1088;
373
6.49k
}
374
375
IV_API_CALL_STATUS_T Codec::decodeFrame(const uint8_t *data, size_t size,
376
39.4k
                                        size_t *bytesConsumed) {
377
39.4k
  IV_API_CALL_STATUS_T ret;
378
39.4k
  ivd_video_decode_ip_t dec_ip;
379
39.4k
  ivd_video_decode_op_t dec_op;
380
381
39.4k
  memset(&dec_ip, 0, sizeof(dec_ip));
382
39.4k
  memset(&dec_op, 0, sizeof(dec_op));
383
384
39.4k
  dec_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
385
39.4k
  dec_ip.u4_ts = 0;
386
39.4k
  dec_ip.pv_stream_buffer = (void *)data;
387
39.4k
  dec_ip.u4_num_Bytes = size;
388
39.4k
  dec_ip.u4_size = sizeof(ivd_video_decode_ip_t);
389
39.4k
  dec_ip.s_out_buffer = mOutBufHandle;
390
391
39.4k
  dec_op.u4_size = sizeof(ivd_video_decode_op_t);
392
393
39.4k
  ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
394
39.4k
  if (IMPEG2D_UNSUPPORTED_DIMENSIONS == dec_op.u4_error_code) {
395
    /* In case of unsupported resolution, reset codec */
396
6.18k
    resetCodec();
397
33.2k
  } else if (IVD_RES_CHANGED == (dec_op.u4_error_code & 0xFF)) {
398
    /* In case of change in resolution, reset codec and feed the same data
399
     * again */
400
1.69k
    resetCodec();
401
1.69k
    ret = ivd_api_function(mCodec, (void *)&dec_ip, (void *)&dec_op);
402
1.69k
  }
403
39.4k
  *bytesConsumed = dec_op.u4_num_bytes_consumed;
404
405
  /* If no bytes are consumed, then consume 4 bytes to ensure fuzzer proceeds
406
   * to feed next data */
407
39.4k
  if (!*bytesConsumed) *bytesConsumed = 4;
408
409
39.4k
  if (dec_op.u4_pic_wd && dec_op.u4_pic_ht &&
410
39.4k
      (mWidth != dec_op.u4_pic_wd || mHeight != dec_op.u4_pic_ht)) {
411
5.15k
    mWidth = std::min(dec_op.u4_pic_wd, (UWORD32)10240);
412
5.15k
    mHeight = std::min(dec_op.u4_pic_ht, (UWORD32)10240);
413
5.15k
    allocFrame();
414
5.15k
  }
415
416
39.4k
  return ret;
417
39.4k
}
418
419
6.49k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
420
6.49k
  if (size < 1) {
421
0
    return 0;
422
0
  }
423
6.49k
  size_t colorFormatOfst = std::min((size_t)OFFSET_COLOR_FORMAT, size - 1);
424
6.49k
  size_t numCoresOfst = std::min((size_t)OFFSET_NUM_CORES, size - 1);
425
6.49k
  size_t architectureOfst = std::min((size_t)OFFSET_ARCH, size - 1);
426
6.49k
  size_t architectureIdx = data[architectureOfst] % kSupportedArchitectures;
427
6.49k
  IVD_ARCH_T arch = (IVD_ARCH_T)supportedArchitectures[architectureIdx];
428
6.49k
  size_t colorFormatIdx = data[colorFormatOfst] % kSupportedColorFormats;
429
6.49k
  IV_COLOR_FORMAT_T colorFormat =
430
6.49k
      (IV_COLOR_FORMAT_T)(supportedColorFormats[colorFormatIdx]);
431
6.49k
  uint32_t numCores = (data[numCoresOfst] % kMaxCores) + 1;
432
6.49k
  size_t numDecodeCalls = 0;
433
6.49k
  Codec *codec = new Codec(colorFormat, numCores);
434
6.49k
  codec->createCodec();
435
6.49k
  codec->setArchitecture(arch);
436
6.49k
  codec->setCores();
437
6.49k
  codec->decodeHeader(data, size);
438
6.49k
  codec->setParams(IVD_DECODE_FRAME);
439
6.49k
  codec->allocFrame();
440
441
45.9k
  while (size > 0 && numDecodeCalls < kMaxNumDecodeCalls) {
442
39.4k
    IV_API_CALL_STATUS_T ret;
443
39.4k
    size_t bytesConsumed;
444
39.4k
    ret = codec->decodeFrame(data, size, &bytesConsumed);
445
446
39.4k
    bytesConsumed = std::min(size, bytesConsumed);
447
39.4k
    data += bytesConsumed;
448
39.4k
    size -= bytesConsumed;
449
39.4k
    numDecodeCalls++;
450
39.4k
  }
451
452
6.49k
  codec->freeFrame();
453
6.49k
  codec->deleteCodec();
454
6.49k
  delete codec;
455
6.49k
  return 0;
456
6.49k
}