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.

668 lines
17KB

  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_uninit(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. av_freep(&frag->units);
  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. static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
  131. CodedBitstreamFragment *frag)
  132. {
  133. int err, i, j;
  134. for (i = 0; i < frag->nb_units; i++) {
  135. CodedBitstreamUnit *unit = &frag->units[i];
  136. if (ctx->decompose_unit_types) {
  137. for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
  138. if (ctx->decompose_unit_types[j] == unit->type)
  139. break;
  140. }
  141. if (j >= ctx->nb_decompose_unit_types)
  142. continue;
  143. }
  144. av_buffer_unref(&unit->content_ref);
  145. unit->content = NULL;
  146. av_assert0(unit->data && unit->data_ref);
  147. err = ctx->codec->read_unit(ctx, unit);
  148. if (err == AVERROR(ENOSYS)) {
  149. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  150. "Decomposition unimplemented for unit %d "
  151. "(type %"PRIu32").\n", i, unit->type);
  152. } else if (err < 0) {
  153. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
  154. "(type %"PRIu32").\n", i, unit->type);
  155. return err;
  156. }
  157. }
  158. return 0;
  159. }
  160. static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
  161. CodedBitstreamFragment *frag,
  162. const uint8_t *data, size_t size)
  163. {
  164. av_assert0(!frag->data && !frag->data_ref);
  165. frag->data_ref =
  166. av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  167. if (!frag->data_ref)
  168. return AVERROR(ENOMEM);
  169. frag->data = frag->data_ref->data;
  170. frag->data_size = size;
  171. memcpy(frag->data, data, size);
  172. memset(frag->data + size, 0,
  173. AV_INPUT_BUFFER_PADDING_SIZE);
  174. return 0;
  175. }
  176. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  177. CodedBitstreamFragment *frag,
  178. const AVCodecParameters *par)
  179. {
  180. int err;
  181. memset(frag, 0, sizeof(*frag));
  182. err = cbs_fill_fragment_data(ctx, frag, par->extradata,
  183. par->extradata_size);
  184. if (err < 0)
  185. return err;
  186. err = ctx->codec->split_fragment(ctx, frag, 1);
  187. if (err < 0)
  188. return err;
  189. return cbs_read_fragment_content(ctx, frag);
  190. }
  191. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  192. CodedBitstreamFragment *frag,
  193. const AVPacket *pkt)
  194. {
  195. int err;
  196. memset(frag, 0, sizeof(*frag));
  197. if (pkt->buf) {
  198. frag->data_ref = av_buffer_ref(pkt->buf);
  199. if (!frag->data_ref)
  200. return AVERROR(ENOMEM);
  201. frag->data = pkt->data;
  202. frag->data_size = pkt->size;
  203. } else {
  204. err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
  205. if (err < 0)
  206. return err;
  207. }
  208. err = ctx->codec->split_fragment(ctx, frag, 0);
  209. if (err < 0)
  210. return err;
  211. return cbs_read_fragment_content(ctx, frag);
  212. }
  213. int ff_cbs_read(CodedBitstreamContext *ctx,
  214. CodedBitstreamFragment *frag,
  215. const uint8_t *data, size_t size)
  216. {
  217. int err;
  218. memset(frag, 0, sizeof(*frag));
  219. err = cbs_fill_fragment_data(ctx, frag, data, size);
  220. if (err < 0)
  221. return err;
  222. err = ctx->codec->split_fragment(ctx, frag, 0);
  223. if (err < 0)
  224. return err;
  225. return cbs_read_fragment_content(ctx, frag);
  226. }
  227. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  228. CodedBitstreamFragment *frag)
  229. {
  230. int err, i;
  231. for (i = 0; i < frag->nb_units; i++) {
  232. CodedBitstreamUnit *unit = &frag->units[i];
  233. if (!unit->content)
  234. continue;
  235. av_buffer_unref(&unit->data_ref);
  236. unit->data = NULL;
  237. err = ctx->codec->write_unit(ctx, unit);
  238. if (err < 0) {
  239. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
  240. "(type %"PRIu32").\n", i, unit->type);
  241. return err;
  242. }
  243. av_assert0(unit->data && unit->data_ref);
  244. }
  245. av_buffer_unref(&frag->data_ref);
  246. frag->data = NULL;
  247. err = ctx->codec->assemble_fragment(ctx, frag);
  248. if (err < 0) {
  249. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
  250. return err;
  251. }
  252. av_assert0(frag->data && frag->data_ref);
  253. return 0;
  254. }
  255. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  256. AVCodecParameters *par,
  257. CodedBitstreamFragment *frag)
  258. {
  259. int err;
  260. err = ff_cbs_write_fragment_data(ctx, frag);
  261. if (err < 0)
  262. return err;
  263. av_freep(&par->extradata);
  264. par->extradata = av_malloc(frag->data_size +
  265. AV_INPUT_BUFFER_PADDING_SIZE);
  266. if (!par->extradata)
  267. return AVERROR(ENOMEM);
  268. memcpy(par->extradata, frag->data, frag->data_size);
  269. memset(par->extradata + frag->data_size, 0,
  270. AV_INPUT_BUFFER_PADDING_SIZE);
  271. par->extradata_size = frag->data_size;
  272. return 0;
  273. }
  274. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  275. AVPacket *pkt,
  276. CodedBitstreamFragment *frag)
  277. {
  278. AVBufferRef *buf;
  279. int err;
  280. err = ff_cbs_write_fragment_data(ctx, frag);
  281. if (err < 0)
  282. return err;
  283. buf = av_buffer_ref(frag->data_ref);
  284. if (!buf)
  285. return AVERROR(ENOMEM);
  286. av_init_packet(pkt);
  287. pkt->buf = buf;
  288. pkt->data = frag->data;
  289. pkt->size = frag->data_size;
  290. return 0;
  291. }
  292. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  293. const char *name)
  294. {
  295. if (!ctx->trace_enable)
  296. return;
  297. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  298. }
  299. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  300. const char *str, const int *subscripts,
  301. const char *bits, int64_t value)
  302. {
  303. char name[256];
  304. size_t name_len, bits_len;
  305. int pad, subs, i, j, k, n;
  306. if (!ctx->trace_enable)
  307. return;
  308. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  309. subs = subscripts ? subscripts[0] : 0;
  310. n = 0;
  311. for (i = j = 0; str[i];) {
  312. if (str[i] == '[') {
  313. if (n < subs) {
  314. ++n;
  315. k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
  316. av_assert0(k > 0 && j + k < sizeof(name));
  317. j += k;
  318. for (++i; str[i] && str[i] != ']'; i++);
  319. av_assert0(str[i] == ']');
  320. } else {
  321. while (str[i] && str[i] != ']')
  322. name[j++] = str[i++];
  323. av_assert0(str[i] == ']');
  324. }
  325. } else {
  326. av_assert0(j + 1 < sizeof(name));
  327. name[j++] = str[i++];
  328. }
  329. }
  330. av_assert0(j + 1 < sizeof(name));
  331. name[j] = 0;
  332. av_assert0(n == subs);
  333. name_len = strlen(name);
  334. bits_len = strlen(bits);
  335. if (name_len + bits_len > 60)
  336. pad = bits_len + 2;
  337. else
  338. pad = 61 - name_len;
  339. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  340. position, name, pad, bits, value);
  341. }
  342. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  343. int width, const char *name,
  344. const int *subscripts, uint32_t *write_to,
  345. uint32_t range_min, uint32_t range_max)
  346. {
  347. uint32_t value;
  348. int position;
  349. av_assert0(width > 0 && width <= 32);
  350. if (get_bits_left(gbc) < width) {
  351. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  352. "%s: bitstream ended.\n", name);
  353. return AVERROR_INVALIDDATA;
  354. }
  355. if (ctx->trace_enable)
  356. position = get_bits_count(gbc);
  357. value = get_bits_long(gbc, width);
  358. if (ctx->trace_enable) {
  359. char bits[33];
  360. int i;
  361. for (i = 0; i < width; i++)
  362. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  363. bits[i] = 0;
  364. ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
  365. bits, value);
  366. }
  367. if (value < range_min || value > range_max) {
  368. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  369. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  370. name, value, range_min, range_max);
  371. return AVERROR_INVALIDDATA;
  372. }
  373. *write_to = value;
  374. return 0;
  375. }
  376. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  377. int width, const char *name,
  378. const int *subscripts, uint32_t value,
  379. uint32_t range_min, uint32_t range_max)
  380. {
  381. av_assert0(width > 0 && width <= 32);
  382. if (value < range_min || value > range_max) {
  383. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  384. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  385. name, value, range_min, range_max);
  386. return AVERROR_INVALIDDATA;
  387. }
  388. if (put_bits_left(pbc) < width)
  389. return AVERROR(ENOSPC);
  390. if (ctx->trace_enable) {
  391. char bits[33];
  392. int i;
  393. for (i = 0; i < width; i++)
  394. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  395. bits[i] = 0;
  396. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
  397. name, subscripts, bits, value);
  398. }
  399. if (width < 32)
  400. put_bits(pbc, width, value);
  401. else
  402. put_bits32(pbc, value);
  403. return 0;
  404. }
  405. int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
  406. CodedBitstreamUnit *unit,
  407. size_t size,
  408. void (*free)(void *opaque, uint8_t *data))
  409. {
  410. av_assert0(!unit->content && !unit->content_ref);
  411. unit->content = av_mallocz(size);
  412. if (!unit->content)
  413. return AVERROR(ENOMEM);
  414. unit->content_ref = av_buffer_create(unit->content, size,
  415. free, ctx, 0);
  416. if (!unit->content_ref) {
  417. av_freep(&unit->content);
  418. return AVERROR(ENOMEM);
  419. }
  420. return 0;
  421. }
  422. int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
  423. CodedBitstreamUnit *unit,
  424. size_t size)
  425. {
  426. av_assert0(!unit->data && !unit->data_ref);
  427. unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  428. if (!unit->data_ref)
  429. return AVERROR(ENOMEM);
  430. unit->data = unit->data_ref->data;
  431. unit->data_size = size;
  432. memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  433. return 0;
  434. }
  435. static int cbs_insert_unit(CodedBitstreamContext *ctx,
  436. CodedBitstreamFragment *frag,
  437. int position)
  438. {
  439. CodedBitstreamUnit *units;
  440. units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
  441. if (!units)
  442. return AVERROR(ENOMEM);
  443. if (position > 0)
  444. memcpy(units, frag->units, position * sizeof(*units));
  445. if (position < frag->nb_units)
  446. memcpy(units + position + 1, frag->units + position,
  447. (frag->nb_units - position) * sizeof(*units));
  448. memset(units + position, 0, sizeof(*units));
  449. av_freep(&frag->units);
  450. frag->units = units;
  451. ++frag->nb_units;
  452. return 0;
  453. }
  454. int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
  455. CodedBitstreamFragment *frag,
  456. int position,
  457. CodedBitstreamUnitType type,
  458. void *content,
  459. AVBufferRef *content_buf)
  460. {
  461. CodedBitstreamUnit *unit;
  462. AVBufferRef *content_ref;
  463. int err;
  464. if (position == -1)
  465. position = frag->nb_units;
  466. av_assert0(position >= 0 && position <= frag->nb_units);
  467. if (content_buf) {
  468. content_ref = av_buffer_ref(content_buf);
  469. if (!content_ref)
  470. return AVERROR(ENOMEM);
  471. } else {
  472. content_ref = NULL;
  473. }
  474. err = cbs_insert_unit(ctx, frag, position);
  475. if (err < 0) {
  476. av_buffer_unref(&content_ref);
  477. return err;
  478. }
  479. unit = &frag->units[position];
  480. unit->type = type;
  481. unit->content = content;
  482. unit->content_ref = content_ref;
  483. return 0;
  484. }
  485. int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
  486. CodedBitstreamFragment *frag,
  487. int position,
  488. CodedBitstreamUnitType type,
  489. uint8_t *data, size_t data_size,
  490. AVBufferRef *data_buf)
  491. {
  492. CodedBitstreamUnit *unit;
  493. AVBufferRef *data_ref;
  494. int err;
  495. if (position == -1)
  496. position = frag->nb_units;
  497. av_assert0(position >= 0 && position <= frag->nb_units);
  498. if (data_buf)
  499. data_ref = av_buffer_ref(data_buf);
  500. else
  501. data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
  502. if (!data_ref)
  503. return AVERROR(ENOMEM);
  504. err = cbs_insert_unit(ctx, frag, position);
  505. if (err < 0) {
  506. av_buffer_unref(&data_ref);
  507. return err;
  508. }
  509. unit = &frag->units[position];
  510. unit->type = type;
  511. unit->data = data;
  512. unit->data_size = data_size;
  513. unit->data_ref = data_ref;
  514. return 0;
  515. }
  516. int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
  517. CodedBitstreamFragment *frag,
  518. int position)
  519. {
  520. if (position < 0 || position >= frag->nb_units)
  521. return AVERROR(EINVAL);
  522. cbs_unit_uninit(ctx, &frag->units[position]);
  523. --frag->nb_units;
  524. if (frag->nb_units == 0) {
  525. av_freep(&frag->units);
  526. } else {
  527. memmove(frag->units + position,
  528. frag->units + position + 1,
  529. (frag->nb_units - position) * sizeof(*frag->units));
  530. // Don't bother reallocating the unit array.
  531. }
  532. return 0;
  533. }