cbs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include "config.h"
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/buffer.h"
  22. #include "libavutil/common.h"
  23. #include "cbs.h"
  24. #include "cbs_internal.h"
  25. static const CodedBitstreamType *cbs_type_table[] = {
  26. #if CONFIG_CBS_AV1
  27. &ff_cbs_type_av1,
  28. #endif
  29. #if CONFIG_CBS_H264
  30. &ff_cbs_type_h264,
  31. #endif
  32. #if CONFIG_CBS_H265
  33. &ff_cbs_type_h265,
  34. #endif
  35. #if CONFIG_CBS_JPEG
  36. &ff_cbs_type_jpeg,
  37. #endif
  38. #if CONFIG_CBS_MPEG2
  39. &ff_cbs_type_mpeg2,
  40. #endif
  41. #if CONFIG_CBS_VP9
  42. &ff_cbs_type_vp9,
  43. #endif
  44. };
  45. const enum AVCodecID ff_cbs_all_codec_ids[] = {
  46. #if CONFIG_CBS_AV1
  47. AV_CODEC_ID_AV1,
  48. #endif
  49. #if CONFIG_CBS_H264
  50. AV_CODEC_ID_H264,
  51. #endif
  52. #if CONFIG_CBS_H265
  53. AV_CODEC_ID_H265,
  54. #endif
  55. #if CONFIG_CBS_JPEG
  56. AV_CODEC_ID_MJPEG,
  57. #endif
  58. #if CONFIG_CBS_MPEG2
  59. AV_CODEC_ID_MPEG2VIDEO,
  60. #endif
  61. #if CONFIG_CBS_VP9
  62. AV_CODEC_ID_VP9,
  63. #endif
  64. AV_CODEC_ID_NONE
  65. };
  66. int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
  67. enum AVCodecID codec_id, void *log_ctx)
  68. {
  69. CodedBitstreamContext *ctx;
  70. const CodedBitstreamType *type;
  71. int i;
  72. type = NULL;
  73. for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
  74. if (cbs_type_table[i]->codec_id == codec_id) {
  75. type = cbs_type_table[i];
  76. break;
  77. }
  78. }
  79. if (!type)
  80. return AVERROR(EINVAL);
  81. ctx = av_mallocz(sizeof(*ctx));
  82. if (!ctx)
  83. return AVERROR(ENOMEM);
  84. ctx->log_ctx = log_ctx;
  85. ctx->codec = type;
  86. if (type->priv_data_size) {
  87. ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
  88. if (!ctx->priv_data) {
  89. av_freep(&ctx);
  90. return AVERROR(ENOMEM);
  91. }
  92. }
  93. ctx->decompose_unit_types = NULL;
  94. ctx->trace_enable = 0;
  95. ctx->trace_level = AV_LOG_TRACE;
  96. *ctx_ptr = ctx;
  97. return 0;
  98. }
  99. void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
  100. {
  101. CodedBitstreamContext *ctx = *ctx_ptr;
  102. if (!ctx)
  103. return;
  104. if (ctx->codec && ctx->codec->close)
  105. ctx->codec->close(ctx);
  106. av_freep(&ctx->write_buffer);
  107. av_freep(&ctx->priv_data);
  108. av_freep(ctx_ptr);
  109. }
  110. static void cbs_unit_uninit(CodedBitstreamUnit *unit)
  111. {
  112. av_buffer_unref(&unit->content_ref);
  113. unit->content = NULL;
  114. av_buffer_unref(&unit->data_ref);
  115. unit->data = NULL;
  116. unit->data_size = 0;
  117. unit->data_bit_padding = 0;
  118. }
  119. void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
  120. {
  121. int i;
  122. for (i = 0; i < frag->nb_units; i++)
  123. cbs_unit_uninit(&frag->units[i]);
  124. frag->nb_units = 0;
  125. av_buffer_unref(&frag->data_ref);
  126. frag->data = NULL;
  127. frag->data_size = 0;
  128. frag->data_bit_padding = 0;
  129. }
  130. void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
  131. {
  132. ff_cbs_fragment_reset(frag);
  133. av_freep(&frag->units);
  134. frag->nb_units_allocated = 0;
  135. }
  136. static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
  137. CodedBitstreamFragment *frag)
  138. {
  139. int err, i, j;
  140. for (i = 0; i < frag->nb_units; i++) {
  141. CodedBitstreamUnit *unit = &frag->units[i];
  142. if (ctx->decompose_unit_types) {
  143. for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
  144. if (ctx->decompose_unit_types[j] == unit->type)
  145. break;
  146. }
  147. if (j >= ctx->nb_decompose_unit_types)
  148. continue;
  149. }
  150. av_buffer_unref(&unit->content_ref);
  151. unit->content = NULL;
  152. av_assert0(unit->data && unit->data_ref);
  153. err = ctx->codec->read_unit(ctx, unit);
  154. if (err == AVERROR(ENOSYS)) {
  155. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  156. "Decomposition unimplemented for unit %d "
  157. "(type %"PRIu32").\n", i, unit->type);
  158. } else if (err < 0) {
  159. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
  160. "(type %"PRIu32").\n", i, unit->type);
  161. return err;
  162. }
  163. }
  164. return 0;
  165. }
  166. static int cbs_fill_fragment_data(CodedBitstreamFragment *frag,
  167. const uint8_t *data, size_t size)
  168. {
  169. av_assert0(!frag->data && !frag->data_ref);
  170. frag->data_ref =
  171. av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  172. if (!frag->data_ref)
  173. return AVERROR(ENOMEM);
  174. frag->data = frag->data_ref->data;
  175. frag->data_size = size;
  176. memcpy(frag->data, data, size);
  177. memset(frag->data + size, 0,
  178. AV_INPUT_BUFFER_PADDING_SIZE);
  179. return 0;
  180. }
  181. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  182. CodedBitstreamFragment *frag,
  183. const AVCodecParameters *par)
  184. {
  185. int err;
  186. err = cbs_fill_fragment_data(frag, par->extradata,
  187. par->extradata_size);
  188. if (err < 0)
  189. return err;
  190. err = ctx->codec->split_fragment(ctx, frag, 1);
  191. if (err < 0)
  192. return err;
  193. return cbs_read_fragment_content(ctx, frag);
  194. }
  195. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  196. CodedBitstreamFragment *frag,
  197. const AVPacket *pkt)
  198. {
  199. int err;
  200. if (pkt->buf) {
  201. frag->data_ref = av_buffer_ref(pkt->buf);
  202. if (!frag->data_ref)
  203. return AVERROR(ENOMEM);
  204. frag->data = pkt->data;
  205. frag->data_size = pkt->size;
  206. } else {
  207. err = cbs_fill_fragment_data(frag, pkt->data, pkt->size);
  208. if (err < 0)
  209. return err;
  210. }
  211. err = ctx->codec->split_fragment(ctx, frag, 0);
  212. if (err < 0)
  213. return err;
  214. return cbs_read_fragment_content(ctx, frag);
  215. }
  216. int ff_cbs_read(CodedBitstreamContext *ctx,
  217. CodedBitstreamFragment *frag,
  218. const uint8_t *data, size_t size)
  219. {
  220. int err;
  221. err = cbs_fill_fragment_data(frag, data, size);
  222. if (err < 0)
  223. return err;
  224. err = ctx->codec->split_fragment(ctx, frag, 0);
  225. if (err < 0)
  226. return err;
  227. return cbs_read_fragment_content(ctx, frag);
  228. }
  229. static int cbs_write_unit_data(CodedBitstreamContext *ctx,
  230. CodedBitstreamUnit *unit)
  231. {
  232. PutBitContext pbc;
  233. int ret;
  234. if (!ctx->write_buffer) {
  235. // Initial write buffer size is 1MB.
  236. ctx->write_buffer_size = 1024 * 1024;
  237. reallocate_and_try_again:
  238. ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
  239. if (ret < 0) {
  240. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  241. "sufficiently large write buffer (last attempt "
  242. "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
  243. return ret;
  244. }
  245. }
  246. init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
  247. ret = ctx->codec->write_unit(ctx, unit, &pbc);
  248. if (ret < 0) {
  249. if (ret == AVERROR(ENOSPC)) {
  250. // Overflow.
  251. if (ctx->write_buffer_size == INT_MAX / 8)
  252. return AVERROR(ENOMEM);
  253. ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
  254. goto reallocate_and_try_again;
  255. }
  256. // Write failed for some other reason.
  257. return ret;
  258. }
  259. // Overflow but we didn't notice.
  260. av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
  261. if (put_bits_count(&pbc) % 8)
  262. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  263. else
  264. unit->data_bit_padding = 0;
  265. flush_put_bits(&pbc);
  266. ret = ff_cbs_alloc_unit_data(unit, put_bits_count(&pbc) / 8);
  267. if (ret < 0)
  268. return ret;
  269. memcpy(unit->data, ctx->write_buffer, unit->data_size);
  270. return 0;
  271. }
  272. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  273. CodedBitstreamFragment *frag)
  274. {
  275. int err, i;
  276. for (i = 0; i < frag->nb_units; i++) {
  277. CodedBitstreamUnit *unit = &frag->units[i];
  278. if (!unit->content)
  279. continue;
  280. av_buffer_unref(&unit->data_ref);
  281. unit->data = NULL;
  282. err = cbs_write_unit_data(ctx, unit);
  283. if (err < 0) {
  284. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
  285. "(type %"PRIu32").\n", i, unit->type);
  286. return err;
  287. }
  288. av_assert0(unit->data && unit->data_ref);
  289. }
  290. av_buffer_unref(&frag->data_ref);
  291. frag->data = NULL;
  292. err = ctx->codec->assemble_fragment(ctx, frag);
  293. if (err < 0) {
  294. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
  295. return err;
  296. }
  297. av_assert0(frag->data && frag->data_ref);
  298. return 0;
  299. }
  300. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  301. AVCodecParameters *par,
  302. CodedBitstreamFragment *frag)
  303. {
  304. int err;
  305. err = ff_cbs_write_fragment_data(ctx, frag);
  306. if (err < 0)
  307. return err;
  308. av_freep(&par->extradata);
  309. par->extradata = av_malloc(frag->data_size +
  310. AV_INPUT_BUFFER_PADDING_SIZE);
  311. if (!par->extradata)
  312. return AVERROR(ENOMEM);
  313. memcpy(par->extradata, frag->data, frag->data_size);
  314. memset(par->extradata + frag->data_size, 0,
  315. AV_INPUT_BUFFER_PADDING_SIZE);
  316. par->extradata_size = frag->data_size;
  317. return 0;
  318. }
  319. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  320. AVPacket *pkt,
  321. CodedBitstreamFragment *frag)
  322. {
  323. AVBufferRef *buf;
  324. int err;
  325. err = ff_cbs_write_fragment_data(ctx, frag);
  326. if (err < 0)
  327. return err;
  328. buf = av_buffer_ref(frag->data_ref);
  329. if (!buf)
  330. return AVERROR(ENOMEM);
  331. av_buffer_unref(&pkt->buf);
  332. pkt->buf = buf;
  333. pkt->data = frag->data;
  334. pkt->size = frag->data_size;
  335. return 0;
  336. }
  337. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  338. const char *name)
  339. {
  340. if (!ctx->trace_enable)
  341. return;
  342. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  343. }
  344. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  345. const char *str, const int *subscripts,
  346. const char *bits, int64_t value)
  347. {
  348. char name[256];
  349. size_t name_len, bits_len;
  350. int pad, subs, i, j, k, n;
  351. if (!ctx->trace_enable)
  352. return;
  353. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  354. subs = subscripts ? subscripts[0] : 0;
  355. n = 0;
  356. for (i = j = 0; str[i];) {
  357. if (str[i] == '[') {
  358. if (n < subs) {
  359. ++n;
  360. k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
  361. av_assert0(k > 0 && j + k < sizeof(name));
  362. j += k;
  363. for (++i; str[i] && str[i] != ']'; i++);
  364. av_assert0(str[i] == ']');
  365. } else {
  366. while (str[i] && str[i] != ']')
  367. name[j++] = str[i++];
  368. av_assert0(str[i] == ']');
  369. }
  370. } else {
  371. av_assert0(j + 1 < sizeof(name));
  372. name[j++] = str[i++];
  373. }
  374. }
  375. av_assert0(j + 1 < sizeof(name));
  376. name[j] = 0;
  377. av_assert0(n == subs);
  378. name_len = strlen(name);
  379. bits_len = strlen(bits);
  380. if (name_len + bits_len > 60)
  381. pad = bits_len + 2;
  382. else
  383. pad = 61 - name_len;
  384. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  385. position, name, pad, bits, value);
  386. }
  387. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  388. int width, const char *name,
  389. const int *subscripts, uint32_t *write_to,
  390. uint32_t range_min, uint32_t range_max)
  391. {
  392. uint32_t value;
  393. int position;
  394. av_assert0(width > 0 && width <= 32);
  395. if (get_bits_left(gbc) < width) {
  396. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  397. "%s: bitstream ended.\n", name);
  398. return AVERROR_INVALIDDATA;
  399. }
  400. if (ctx->trace_enable)
  401. position = get_bits_count(gbc);
  402. value = get_bits_long(gbc, width);
  403. if (ctx->trace_enable) {
  404. char bits[33];
  405. int i;
  406. for (i = 0; i < width; i++)
  407. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  408. bits[i] = 0;
  409. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  410. bits, value);
  411. }
  412. if (value < range_min || value > range_max) {
  413. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  414. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  415. name, value, range_min, range_max);
  416. return AVERROR_INVALIDDATA;
  417. }
  418. *write_to = value;
  419. return 0;
  420. }
  421. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  422. int width, const char *name,
  423. const int *subscripts, uint32_t value,
  424. uint32_t range_min, uint32_t range_max)
  425. {
  426. av_assert0(width > 0 && width <= 32);
  427. if (value < range_min || value > range_max) {
  428. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  429. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  430. name, value, range_min, range_max);
  431. return AVERROR_INVALIDDATA;
  432. }
  433. if (put_bits_left(pbc) < width)
  434. return AVERROR(ENOSPC);
  435. if (ctx->trace_enable) {
  436. char bits[33];
  437. int i;
  438. for (i = 0; i < width; i++)
  439. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  440. bits[i] = 0;
  441. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  442. name, subscripts, bits, value);
  443. }
  444. if (width < 32)
  445. put_bits(pbc, width, value);
  446. else
  447. put_bits32(pbc, value);
  448. return 0;
  449. }
  450. int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
  451. int width, const char *name,
  452. const int *subscripts, int32_t *write_to,
  453. int32_t range_min, int32_t range_max)
  454. {
  455. int32_t value;
  456. int position;
  457. av_assert0(width > 0 && width <= 32);
  458. if (get_bits_left(gbc) < width) {
  459. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  460. "%s: bitstream ended.\n", name);
  461. return AVERROR_INVALIDDATA;
  462. }
  463. if (ctx->trace_enable)
  464. position = get_bits_count(gbc);
  465. value = get_sbits_long(gbc, width);
  466. if (ctx->trace_enable) {
  467. char bits[33];
  468. int i;
  469. for (i = 0; i < width; i++)
  470. bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
  471. bits[i] = 0;
  472. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  473. bits, value);
  474. }
  475. if (value < range_min || value > range_max) {
  476. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  477. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  478. name, value, range_min, range_max);
  479. return AVERROR_INVALIDDATA;
  480. }
  481. *write_to = value;
  482. return 0;
  483. }
  484. int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
  485. int width, const char *name,
  486. const int *subscripts, int32_t value,
  487. int32_t range_min, int32_t range_max)
  488. {
  489. av_assert0(width > 0 && width <= 32);
  490. if (value < range_min || value > range_max) {
  491. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  492. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  493. name, value, range_min, range_max);
  494. return AVERROR_INVALIDDATA;
  495. }
  496. if (put_bits_left(pbc) < width)
  497. return AVERROR(ENOSPC);
  498. if (ctx->trace_enable) {
  499. char bits[33];
  500. int i;
  501. for (i = 0; i < width; i++)
  502. bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
  503. bits[i] = 0;
  504. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  505. name, subscripts, bits, value);
  506. }
  507. if (width < 32)
  508. put_sbits(pbc, width, value);
  509. else
  510. put_bits32(pbc, value);
  511. return 0;
  512. }
  513. int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
  514. size_t size,
  515. void (*free)(void *opaque, uint8_t *data))
  516. {
  517. av_assert0(!unit->content && !unit->content_ref);
  518. unit->content = av_mallocz(size);
  519. if (!unit->content)
  520. return AVERROR(ENOMEM);
  521. unit->content_ref = av_buffer_create(unit->content, size,
  522. free, NULL, 0);
  523. if (!unit->content_ref) {
  524. av_freep(&unit->content);
  525. return AVERROR(ENOMEM);
  526. }
  527. return 0;
  528. }
  529. int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit,
  530. size_t size)
  531. {
  532. av_assert0(!unit->data && !unit->data_ref);
  533. unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  534. if (!unit->data_ref)
  535. return AVERROR(ENOMEM);
  536. unit->data = unit->data_ref->data;
  537. unit->data_size = size;
  538. memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  539. return 0;
  540. }
  541. static int cbs_insert_unit(CodedBitstreamFragment *frag,
  542. int position)
  543. {
  544. CodedBitstreamUnit *units;
  545. if (frag->nb_units < frag->nb_units_allocated) {
  546. units = frag->units;
  547. if (position < frag->nb_units)
  548. memmove(units + position + 1, units + position,
  549. (frag->nb_units - position) * sizeof(*units));
  550. } else {
  551. units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
  552. if (!units)
  553. return AVERROR(ENOMEM);
  554. frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
  555. if (position > 0)
  556. memcpy(units, frag->units, position * sizeof(*units));
  557. if (position < frag->nb_units)
  558. memcpy(units + position + 1, frag->units + position,
  559. (frag->nb_units - position) * sizeof(*units));
  560. }
  561. memset(units + position, 0, sizeof(*units));
  562. if (units != frag->units) {
  563. av_free(frag->units);
  564. frag->units = units;
  565. }
  566. ++frag->nb_units;
  567. return 0;
  568. }
  569. int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
  570. int position,
  571. CodedBitstreamUnitType type,
  572. void *content,
  573. AVBufferRef *content_buf)
  574. {
  575. CodedBitstreamUnit *unit;
  576. AVBufferRef *content_ref;
  577. int err;
  578. if (position == -1)
  579. position = frag->nb_units;
  580. av_assert0(position >= 0 && position <= frag->nb_units);
  581. if (content_buf) {
  582. content_ref = av_buffer_ref(content_buf);
  583. if (!content_ref)
  584. return AVERROR(ENOMEM);
  585. } else {
  586. content_ref = NULL;
  587. }
  588. err = cbs_insert_unit(frag, position);
  589. if (err < 0) {
  590. av_buffer_unref(&content_ref);
  591. return err;
  592. }
  593. unit = &frag->units[position];
  594. unit->type = type;
  595. unit->content = content;
  596. unit->content_ref = content_ref;
  597. return 0;
  598. }
  599. int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag,
  600. int position,
  601. CodedBitstreamUnitType type,
  602. uint8_t *data, size_t data_size,
  603. AVBufferRef *data_buf)
  604. {
  605. CodedBitstreamUnit *unit;
  606. AVBufferRef *data_ref;
  607. int err;
  608. if (position == -1)
  609. position = frag->nb_units;
  610. av_assert0(position >= 0 && position <= frag->nb_units);
  611. if (data_buf)
  612. data_ref = av_buffer_ref(data_buf);
  613. else
  614. data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
  615. if (!data_ref) {
  616. if (!data_buf)
  617. av_free(data);
  618. return AVERROR(ENOMEM);
  619. }
  620. err = cbs_insert_unit(frag, position);
  621. if (err < 0) {
  622. av_buffer_unref(&data_ref);
  623. return err;
  624. }
  625. unit = &frag->units[position];
  626. unit->type = type;
  627. unit->data = data;
  628. unit->data_size = data_size;
  629. unit->data_ref = data_ref;
  630. return 0;
  631. }
  632. void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
  633. int position)
  634. {
  635. av_assert0(0 <= position && position < frag->nb_units
  636. && "Unit to be deleted not in fragment.");
  637. cbs_unit_uninit(&frag->units[position]);
  638. --frag->nb_units;
  639. if (frag->nb_units > 0)
  640. memmove(frag->units + position,
  641. frag->units + position + 1,
  642. (frag->nb_units - position) * sizeof(*frag->units));
  643. }
  644. static void cbs_default_free_unit_content(void *opaque, uint8_t *data)
  645. {
  646. const CodedBitstreamUnitTypeDescriptor *desc = opaque;
  647. if (desc->content_type == CBS_CONTENT_TYPE_INTERNAL_REFS) {
  648. int i;
  649. for (i = 0; i < desc->nb_ref_offsets; i++) {
  650. void **ptr = (void**)(data + desc->ref_offsets[i]);
  651. av_buffer_unref((AVBufferRef**)(ptr + 1));
  652. }
  653. }
  654. av_free(data);
  655. }
  656. static const CodedBitstreamUnitTypeDescriptor
  657. *cbs_find_unit_type_desc(CodedBitstreamContext *ctx,
  658. CodedBitstreamUnit *unit)
  659. {
  660. const CodedBitstreamUnitTypeDescriptor *desc;
  661. int i, j;
  662. if (!ctx->codec->unit_types)
  663. return NULL;
  664. for (i = 0;; i++) {
  665. desc = &ctx->codec->unit_types[i];
  666. if (desc->nb_unit_types == 0)
  667. break;
  668. if (desc->nb_unit_types == CBS_UNIT_TYPE_RANGE) {
  669. if (unit->type >= desc->unit_type_range_start &&
  670. unit->type <= desc->unit_type_range_end)
  671. return desc;
  672. } else {
  673. for (j = 0; j < desc->nb_unit_types; j++) {
  674. if (desc->unit_types[j] == unit->type)
  675. return desc;
  676. }
  677. }
  678. }
  679. return NULL;
  680. }
  681. int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx,
  682. CodedBitstreamUnit *unit)
  683. {
  684. const CodedBitstreamUnitTypeDescriptor *desc;
  685. av_assert0(!unit->content && !unit->content_ref);
  686. desc = cbs_find_unit_type_desc(ctx, unit);
  687. if (!desc)
  688. return AVERROR(ENOSYS);
  689. unit->content = av_mallocz(desc->content_size);
  690. if (!unit->content)
  691. return AVERROR(ENOMEM);
  692. unit->content_ref =
  693. av_buffer_create(unit->content, desc->content_size,
  694. desc->content_free ? desc->content_free
  695. : cbs_default_free_unit_content,
  696. (void*)desc, 0);
  697. if (!unit->content_ref) {
  698. av_freep(&unit->content);
  699. return AVERROR(ENOMEM);
  700. }
  701. return 0;
  702. }