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.

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