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.

616 lines
16KB

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