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.

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