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.

476 lines
12KB

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