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.

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