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.

758 lines
20KB

  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. ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
  87. if (!ctx->priv_data) {
  88. av_freep(&ctx);
  89. return AVERROR(ENOMEM);
  90. }
  91. ctx->decompose_unit_types = NULL;
  92. ctx->trace_enable = 0;
  93. ctx->trace_level = AV_LOG_TRACE;
  94. *ctx_ptr = ctx;
  95. return 0;
  96. }
  97. void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
  98. {
  99. CodedBitstreamContext *ctx = *ctx_ptr;
  100. if (!ctx)
  101. return;
  102. if (ctx->codec && ctx->codec->close)
  103. ctx->codec->close(ctx);
  104. av_freep(&ctx->priv_data);
  105. av_freep(ctx_ptr);
  106. }
  107. static void cbs_unit_uninit(CodedBitstreamContext *ctx,
  108. CodedBitstreamUnit *unit)
  109. {
  110. av_buffer_unref(&unit->content_ref);
  111. unit->content = NULL;
  112. av_buffer_unref(&unit->data_ref);
  113. unit->data = NULL;
  114. unit->data_size = 0;
  115. unit->data_bit_padding = 0;
  116. }
  117. void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
  118. CodedBitstreamFragment *frag)
  119. {
  120. int i;
  121. for (i = 0; i < frag->nb_units; i++)
  122. cbs_unit_uninit(ctx, &frag->units[i]);
  123. frag->nb_units = 0;
  124. av_buffer_unref(&frag->data_ref);
  125. frag->data = NULL;
  126. frag->data_size = 0;
  127. frag->data_bit_padding = 0;
  128. }
  129. void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
  130. CodedBitstreamFragment *frag)
  131. {
  132. ff_cbs_fragment_reset(ctx, 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(CodedBitstreamContext *ctx,
  167. CodedBitstreamFragment *frag,
  168. const uint8_t *data, size_t size)
  169. {
  170. av_assert0(!frag->data && !frag->data_ref);
  171. frag->data_ref =
  172. av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  173. if (!frag->data_ref)
  174. return AVERROR(ENOMEM);
  175. frag->data = frag->data_ref->data;
  176. frag->data_size = size;
  177. memcpy(frag->data, data, size);
  178. memset(frag->data + size, 0,
  179. AV_INPUT_BUFFER_PADDING_SIZE);
  180. return 0;
  181. }
  182. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  183. CodedBitstreamFragment *frag,
  184. const AVCodecParameters *par)
  185. {
  186. int err;
  187. err = cbs_fill_fragment_data(ctx, frag, par->extradata,
  188. par->extradata_size);
  189. if (err < 0)
  190. return err;
  191. err = ctx->codec->split_fragment(ctx, frag, 1);
  192. if (err < 0)
  193. return err;
  194. return cbs_read_fragment_content(ctx, frag);
  195. }
  196. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  197. CodedBitstreamFragment *frag,
  198. const AVPacket *pkt)
  199. {
  200. int err;
  201. if (pkt->buf) {
  202. frag->data_ref = av_buffer_ref(pkt->buf);
  203. if (!frag->data_ref)
  204. return AVERROR(ENOMEM);
  205. frag->data = pkt->data;
  206. frag->data_size = pkt->size;
  207. } else {
  208. err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
  209. if (err < 0)
  210. return err;
  211. }
  212. err = ctx->codec->split_fragment(ctx, frag, 0);
  213. if (err < 0)
  214. return err;
  215. return cbs_read_fragment_content(ctx, frag);
  216. }
  217. int ff_cbs_read(CodedBitstreamContext *ctx,
  218. CodedBitstreamFragment *frag,
  219. const uint8_t *data, size_t size)
  220. {
  221. int err;
  222. err = cbs_fill_fragment_data(ctx, frag, data, size);
  223. if (err < 0)
  224. return err;
  225. err = ctx->codec->split_fragment(ctx, frag, 0);
  226. if (err < 0)
  227. return err;
  228. return cbs_read_fragment_content(ctx, frag);
  229. }
  230. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  231. CodedBitstreamFragment *frag)
  232. {
  233. int err, i;
  234. for (i = 0; i < frag->nb_units; i++) {
  235. CodedBitstreamUnit *unit = &frag->units[i];
  236. if (!unit->content)
  237. continue;
  238. av_buffer_unref(&unit->data_ref);
  239. unit->data = NULL;
  240. err = ctx->codec->write_unit(ctx, unit);
  241. if (err < 0) {
  242. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
  243. "(type %"PRIu32").\n", i, unit->type);
  244. return err;
  245. }
  246. av_assert0(unit->data && unit->data_ref);
  247. }
  248. av_buffer_unref(&frag->data_ref);
  249. frag->data = NULL;
  250. err = ctx->codec->assemble_fragment(ctx, frag);
  251. if (err < 0) {
  252. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
  253. return err;
  254. }
  255. av_assert0(frag->data && frag->data_ref);
  256. return 0;
  257. }
  258. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  259. AVCodecParameters *par,
  260. CodedBitstreamFragment *frag)
  261. {
  262. int err;
  263. err = ff_cbs_write_fragment_data(ctx, frag);
  264. if (err < 0)
  265. return err;
  266. av_freep(&par->extradata);
  267. par->extradata = av_malloc(frag->data_size +
  268. AV_INPUT_BUFFER_PADDING_SIZE);
  269. if (!par->extradata)
  270. return AVERROR(ENOMEM);
  271. memcpy(par->extradata, frag->data, frag->data_size);
  272. memset(par->extradata + frag->data_size, 0,
  273. AV_INPUT_BUFFER_PADDING_SIZE);
  274. par->extradata_size = frag->data_size;
  275. return 0;
  276. }
  277. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  278. AVPacket *pkt,
  279. CodedBitstreamFragment *frag)
  280. {
  281. AVBufferRef *buf;
  282. int err;
  283. err = ff_cbs_write_fragment_data(ctx, frag);
  284. if (err < 0)
  285. return err;
  286. buf = av_buffer_ref(frag->data_ref);
  287. if (!buf)
  288. return AVERROR(ENOMEM);
  289. av_buffer_unref(&pkt->buf);
  290. pkt->buf = buf;
  291. pkt->data = frag->data;
  292. pkt->size = frag->data_size;
  293. return 0;
  294. }
  295. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  296. const char *name)
  297. {
  298. if (!ctx->trace_enable)
  299. return;
  300. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  301. }
  302. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  303. const char *str, const int *subscripts,
  304. const char *bits, int64_t value)
  305. {
  306. char name[256];
  307. size_t name_len, bits_len;
  308. int pad, subs, i, j, k, n;
  309. if (!ctx->trace_enable)
  310. return;
  311. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  312. subs = subscripts ? subscripts[0] : 0;
  313. n = 0;
  314. for (i = j = 0; str[i];) {
  315. if (str[i] == '[') {
  316. if (n < subs) {
  317. ++n;
  318. k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
  319. av_assert0(k > 0 && j + k < sizeof(name));
  320. j += k;
  321. for (++i; str[i] && str[i] != ']'; i++);
  322. av_assert0(str[i] == ']');
  323. } else {
  324. while (str[i] && str[i] != ']')
  325. name[j++] = str[i++];
  326. av_assert0(str[i] == ']');
  327. }
  328. } else {
  329. av_assert0(j + 1 < sizeof(name));
  330. name[j++] = str[i++];
  331. }
  332. }
  333. av_assert0(j + 1 < sizeof(name));
  334. name[j] = 0;
  335. av_assert0(n == subs);
  336. name_len = strlen(name);
  337. bits_len = strlen(bits);
  338. if (name_len + bits_len > 60)
  339. pad = bits_len + 2;
  340. else
  341. pad = 61 - name_len;
  342. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  343. position, name, pad, bits, value);
  344. }
  345. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  346. int width, const char *name,
  347. const int *subscripts, uint32_t *write_to,
  348. uint32_t range_min, uint32_t range_max)
  349. {
  350. uint32_t value;
  351. int position;
  352. av_assert0(width > 0 && width <= 32);
  353. if (get_bits_left(gbc) < width) {
  354. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  355. "%s: bitstream ended.\n", name);
  356. return AVERROR_INVALIDDATA;
  357. }
  358. if (ctx->trace_enable)
  359. position = get_bits_count(gbc);
  360. value = get_bits_long(gbc, width);
  361. if (ctx->trace_enable) {
  362. char bits[33];
  363. int i;
  364. for (i = 0; i < width; i++)
  365. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  366. bits[i] = 0;
  367. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  368. bits, value);
  369. }
  370. if (value < range_min || value > range_max) {
  371. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  372. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  373. name, value, range_min, range_max);
  374. return AVERROR_INVALIDDATA;
  375. }
  376. *write_to = value;
  377. return 0;
  378. }
  379. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  380. int width, const char *name,
  381. const int *subscripts, uint32_t value,
  382. uint32_t range_min, uint32_t range_max)
  383. {
  384. av_assert0(width > 0 && width <= 32);
  385. if (value < range_min || value > range_max) {
  386. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  387. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  388. name, value, range_min, range_max);
  389. return AVERROR_INVALIDDATA;
  390. }
  391. if (put_bits_left(pbc) < width)
  392. return AVERROR(ENOSPC);
  393. if (ctx->trace_enable) {
  394. char bits[33];
  395. int i;
  396. for (i = 0; i < width; i++)
  397. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  398. bits[i] = 0;
  399. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  400. name, subscripts, bits, value);
  401. }
  402. if (width < 32)
  403. put_bits(pbc, width, value);
  404. else
  405. put_bits32(pbc, value);
  406. return 0;
  407. }
  408. int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
  409. int width, const char *name,
  410. const int *subscripts, int32_t *write_to,
  411. int32_t range_min, int32_t range_max)
  412. {
  413. int32_t value;
  414. int position;
  415. av_assert0(width > 0 && width <= 32);
  416. if (get_bits_left(gbc) < width) {
  417. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  418. "%s: bitstream ended.\n", name);
  419. return AVERROR_INVALIDDATA;
  420. }
  421. if (ctx->trace_enable)
  422. position = get_bits_count(gbc);
  423. value = get_sbits_long(gbc, width);
  424. if (ctx->trace_enable) {
  425. char bits[33];
  426. int i;
  427. for (i = 0; i < width; i++)
  428. bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
  429. bits[i] = 0;
  430. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  431. bits, value);
  432. }
  433. if (value < range_min || value > range_max) {
  434. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  435. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  436. name, value, range_min, range_max);
  437. return AVERROR_INVALIDDATA;
  438. }
  439. *write_to = value;
  440. return 0;
  441. }
  442. int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
  443. int width, const char *name,
  444. const int *subscripts, int32_t value,
  445. int32_t range_min, int32_t range_max)
  446. {
  447. av_assert0(width > 0 && width <= 32);
  448. if (value < range_min || value > range_max) {
  449. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  450. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  451. name, value, range_min, range_max);
  452. return AVERROR_INVALIDDATA;
  453. }
  454. if (put_bits_left(pbc) < width)
  455. return AVERROR(ENOSPC);
  456. if (ctx->trace_enable) {
  457. char bits[33];
  458. int i;
  459. for (i = 0; i < width; i++)
  460. bits[i] = value & (1U << (width - i - 1)) ? '1' : '0';
  461. bits[i] = 0;
  462. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  463. name, subscripts, bits, value);
  464. }
  465. if (width < 32)
  466. put_sbits(pbc, width, value);
  467. else
  468. put_bits32(pbc, value);
  469. return 0;
  470. }
  471. int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
  472. CodedBitstreamUnit *unit,
  473. size_t size,
  474. void (*free)(void *opaque, uint8_t *data))
  475. {
  476. av_assert0(!unit->content && !unit->content_ref);
  477. unit->content = av_mallocz(size);
  478. if (!unit->content)
  479. return AVERROR(ENOMEM);
  480. unit->content_ref = av_buffer_create(unit->content, size,
  481. free, ctx, 0);
  482. if (!unit->content_ref) {
  483. av_freep(&unit->content);
  484. return AVERROR(ENOMEM);
  485. }
  486. return 0;
  487. }
  488. int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
  489. CodedBitstreamUnit *unit,
  490. size_t size)
  491. {
  492. av_assert0(!unit->data && !unit->data_ref);
  493. unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  494. if (!unit->data_ref)
  495. return AVERROR(ENOMEM);
  496. unit->data = unit->data_ref->data;
  497. unit->data_size = size;
  498. memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  499. return 0;
  500. }
  501. static int cbs_insert_unit(CodedBitstreamContext *ctx,
  502. CodedBitstreamFragment *frag,
  503. int position)
  504. {
  505. CodedBitstreamUnit *units;
  506. if (frag->nb_units < frag->nb_units_allocated) {
  507. units = frag->units;
  508. if (position < frag->nb_units)
  509. memmove(units + position + 1, units + position,
  510. (frag->nb_units - position) * sizeof(*units));
  511. } else {
  512. units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
  513. if (!units)
  514. return AVERROR(ENOMEM);
  515. ++frag->nb_units_allocated;
  516. if (position > 0)
  517. memcpy(units, frag->units, position * sizeof(*units));
  518. if (position < frag->nb_units)
  519. memcpy(units + position + 1, frag->units + position,
  520. (frag->nb_units - position) * sizeof(*units));
  521. }
  522. memset(units + position, 0, sizeof(*units));
  523. if (units != frag->units) {
  524. av_free(frag->units);
  525. frag->units = units;
  526. }
  527. ++frag->nb_units;
  528. return 0;
  529. }
  530. int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
  531. CodedBitstreamFragment *frag,
  532. int position,
  533. CodedBitstreamUnitType type,
  534. void *content,
  535. AVBufferRef *content_buf)
  536. {
  537. CodedBitstreamUnit *unit;
  538. AVBufferRef *content_ref;
  539. int err;
  540. if (position == -1)
  541. position = frag->nb_units;
  542. av_assert0(position >= 0 && position <= frag->nb_units);
  543. if (content_buf) {
  544. content_ref = av_buffer_ref(content_buf);
  545. if (!content_ref)
  546. return AVERROR(ENOMEM);
  547. } else {
  548. content_ref = NULL;
  549. }
  550. err = cbs_insert_unit(ctx, frag, position);
  551. if (err < 0) {
  552. av_buffer_unref(&content_ref);
  553. return err;
  554. }
  555. unit = &frag->units[position];
  556. unit->type = type;
  557. unit->content = content;
  558. unit->content_ref = content_ref;
  559. return 0;
  560. }
  561. int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
  562. CodedBitstreamFragment *frag,
  563. int position,
  564. CodedBitstreamUnitType type,
  565. uint8_t *data, size_t data_size,
  566. AVBufferRef *data_buf)
  567. {
  568. CodedBitstreamUnit *unit;
  569. AVBufferRef *data_ref;
  570. int err;
  571. if (position == -1)
  572. position = frag->nb_units;
  573. av_assert0(position >= 0 && position <= frag->nb_units);
  574. if (data_buf)
  575. data_ref = av_buffer_ref(data_buf);
  576. else
  577. data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
  578. if (!data_ref)
  579. return AVERROR(ENOMEM);
  580. err = cbs_insert_unit(ctx, frag, position);
  581. if (err < 0) {
  582. av_buffer_unref(&data_ref);
  583. return err;
  584. }
  585. unit = &frag->units[position];
  586. unit->type = type;
  587. unit->data = data;
  588. unit->data_size = data_size;
  589. unit->data_ref = data_ref;
  590. return 0;
  591. }
  592. int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
  593. CodedBitstreamFragment *frag,
  594. int position)
  595. {
  596. if (position < 0 || position >= frag->nb_units)
  597. return AVERROR(EINVAL);
  598. cbs_unit_uninit(ctx, &frag->units[position]);
  599. --frag->nb_units;
  600. if (frag->nb_units > 0)
  601. memmove(frag->units + position,
  602. frag->units + position + 1,
  603. (frag->nb_units - position) * sizeof(*frag->units));
  604. return 0;
  605. }