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.

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