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.

603 lines
15KB

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