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.

492 lines
13KB

  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/common.h"
  22. #include "cbs.h"
  23. #include "cbs_internal.h"
  24. static const CodedBitstreamType *cbs_type_table[] = {
  25. #if CONFIG_CBS_H264
  26. &ff_cbs_type_h264,
  27. #endif
  28. #if CONFIG_CBS_H265
  29. &ff_cbs_type_h265,
  30. #endif
  31. #if CONFIG_CBS_MPEG2
  32. &ff_cbs_type_mpeg2,
  33. #endif
  34. };
  35. int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
  36. enum AVCodecID codec_id, void *log_ctx)
  37. {
  38. CodedBitstreamContext *ctx;
  39. const CodedBitstreamType *type;
  40. int i;
  41. type = NULL;
  42. for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
  43. if (cbs_type_table[i]->codec_id == codec_id) {
  44. type = cbs_type_table[i];
  45. break;
  46. }
  47. }
  48. if (!type)
  49. return AVERROR(EINVAL);
  50. ctx = av_mallocz(sizeof(*ctx));
  51. if (!ctx)
  52. return AVERROR(ENOMEM);
  53. ctx->log_ctx = log_ctx;
  54. ctx->codec = type;
  55. ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
  56. if (!ctx->priv_data) {
  57. av_freep(&ctx);
  58. return AVERROR(ENOMEM);
  59. }
  60. ctx->decompose_unit_types = NULL;
  61. ctx->trace_enable = 0;
  62. ctx->trace_level = AV_LOG_TRACE;
  63. *ctx_ptr = ctx;
  64. return 0;
  65. }
  66. void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
  67. {
  68. CodedBitstreamContext *ctx = *ctx_ptr;
  69. if (!ctx)
  70. return;
  71. if (ctx->codec && ctx->codec->close)
  72. ctx->codec->close(ctx);
  73. av_freep(&ctx->priv_data);
  74. av_freep(ctx_ptr);
  75. }
  76. static void cbs_unit_uninit(CodedBitstreamContext *ctx,
  77. CodedBitstreamUnit *unit)
  78. {
  79. if (ctx->codec->free_unit && unit->content && !unit->content_external)
  80. ctx->codec->free_unit(unit);
  81. av_freep(&unit->data);
  82. unit->data_size = 0;
  83. unit->data_bit_padding = 0;
  84. }
  85. void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
  86. CodedBitstreamFragment *frag)
  87. {
  88. int i;
  89. for (i = 0; i < frag->nb_units; i++)
  90. cbs_unit_uninit(ctx, &frag->units[i]);
  91. av_freep(&frag->units);
  92. frag->nb_units = 0;
  93. av_freep(&frag->data);
  94. frag->data_size = 0;
  95. frag->data_bit_padding = 0;
  96. }
  97. static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
  98. CodedBitstreamFragment *frag)
  99. {
  100. int err, i, j;
  101. for (i = 0; i < frag->nb_units; i++) {
  102. if (ctx->decompose_unit_types) {
  103. for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
  104. if (ctx->decompose_unit_types[j] == frag->units[i].type)
  105. break;
  106. }
  107. if (j >= ctx->nb_decompose_unit_types)
  108. continue;
  109. }
  110. err = ctx->codec->read_unit(ctx, &frag->units[i]);
  111. if (err == AVERROR(ENOSYS)) {
  112. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  113. "Decomposition unimplemented for unit %d "
  114. "(type %"PRIu32").\n", i, frag->units[i].type);
  115. } else if (err < 0) {
  116. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
  117. "(type %"PRIu32").\n", i, frag->units[i].type);
  118. return err;
  119. }
  120. }
  121. return 0;
  122. }
  123. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  124. CodedBitstreamFragment *frag,
  125. const AVCodecParameters *par)
  126. {
  127. int err;
  128. memset(frag, 0, sizeof(*frag));
  129. frag->data = par->extradata;
  130. frag->data_size = par->extradata_size;
  131. err = ctx->codec->split_fragment(ctx, frag, 1);
  132. if (err < 0)
  133. return err;
  134. frag->data = NULL;
  135. frag->data_size = 0;
  136. return cbs_read_fragment_content(ctx, frag);
  137. }
  138. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  139. CodedBitstreamFragment *frag,
  140. const AVPacket *pkt)
  141. {
  142. int err;
  143. memset(frag, 0, sizeof(*frag));
  144. frag->data = pkt->data;
  145. frag->data_size = pkt->size;
  146. err = ctx->codec->split_fragment(ctx, frag, 0);
  147. if (err < 0)
  148. return err;
  149. frag->data = NULL;
  150. frag->data_size = 0;
  151. return cbs_read_fragment_content(ctx, frag);
  152. }
  153. int ff_cbs_read(CodedBitstreamContext *ctx,
  154. CodedBitstreamFragment *frag,
  155. const uint8_t *data, size_t size)
  156. {
  157. int err;
  158. memset(frag, 0, sizeof(*frag));
  159. // (We won't write to this during split.)
  160. frag->data = (uint8_t*)data;
  161. frag->data_size = size;
  162. err = ctx->codec->split_fragment(ctx, frag, 0);
  163. if (err < 0)
  164. return err;
  165. frag->data = NULL;
  166. frag->data_size = 0;
  167. return cbs_read_fragment_content(ctx, frag);
  168. }
  169. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  170. CodedBitstreamFragment *frag)
  171. {
  172. int err, i;
  173. for (i = 0; i < frag->nb_units; i++) {
  174. if (!frag->units[i].content)
  175. continue;
  176. err = ctx->codec->write_unit(ctx, &frag->units[i]);
  177. if (err < 0) {
  178. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
  179. "(type %"PRIu32").\n", i, frag->units[i].type);
  180. return err;
  181. }
  182. }
  183. err = ctx->codec->assemble_fragment(ctx, frag);
  184. if (err < 0) {
  185. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
  186. return err;
  187. }
  188. return 0;
  189. }
  190. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  191. AVCodecParameters *par,
  192. CodedBitstreamFragment *frag)
  193. {
  194. int err;
  195. err = ff_cbs_write_fragment_data(ctx, frag);
  196. if (err < 0)
  197. return err;
  198. av_freep(&par->extradata);
  199. par->extradata = av_malloc(frag->data_size +
  200. AV_INPUT_BUFFER_PADDING_SIZE);
  201. if (!par->extradata)
  202. return AVERROR(ENOMEM);
  203. memcpy(par->extradata, frag->data, frag->data_size);
  204. memset(par->extradata + frag->data_size, 0,
  205. AV_INPUT_BUFFER_PADDING_SIZE);
  206. par->extradata_size = frag->data_size;
  207. return 0;
  208. }
  209. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  210. AVPacket *pkt,
  211. CodedBitstreamFragment *frag)
  212. {
  213. int err;
  214. err = ff_cbs_write_fragment_data(ctx, frag);
  215. if (err < 0)
  216. return err;
  217. err = av_new_packet(pkt, frag->data_size);
  218. if (err < 0)
  219. return err;
  220. memcpy(pkt->data, frag->data, frag->data_size);
  221. pkt->size = frag->data_size;
  222. return 0;
  223. }
  224. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  225. const char *name)
  226. {
  227. if (!ctx->trace_enable)
  228. return;
  229. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  230. }
  231. void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
  232. const char *name, const char *bits,
  233. int64_t value)
  234. {
  235. size_t name_len, bits_len;
  236. int pad;
  237. if (!ctx->trace_enable)
  238. return;
  239. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  240. name_len = strlen(name);
  241. bits_len = strlen(bits);
  242. if (name_len + bits_len > 60)
  243. pad = bits_len + 2;
  244. else
  245. pad = 61 - name_len;
  246. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  247. position, name, pad, bits, value);
  248. }
  249. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  250. int width, const char *name, uint32_t *write_to,
  251. uint32_t range_min, uint32_t range_max)
  252. {
  253. uint32_t value;
  254. int position;
  255. av_assert0(width > 0 && width <= 32);
  256. if (get_bits_left(gbc) < width) {
  257. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  258. "%s: bitstream ended.\n", name);
  259. return AVERROR_INVALIDDATA;
  260. }
  261. if (ctx->trace_enable)
  262. position = get_bits_count(gbc);
  263. value = get_bits_long(gbc, width);
  264. if (ctx->trace_enable) {
  265. char bits[33];
  266. int i;
  267. for (i = 0; i < width; i++)
  268. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  269. bits[i] = 0;
  270. ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
  271. }
  272. if (value < range_min || value > range_max) {
  273. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  274. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  275. name, value, range_min, range_max);
  276. return AVERROR_INVALIDDATA;
  277. }
  278. *write_to = value;
  279. return 0;
  280. }
  281. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  282. int width, const char *name, uint32_t value,
  283. uint32_t range_min, uint32_t range_max)
  284. {
  285. av_assert0(width > 0 && width <= 32);
  286. if (value < range_min || value > range_max) {
  287. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  288. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  289. name, value, range_min, range_max);
  290. return AVERROR_INVALIDDATA;
  291. }
  292. if (put_bits_left(pbc) < width)
  293. return AVERROR(ENOSPC);
  294. if (ctx->trace_enable) {
  295. char bits[33];
  296. int i;
  297. for (i = 0; i < width; i++)
  298. bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
  299. bits[i] = 0;
  300. ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
  301. }
  302. if (width < 32)
  303. put_bits(pbc, width, value);
  304. else
  305. put_bits32(pbc, value);
  306. return 0;
  307. }
  308. static int cbs_insert_unit(CodedBitstreamContext *ctx,
  309. CodedBitstreamFragment *frag,
  310. int position)
  311. {
  312. CodedBitstreamUnit *units;
  313. units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
  314. if (!units)
  315. return AVERROR(ENOMEM);
  316. if (position > 0)
  317. memcpy(units, frag->units, position * sizeof(*units));
  318. if (position < frag->nb_units)
  319. memcpy(units + position + 1, frag->units + position,
  320. (frag->nb_units - position) * sizeof(*units));
  321. memset(units + position, 0, sizeof(*units));
  322. av_freep(&frag->units);
  323. frag->units = units;
  324. ++frag->nb_units;
  325. return 0;
  326. }
  327. int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
  328. CodedBitstreamFragment *frag,
  329. int position,
  330. CodedBitstreamUnitType type,
  331. void *content)
  332. {
  333. int err;
  334. if (position == -1)
  335. position = frag->nb_units;
  336. av_assert0(position >= 0 && position <= frag->nb_units);
  337. err = cbs_insert_unit(ctx, frag, position);
  338. if (err < 0)
  339. return err;
  340. frag->units[position].type = type;
  341. frag->units[position].content = content;
  342. frag->units[position].content_external = 1;
  343. return 0;
  344. }
  345. int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
  346. CodedBitstreamFragment *frag,
  347. int position,
  348. CodedBitstreamUnitType type,
  349. uint8_t *data, size_t data_size)
  350. {
  351. int err;
  352. if (position == -1)
  353. position = frag->nb_units;
  354. av_assert0(position >= 0 && position <= frag->nb_units);
  355. err = cbs_insert_unit(ctx, frag, position);
  356. if (err < 0)
  357. return err;
  358. frag->units[position].type = type;
  359. frag->units[position].data = data;
  360. frag->units[position].data_size = data_size;
  361. return 0;
  362. }
  363. int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
  364. CodedBitstreamFragment *frag,
  365. int position)
  366. {
  367. if (position < 0 || position >= frag->nb_units)
  368. return AVERROR(EINVAL);
  369. cbs_unit_uninit(ctx, &frag->units[position]);
  370. --frag->nb_units;
  371. if (frag->nb_units == 0) {
  372. av_freep(&frag->units);
  373. } else {
  374. memmove(frag->units + position,
  375. frag->units + position + 1,
  376. (frag->nb_units - position) * sizeof(*frag->units));
  377. // Don't bother reallocating the unit array.
  378. }
  379. return 0;
  380. }