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.

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