You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1059 lines
28KB

  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 "libavutil/opt.h"
  24. #include "cbs.h"
  25. #include "cbs_internal.h"
  26. static const CodedBitstreamType *const cbs_type_table[] = {
  27. #if CONFIG_CBS_AV1
  28. &ff_cbs_type_av1,
  29. #endif
  30. #if CONFIG_CBS_H264
  31. &ff_cbs_type_h264,
  32. #endif
  33. #if CONFIG_CBS_H265
  34. &ff_cbs_type_h265,
  35. #endif
  36. #if CONFIG_CBS_JPEG
  37. &ff_cbs_type_jpeg,
  38. #endif
  39. #if CONFIG_CBS_MPEG2
  40. &ff_cbs_type_mpeg2,
  41. #endif
  42. #if CONFIG_CBS_VP9
  43. &ff_cbs_type_vp9,
  44. #endif
  45. };
  46. const enum AVCodecID ff_cbs_all_codec_ids[] = {
  47. #if CONFIG_CBS_AV1
  48. AV_CODEC_ID_AV1,
  49. #endif
  50. #if CONFIG_CBS_H264
  51. AV_CODEC_ID_H264,
  52. #endif
  53. #if CONFIG_CBS_H265
  54. AV_CODEC_ID_H265,
  55. #endif
  56. #if CONFIG_CBS_JPEG
  57. AV_CODEC_ID_MJPEG,
  58. #endif
  59. #if CONFIG_CBS_MPEG2
  60. AV_CODEC_ID_MPEG2VIDEO,
  61. #endif
  62. #if CONFIG_CBS_VP9
  63. AV_CODEC_ID_VP9,
  64. #endif
  65. AV_CODEC_ID_NONE
  66. };
  67. int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
  68. enum AVCodecID codec_id, void *log_ctx)
  69. {
  70. CodedBitstreamContext *ctx;
  71. const CodedBitstreamType *type;
  72. int i;
  73. type = NULL;
  74. for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
  75. if (cbs_type_table[i]->codec_id == codec_id) {
  76. type = cbs_type_table[i];
  77. break;
  78. }
  79. }
  80. if (!type)
  81. return AVERROR(EINVAL);
  82. ctx = av_mallocz(sizeof(*ctx));
  83. if (!ctx)
  84. return AVERROR(ENOMEM);
  85. ctx->log_ctx = log_ctx;
  86. ctx->codec = type; /* Must be before any error */
  87. if (type->priv_data_size) {
  88. ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
  89. if (!ctx->priv_data) {
  90. av_freep(&ctx);
  91. return AVERROR(ENOMEM);
  92. }
  93. if (type->priv_class) {
  94. *(const AVClass **)ctx->priv_data = type->priv_class;
  95. av_opt_set_defaults(ctx->priv_data);
  96. }
  97. }
  98. ctx->decompose_unit_types = NULL;
  99. ctx->trace_enable = 0;
  100. ctx->trace_level = AV_LOG_TRACE;
  101. *ctx_ptr = ctx;
  102. return 0;
  103. }
  104. void ff_cbs_flush(CodedBitstreamContext *ctx)
  105. {
  106. if (ctx->codec->flush)
  107. ctx->codec->flush(ctx);
  108. }
  109. void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
  110. {
  111. CodedBitstreamContext *ctx = *ctx_ptr;
  112. if (!ctx)
  113. return;
  114. if (ctx->codec->close)
  115. ctx->codec->close(ctx);
  116. av_freep(&ctx->write_buffer);
  117. if (ctx->codec->priv_class && ctx->priv_data)
  118. av_opt_free(ctx->priv_data);
  119. av_freep(&ctx->priv_data);
  120. av_freep(ctx_ptr);
  121. }
  122. static void cbs_unit_uninit(CodedBitstreamUnit *unit)
  123. {
  124. av_buffer_unref(&unit->content_ref);
  125. unit->content = NULL;
  126. av_buffer_unref(&unit->data_ref);
  127. unit->data = NULL;
  128. unit->data_size = 0;
  129. unit->data_bit_padding = 0;
  130. }
  131. void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
  132. {
  133. int i;
  134. for (i = 0; i < frag->nb_units; i++)
  135. cbs_unit_uninit(&frag->units[i]);
  136. frag->nb_units = 0;
  137. av_buffer_unref(&frag->data_ref);
  138. frag->data = NULL;
  139. frag->data_size = 0;
  140. frag->data_bit_padding = 0;
  141. }
  142. void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
  143. {
  144. ff_cbs_fragment_reset(frag);
  145. av_freep(&frag->units);
  146. frag->nb_units_allocated = 0;
  147. }
  148. static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
  149. CodedBitstreamFragment *frag)
  150. {
  151. int err, i, j;
  152. for (i = 0; i < frag->nb_units; i++) {
  153. CodedBitstreamUnit *unit = &frag->units[i];
  154. if (ctx->decompose_unit_types) {
  155. for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
  156. if (ctx->decompose_unit_types[j] == unit->type)
  157. break;
  158. }
  159. if (j >= ctx->nb_decompose_unit_types)
  160. continue;
  161. }
  162. av_buffer_unref(&unit->content_ref);
  163. unit->content = NULL;
  164. av_assert0(unit->data && unit->data_ref);
  165. err = ctx->codec->read_unit(ctx, unit);
  166. if (err == AVERROR(ENOSYS)) {
  167. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  168. "Decomposition unimplemented for unit %d "
  169. "(type %"PRIu32").\n", i, unit->type);
  170. } else if (err == AVERROR(EAGAIN)) {
  171. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  172. "Skipping decomposition of unit %d "
  173. "(type %"PRIu32").\n", i, unit->type);
  174. av_buffer_unref(&unit->content_ref);
  175. unit->content = NULL;
  176. } else if (err < 0) {
  177. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
  178. "(type %"PRIu32").\n", i, unit->type);
  179. return err;
  180. }
  181. }
  182. return 0;
  183. }
  184. static int cbs_fill_fragment_data(CodedBitstreamFragment *frag,
  185. const uint8_t *data, size_t size)
  186. {
  187. av_assert0(!frag->data && !frag->data_ref);
  188. frag->data_ref =
  189. av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  190. if (!frag->data_ref)
  191. return AVERROR(ENOMEM);
  192. frag->data = frag->data_ref->data;
  193. frag->data_size = size;
  194. memcpy(frag->data, data, size);
  195. memset(frag->data + size, 0,
  196. AV_INPUT_BUFFER_PADDING_SIZE);
  197. return 0;
  198. }
  199. static int cbs_read_data(CodedBitstreamContext *ctx,
  200. CodedBitstreamFragment *frag,
  201. AVBufferRef *buf,
  202. const uint8_t *data, size_t size,
  203. int header)
  204. {
  205. int err;
  206. if (buf) {
  207. frag->data_ref = av_buffer_ref(buf);
  208. if (!frag->data_ref)
  209. return AVERROR(ENOMEM);
  210. frag->data = (uint8_t *)data;
  211. frag->data_size = size;
  212. } else {
  213. err = cbs_fill_fragment_data(frag, data, size);
  214. if (err < 0)
  215. return err;
  216. }
  217. err = ctx->codec->split_fragment(ctx, frag, header);
  218. if (err < 0)
  219. return err;
  220. return cbs_read_fragment_content(ctx, frag);
  221. }
  222. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  223. CodedBitstreamFragment *frag,
  224. const AVCodecParameters *par)
  225. {
  226. return cbs_read_data(ctx, frag, NULL,
  227. par->extradata,
  228. par->extradata_size, 1);
  229. }
  230. int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx,
  231. CodedBitstreamFragment *frag,
  232. const AVCodecContext *avctx)
  233. {
  234. return cbs_read_data(ctx, frag, NULL,
  235. avctx->extradata,
  236. avctx->extradata_size, 1);
  237. }
  238. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  239. CodedBitstreamFragment *frag,
  240. const AVPacket *pkt)
  241. {
  242. return cbs_read_data(ctx, frag, pkt->buf,
  243. pkt->data, pkt->size, 0);
  244. }
  245. int ff_cbs_read(CodedBitstreamContext *ctx,
  246. CodedBitstreamFragment *frag,
  247. const uint8_t *data, size_t size)
  248. {
  249. return cbs_read_data(ctx, frag, NULL,
  250. data, size, 0);
  251. }
  252. static int cbs_write_unit_data(CodedBitstreamContext *ctx,
  253. CodedBitstreamUnit *unit)
  254. {
  255. PutBitContext pbc;
  256. int ret;
  257. if (!ctx->write_buffer) {
  258. // Initial write buffer size is 1MB.
  259. ctx->write_buffer_size = 1024 * 1024;
  260. reallocate_and_try_again:
  261. ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
  262. if (ret < 0) {
  263. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  264. "sufficiently large write buffer (last attempt "
  265. "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
  266. return ret;
  267. }
  268. }
  269. init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
  270. ret = ctx->codec->write_unit(ctx, unit, &pbc);
  271. if (ret < 0) {
  272. if (ret == AVERROR(ENOSPC)) {
  273. // Overflow.
  274. if (ctx->write_buffer_size == INT_MAX / 8)
  275. return AVERROR(ENOMEM);
  276. ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
  277. goto reallocate_and_try_again;
  278. }
  279. // Write failed for some other reason.
  280. return ret;
  281. }
  282. // Overflow but we didn't notice.
  283. av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
  284. if (put_bits_count(&pbc) % 8)
  285. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  286. else
  287. unit->data_bit_padding = 0;
  288. flush_put_bits(&pbc);
  289. ret = ff_cbs_alloc_unit_data(unit, put_bytes_output(&pbc));
  290. if (ret < 0)
  291. return ret;
  292. memcpy(unit->data, ctx->write_buffer, unit->data_size);
  293. return 0;
  294. }
  295. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  296. CodedBitstreamFragment *frag)
  297. {
  298. int err, i;
  299. for (i = 0; i < frag->nb_units; i++) {
  300. CodedBitstreamUnit *unit = &frag->units[i];
  301. if (!unit->content)
  302. continue;
  303. av_buffer_unref(&unit->data_ref);
  304. unit->data = NULL;
  305. err = cbs_write_unit_data(ctx, unit);
  306. if (err < 0) {
  307. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
  308. "(type %"PRIu32").\n", i, unit->type);
  309. return err;
  310. }
  311. av_assert0(unit->data && unit->data_ref);
  312. }
  313. av_buffer_unref(&frag->data_ref);
  314. frag->data = NULL;
  315. err = ctx->codec->assemble_fragment(ctx, frag);
  316. if (err < 0) {
  317. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
  318. return err;
  319. }
  320. av_assert0(frag->data && frag->data_ref);
  321. return 0;
  322. }
  323. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  324. AVCodecParameters *par,
  325. CodedBitstreamFragment *frag)
  326. {
  327. int err;
  328. err = ff_cbs_write_fragment_data(ctx, frag);
  329. if (err < 0)
  330. return err;
  331. av_freep(&par->extradata);
  332. par->extradata = av_malloc(frag->data_size +
  333. AV_INPUT_BUFFER_PADDING_SIZE);
  334. if (!par->extradata)
  335. return AVERROR(ENOMEM);
  336. memcpy(par->extradata, frag->data, frag->data_size);
  337. memset(par->extradata + frag->data_size, 0,
  338. AV_INPUT_BUFFER_PADDING_SIZE);
  339. par->extradata_size = frag->data_size;
  340. return 0;
  341. }
  342. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  343. AVPacket *pkt,
  344. CodedBitstreamFragment *frag)
  345. {
  346. AVBufferRef *buf;
  347. int err;
  348. err = ff_cbs_write_fragment_data(ctx, frag);
  349. if (err < 0)
  350. return err;
  351. buf = av_buffer_ref(frag->data_ref);
  352. if (!buf)
  353. return AVERROR(ENOMEM);
  354. av_buffer_unref(&pkt->buf);
  355. pkt->buf = buf;
  356. pkt->data = frag->data;
  357. pkt->size = frag->data_size;
  358. return 0;
  359. }
  360. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  361. const char *name)
  362. {
  363. if (!ctx->trace_enable)
  364. return;
  365. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  366. }
  367. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  368. const char *str, const int *subscripts,
  369. const char *bits, int64_t value)
  370. {
  371. char name[256];
  372. size_t name_len, bits_len;
  373. int pad, subs, i, j, k, n;
  374. if (!ctx->trace_enable)
  375. return;
  376. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  377. subs = subscripts ? subscripts[0] : 0;
  378. n = 0;
  379. for (i = j = 0; str[i];) {
  380. if (str[i] == '[') {
  381. if (n < subs) {
  382. ++n;
  383. k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
  384. av_assert0(k > 0 && j + k < sizeof(name));
  385. j += k;
  386. for (++i; str[i] && str[i] != ']'; i++);
  387. av_assert0(str[i] == ']');
  388. } else {
  389. while (str[i] && str[i] != ']')
  390. name[j++] = str[i++];
  391. av_assert0(str[i] == ']');
  392. }
  393. } else {
  394. av_assert0(j + 1 < sizeof(name));
  395. name[j++] = str[i++];
  396. }
  397. }
  398. av_assert0(j + 1 < sizeof(name));
  399. name[j] = 0;
  400. av_assert0(n == subs);
  401. name_len = strlen(name);
  402. bits_len = strlen(bits);
  403. if (name_len + bits_len > 60)
  404. pad = bits_len + 2;
  405. else
  406. pad = 61 - name_len;
  407. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  408. position, name, pad, bits, value);
  409. }
  410. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  411. int width, const char *name,
  412. const int *subscripts, uint32_t *write_to,
  413. uint32_t range_min, uint32_t range_max)
  414. {
  415. uint32_t value;
  416. int position;
  417. av_assert0(width > 0 && width <= 32);
  418. if (get_bits_left(gbc) < width) {
  419. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  420. "%s: bitstream ended.\n", name);
  421. return AVERROR_INVALIDDATA;
  422. }
  423. if (ctx->trace_enable)
  424. position = get_bits_count(gbc);
  425. value = get_bits_long(gbc, width);
  426. if (ctx->trace_enable) {
  427. char bits[33];
  428. int i;
  429. for (i = 0; i < width; i++)
  430. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  431. bits[i] = 0;
  432. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  433. bits, value);
  434. }
  435. if (value < range_min || value > range_max) {
  436. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  437. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  438. name, value, range_min, range_max);
  439. return AVERROR_INVALIDDATA;
  440. }
  441. *write_to = value;
  442. return 0;
  443. }
  444. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  445. int width, const char *name,
  446. const int *subscripts, uint32_t value,
  447. uint32_t range_min, uint32_t range_max)
  448. {
  449. av_assert0(width > 0 && width <= 32);
  450. if (value < range_min || value > range_max) {
  451. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  452. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  453. name, value, range_min, range_max);
  454. return AVERROR_INVALIDDATA;
  455. }
  456. if (put_bits_left(pbc) < width)
  457. return AVERROR(ENOSPC);
  458. if (ctx->trace_enable) {
  459. char bits[33];
  460. int i;
  461. for (i = 0; i < width; i++)
  462. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  463. bits[i] = 0;
  464. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  465. name, subscripts, bits, value);
  466. }
  467. if (width < 32)
  468. put_bits(pbc, width, value);
  469. else
  470. put_bits32(pbc, value);
  471. return 0;
  472. }
  473. int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
  474. int width, const char *name,
  475. const int *subscripts, int32_t *write_to,
  476. int32_t range_min, int32_t range_max)
  477. {
  478. int32_t value;
  479. int position;
  480. av_assert0(width > 0 && width <= 32);
  481. if (get_bits_left(gbc) < width) {
  482. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  483. "%s: bitstream ended.\n", name);
  484. return AVERROR_INVALIDDATA;
  485. }
  486. if (ctx->trace_enable)
  487. position = get_bits_count(gbc);
  488. value = get_sbits_long(gbc, width);
  489. if (ctx->trace_enable) {
  490. char bits[33];
  491. int i;
  492. for (i = 0; i < width; i++)
  493. bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
  494. bits[i] = 0;
  495. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  496. bits, value);
  497. }
  498. if (value < range_min || value > range_max) {
  499. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  500. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  501. name, value, range_min, range_max);
  502. return AVERROR_INVALIDDATA;
  503. }
  504. *write_to = value;
  505. return 0;
  506. }
  507. int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
  508. int width, const char *name,
  509. const int *subscripts, int32_t value,
  510. int32_t range_min, int32_t range_max)
  511. {
  512. av_assert0(width > 0 && width <= 32);
  513. if (value < range_min || value > range_max) {
  514. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  515. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  516. name, value, range_min, range_max);
  517. return AVERROR_INVALIDDATA;
  518. }
  519. if (put_bits_left(pbc) < width)
  520. return AVERROR(ENOSPC);
  521. if (ctx->trace_enable) {
  522. char bits[33];
  523. int i;
  524. for (i = 0; i < width; i++)
  525. bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
  526. bits[i] = 0;
  527. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  528. name, subscripts, bits, value);
  529. }
  530. if (width < 32)
  531. put_sbits(pbc, width, value);
  532. else
  533. put_bits32(pbc, value);
  534. return 0;
  535. }
  536. int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
  537. size_t size,
  538. void (*free)(void *opaque, uint8_t *data))
  539. {
  540. av_assert0(!unit->content && !unit->content_ref);
  541. unit->content = av_mallocz(size);
  542. if (!unit->content)
  543. return AVERROR(ENOMEM);
  544. unit->content_ref = av_buffer_create(unit->content, size,
  545. free, NULL, 0);
  546. if (!unit->content_ref) {
  547. av_freep(&unit->content);
  548. return AVERROR(ENOMEM);
  549. }
  550. return 0;
  551. }
  552. int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit,
  553. size_t size)
  554. {
  555. av_assert0(!unit->data && !unit->data_ref);
  556. unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  557. if (!unit->data_ref)
  558. return AVERROR(ENOMEM);
  559. unit->data = unit->data_ref->data;
  560. unit->data_size = size;
  561. memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  562. return 0;
  563. }
  564. static int cbs_insert_unit(CodedBitstreamFragment *frag,
  565. int position)
  566. {
  567. CodedBitstreamUnit *units;
  568. if (frag->nb_units < frag->nb_units_allocated) {
  569. units = frag->units;
  570. if (position < frag->nb_units)
  571. memmove(units + position + 1, units + position,
  572. (frag->nb_units - position) * sizeof(*units));
  573. } else {
  574. units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
  575. if (!units)
  576. return AVERROR(ENOMEM);
  577. frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
  578. if (position > 0)
  579. memcpy(units, frag->units, position * sizeof(*units));
  580. if (position < frag->nb_units)
  581. memcpy(units + position + 1, frag->units + position,
  582. (frag->nb_units - position) * sizeof(*units));
  583. }
  584. memset(units + position, 0, sizeof(*units));
  585. if (units != frag->units) {
  586. av_free(frag->units);
  587. frag->units = units;
  588. }
  589. ++frag->nb_units;
  590. return 0;
  591. }
  592. int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
  593. int position,
  594. CodedBitstreamUnitType type,
  595. void *content,
  596. AVBufferRef *content_buf)
  597. {
  598. CodedBitstreamUnit *unit;
  599. AVBufferRef *content_ref;
  600. int err;
  601. if (position == -1)
  602. position = frag->nb_units;
  603. av_assert0(position >= 0 && position <= frag->nb_units);
  604. if (content_buf) {
  605. content_ref = av_buffer_ref(content_buf);
  606. if (!content_ref)
  607. return AVERROR(ENOMEM);
  608. } else {
  609. content_ref = NULL;
  610. }
  611. err = cbs_insert_unit(frag, position);
  612. if (err < 0) {
  613. av_buffer_unref(&content_ref);
  614. return err;
  615. }
  616. unit = &frag->units[position];
  617. unit->type = type;
  618. unit->content = content;
  619. unit->content_ref = content_ref;
  620. return 0;
  621. }
  622. int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag,
  623. int position,
  624. CodedBitstreamUnitType type,
  625. uint8_t *data, size_t data_size,
  626. AVBufferRef *data_buf)
  627. {
  628. CodedBitstreamUnit *unit;
  629. AVBufferRef *data_ref;
  630. int err;
  631. if (position == -1)
  632. position = frag->nb_units;
  633. av_assert0(position >= 0 && position <= frag->nb_units);
  634. if (data_buf)
  635. data_ref = av_buffer_ref(data_buf);
  636. else
  637. data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
  638. if (!data_ref) {
  639. if (!data_buf)
  640. av_free(data);
  641. return AVERROR(ENOMEM);
  642. }
  643. err = cbs_insert_unit(frag, position);
  644. if (err < 0) {
  645. av_buffer_unref(&data_ref);
  646. return err;
  647. }
  648. unit = &frag->units[position];
  649. unit->type = type;
  650. unit->data = data;
  651. unit->data_size = data_size;
  652. unit->data_ref = data_ref;
  653. return 0;
  654. }
  655. void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
  656. int position)
  657. {
  658. av_assert0(0 <= position && position < frag->nb_units
  659. && "Unit to be deleted not in fragment.");
  660. cbs_unit_uninit(&frag->units[position]);
  661. --frag->nb_units;
  662. if (frag->nb_units > 0)
  663. memmove(frag->units + position,
  664. frag->units + position + 1,
  665. (frag->nb_units - position) * sizeof(*frag->units));
  666. }
  667. static void cbs_default_free_unit_content(void *opaque, uint8_t *data)
  668. {
  669. const CodedBitstreamUnitTypeDescriptor *desc = opaque;
  670. if (desc->content_type == CBS_CONTENT_TYPE_INTERNAL_REFS) {
  671. int i;
  672. for (i = 0; i < desc->nb_ref_offsets; i++) {
  673. void **ptr = (void**)(data + desc->ref_offsets[i]);
  674. av_buffer_unref((AVBufferRef**)(ptr + 1));
  675. }
  676. }
  677. av_free(data);
  678. }
  679. static const CodedBitstreamUnitTypeDescriptor
  680. *cbs_find_unit_type_desc(CodedBitstreamContext *ctx,
  681. CodedBitstreamUnit *unit)
  682. {
  683. const CodedBitstreamUnitTypeDescriptor *desc;
  684. int i, j;
  685. if (!ctx->codec->unit_types)
  686. return NULL;
  687. for (i = 0;; i++) {
  688. desc = &ctx->codec->unit_types[i];
  689. if (desc->nb_unit_types == 0)
  690. break;
  691. if (desc->nb_unit_types == CBS_UNIT_TYPE_RANGE) {
  692. if (unit->type >= desc->unit_type_range_start &&
  693. unit->type <= desc->unit_type_range_end)
  694. return desc;
  695. } else {
  696. for (j = 0; j < desc->nb_unit_types; j++) {
  697. if (desc->unit_types[j] == unit->type)
  698. return desc;
  699. }
  700. }
  701. }
  702. return NULL;
  703. }
  704. int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx,
  705. CodedBitstreamUnit *unit)
  706. {
  707. const CodedBitstreamUnitTypeDescriptor *desc;
  708. av_assert0(!unit->content && !unit->content_ref);
  709. desc = cbs_find_unit_type_desc(ctx, unit);
  710. if (!desc)
  711. return AVERROR(ENOSYS);
  712. unit->content = av_mallocz(desc->content_size);
  713. if (!unit->content)
  714. return AVERROR(ENOMEM);
  715. unit->content_ref =
  716. av_buffer_create(unit->content, desc->content_size,
  717. desc->content_free ? desc->content_free
  718. : cbs_default_free_unit_content,
  719. (void*)desc, 0);
  720. if (!unit->content_ref) {
  721. av_freep(&unit->content);
  722. return AVERROR(ENOMEM);
  723. }
  724. return 0;
  725. }
  726. static int cbs_clone_unit_content(AVBufferRef **clone_ref,
  727. CodedBitstreamUnit *unit,
  728. const CodedBitstreamUnitTypeDescriptor *desc)
  729. {
  730. uint8_t *src, *copy;
  731. uint8_t **src_ptr, **copy_ptr;
  732. AVBufferRef **src_buf, **copy_buf;
  733. int err, i;
  734. av_assert0(unit->content);
  735. src = unit->content;
  736. copy = av_memdup(src, desc->content_size);
  737. if (!copy)
  738. return AVERROR(ENOMEM);
  739. for (i = 0; i < desc->nb_ref_offsets; i++) {
  740. src_ptr = (uint8_t**)(src + desc->ref_offsets[i]);
  741. src_buf = (AVBufferRef**)(src_ptr + 1);
  742. copy_ptr = (uint8_t**)(copy + desc->ref_offsets[i]);
  743. copy_buf = (AVBufferRef**)(copy_ptr + 1);
  744. if (!*src_ptr) {
  745. av_assert0(!*src_buf);
  746. continue;
  747. }
  748. if (!*src_buf) {
  749. // We can't handle a non-refcounted pointer here - we don't
  750. // have enough information to handle whatever structure lies
  751. // at the other end of it.
  752. err = AVERROR(EINVAL);
  753. goto fail;
  754. }
  755. // src_ptr is required to point somewhere inside src_buf. If it
  756. // doesn't, there is a bug somewhere.
  757. av_assert0(*src_ptr >= (*src_buf)->data &&
  758. *src_ptr < (*src_buf)->data + (*src_buf)->size);
  759. *copy_buf = av_buffer_ref(*src_buf);
  760. if (!*copy_buf) {
  761. err = AVERROR(ENOMEM);
  762. goto fail;
  763. }
  764. *copy_ptr = (*copy_buf)->data + (*src_ptr - (*src_buf)->data);
  765. }
  766. *clone_ref = av_buffer_create(copy, desc->content_size,
  767. desc->content_free ? desc->content_free :
  768. cbs_default_free_unit_content,
  769. (void*)desc, 0);
  770. if (!*clone_ref) {
  771. err = AVERROR(ENOMEM);
  772. goto fail;
  773. }
  774. return 0;
  775. fail:
  776. for (--i; i >= 0; i--)
  777. av_buffer_unref((AVBufferRef**)(copy + desc->ref_offsets[i]));
  778. av_freep(&copy);
  779. *clone_ref = NULL;
  780. return err;
  781. }
  782. int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx,
  783. CodedBitstreamUnit *unit)
  784. {
  785. const CodedBitstreamUnitTypeDescriptor *desc;
  786. AVBufferRef *ref;
  787. int err;
  788. av_assert0(unit->content);
  789. if (unit->content_ref) {
  790. // Already refcounted, nothing to do.
  791. return 0;
  792. }
  793. desc = cbs_find_unit_type_desc(ctx, unit);
  794. if (!desc)
  795. return AVERROR(ENOSYS);
  796. switch (desc->content_type) {
  797. case CBS_CONTENT_TYPE_POD:
  798. ref = av_buffer_alloc(desc->content_size);
  799. if (!ref)
  800. return AVERROR(ENOMEM);
  801. memcpy(ref->data, unit->content, desc->content_size);
  802. err = 0;
  803. break;
  804. case CBS_CONTENT_TYPE_INTERNAL_REFS:
  805. err = cbs_clone_unit_content(&ref, unit, desc);
  806. break;
  807. case CBS_CONTENT_TYPE_COMPLEX:
  808. if (!desc->content_clone)
  809. return AVERROR_PATCHWELCOME;
  810. err = desc->content_clone(&ref, unit);
  811. break;
  812. default:
  813. av_assert0(0 && "Invalid content type.");
  814. }
  815. if (err < 0)
  816. return err;
  817. unit->content_ref = ref;
  818. unit->content = ref->data;
  819. return 0;
  820. }
  821. int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
  822. CodedBitstreamUnit *unit)
  823. {
  824. const CodedBitstreamUnitTypeDescriptor *desc;
  825. AVBufferRef *ref;
  826. int err;
  827. // This can only be applied to refcounted units.
  828. err = ff_cbs_make_unit_refcounted(ctx, unit);
  829. if (err < 0)
  830. return err;
  831. av_assert0(unit->content && unit->content_ref);
  832. if (av_buffer_is_writable(unit->content_ref))
  833. return 0;
  834. desc = cbs_find_unit_type_desc(ctx, unit);
  835. if (!desc)
  836. return AVERROR(ENOSYS);
  837. switch (desc->content_type) {
  838. case CBS_CONTENT_TYPE_POD:
  839. err = av_buffer_make_writable(&unit->content_ref);
  840. break;
  841. case CBS_CONTENT_TYPE_INTERNAL_REFS:
  842. err = cbs_clone_unit_content(&ref, unit, desc);
  843. break;
  844. case CBS_CONTENT_TYPE_COMPLEX:
  845. if (!desc->content_clone)
  846. return AVERROR_PATCHWELCOME;
  847. err = desc->content_clone(&ref, unit);
  848. break;
  849. default:
  850. av_assert0(0 && "Invalid content type.");
  851. }
  852. if (err < 0)
  853. return err;
  854. if (desc->content_type != CBS_CONTENT_TYPE_POD) {
  855. av_buffer_unref(&unit->content_ref);
  856. unit->content_ref = ref;
  857. }
  858. unit->content = unit->content_ref->data;
  859. return 0;
  860. }