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.

599 lines
15KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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. int err;
  245. err = ff_cbs_write_fragment_data(ctx, frag);
  246. if (err < 0)
  247. return err;
  248. err = av_new_packet(pkt, frag->data_size);
  249. if (err < 0)
  250. return err;
  251. memcpy(pkt->data, frag->data, frag->data_size);
  252. pkt->size = frag->data_size;
  253. return 0;
  254. }
  255. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  256. const char *name)
  257. {
  258. if (!ctx->trace_enable)
  259. return;
  260. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  261. }
  262. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  263. const char *name, const char *bits,
  264. int64_t value)
  265. {
  266. size_t name_len, bits_len;
  267. int pad;
  268. if (!ctx->trace_enable)
  269. return;
  270. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  271. name_len = strlen(name);
  272. bits_len = strlen(bits);
  273. if (name_len + bits_len > 60)
  274. pad = bits_len + 2;
  275. else
  276. pad = 61 - name_len;
  277. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  278. position, name, pad, bits, value);
  279. }
  280. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, BitstreamContext *bc,
  281. int width, const char *name, uint32_t *write_to,
  282. uint32_t range_min, uint32_t range_max)
  283. {
  284. uint32_t value;
  285. int position;
  286. av_assert0(width <= 32);
  287. if (bitstream_bits_left(bc) < width) {
  288. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  289. "%s: bitstream ended.\n", name);
  290. return AVERROR_INVALIDDATA;
  291. }
  292. if (ctx->trace_enable)
  293. position = bitstream_tell(bc);
  294. value = bitstream_read(bc, width);
  295. if (ctx->trace_enable) {
  296. char bits[33];
  297. int i;
  298. for (i = 0; i < width; i++)
  299. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  300. bits[i] = 0;
  301. ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
  302. }
  303. if (value < range_min || value > range_max) {
  304. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  305. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  306. name, value, range_min, range_max);
  307. return AVERROR_INVALIDDATA;
  308. }
  309. *write_to = value;
  310. return 0;
  311. }
  312. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  313. int width, const char *name, uint32_t value,
  314. uint32_t range_min, uint32_t range_max)
  315. {
  316. av_assert0(width <= 32);
  317. if (value < range_min || value > range_max) {
  318. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  319. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  320. name, value, range_min, range_max);
  321. return AVERROR_INVALIDDATA;
  322. }
  323. if (put_bits_left(pbc) < width)
  324. return AVERROR(ENOSPC);
  325. if (ctx->trace_enable) {
  326. char bits[33];
  327. int i;
  328. for (i = 0; i < width; i++)
  329. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  330. bits[i] = 0;
  331. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
  332. }
  333. if (width < 32)
  334. put_bits(pbc, width, value);
  335. else
  336. put_bits32(pbc, value);
  337. return 0;
  338. }
  339. int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
  340. CodedBitstreamUnit *unit,
  341. size_t size,
  342. void (*free)(void *opaque, uint8_t *data))
  343. {
  344. av_assert0(!unit->content && !unit->content_ref);
  345. unit->content = av_mallocz(size);
  346. if (!unit->content)
  347. return AVERROR(ENOMEM);
  348. unit->content_ref = av_buffer_create(unit->content, size,
  349. free, ctx, 0);
  350. if (!unit->content_ref) {
  351. av_freep(&unit->content);
  352. return AVERROR(ENOMEM);
  353. }
  354. return 0;
  355. }
  356. int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
  357. CodedBitstreamUnit *unit,
  358. size_t size)
  359. {
  360. av_assert0(!unit->data && !unit->data_ref);
  361. unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  362. if (!unit->data_ref)
  363. return AVERROR(ENOMEM);
  364. unit->data = unit->data_ref->data;
  365. unit->data_size = size;
  366. memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  367. return 0;
  368. }
  369. static int cbs_insert_unit(CodedBitstreamContext *ctx,
  370. CodedBitstreamFragment *frag,
  371. int position)
  372. {
  373. CodedBitstreamUnit *units;
  374. units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
  375. if (!units)
  376. return AVERROR(ENOMEM);
  377. if (position > 0)
  378. memcpy(units, frag->units, position * sizeof(*units));
  379. if (position < frag->nb_units)
  380. memcpy(units + position + 1, frag->units + position,
  381. (frag->nb_units - position) * sizeof(*units));
  382. memset(units + position, 0, sizeof(*units));
  383. av_freep(&frag->units);
  384. frag->units = units;
  385. ++frag->nb_units;
  386. return 0;
  387. }
  388. int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
  389. CodedBitstreamFragment *frag,
  390. int position,
  391. CodedBitstreamUnitType type,
  392. void *content,
  393. AVBufferRef *content_buf)
  394. {
  395. CodedBitstreamUnit *unit;
  396. AVBufferRef *content_ref;
  397. int err;
  398. if (position == -1)
  399. position = frag->nb_units;
  400. av_assert0(position >= 0 && position <= frag->nb_units);
  401. if (content_buf) {
  402. content_ref = av_buffer_ref(content_buf);
  403. if (!content_ref)
  404. return AVERROR(ENOMEM);
  405. } else {
  406. content_ref = NULL;
  407. }
  408. err = cbs_insert_unit(ctx, frag, position);
  409. if (err < 0) {
  410. av_buffer_unref(&content_ref);
  411. return err;
  412. }
  413. unit = &frag->units[position];
  414. unit->type = type;
  415. unit->content = content;
  416. unit->content_ref = content_ref;
  417. return 0;
  418. }
  419. int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
  420. CodedBitstreamFragment *frag,
  421. int position,
  422. CodedBitstreamUnitType type,
  423. uint8_t *data, size_t data_size,
  424. AVBufferRef *data_buf)
  425. {
  426. CodedBitstreamUnit *unit;
  427. AVBufferRef *data_ref;
  428. int err;
  429. if (position == -1)
  430. position = frag->nb_units;
  431. av_assert0(position >= 0 && position <= frag->nb_units);
  432. if (data_buf)
  433. data_ref = av_buffer_ref(data_buf);
  434. else
  435. data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
  436. if (!data_ref)
  437. return AVERROR(ENOMEM);
  438. err = cbs_insert_unit(ctx, frag, position);
  439. if (err < 0) {
  440. av_buffer_unref(&data_ref);
  441. return err;
  442. }
  443. unit = &frag->units[position];
  444. unit->type = type;
  445. unit->data = data;
  446. unit->data_size = data_size;
  447. unit->data_ref = data_ref;
  448. return 0;
  449. }
  450. int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
  451. CodedBitstreamFragment *frag,
  452. int position)
  453. {
  454. if (position < 0 || position >= frag->nb_units)
  455. return AVERROR(EINVAL);
  456. cbs_unit_uninit(ctx, &frag->units[position]);
  457. --frag->nb_units;
  458. if (frag->nb_units == 0) {
  459. av_freep(&frag->units);
  460. } else {
  461. memmove(frag->units + position,
  462. frag->units + position + 1,
  463. (frag->nb_units - position) * sizeof(*frag->units));
  464. // Don't bother reallocating the unit array.
  465. }
  466. return 0;
  467. }