cbs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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/buffer.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "avcodec.h"
  25. #include "cbs.h"
  26. #include "cbs_internal.h"
  27. #include "refstruct.h"
  28. static const CodedBitstreamType *const cbs_type_table[] = {
  29. #if CONFIG_CBS_AV1
  30. &ff_cbs_type_av1,
  31. #endif
  32. #if CONFIG_CBS_H264
  33. &ff_cbs_type_h264,
  34. #endif
  35. #if CONFIG_CBS_H265
  36. &ff_cbs_type_h265,
  37. #endif
  38. #if CONFIG_CBS_H266
  39. &ff_cbs_type_h266,
  40. #endif
  41. #if CONFIG_CBS_JPEG
  42. &ff_cbs_type_jpeg,
  43. #endif
  44. #if CONFIG_CBS_MPEG2
  45. &ff_cbs_type_mpeg2,
  46. #endif
  47. #if CONFIG_CBS_VP9
  48. &ff_cbs_type_vp9,
  49. #endif
  50. };
  51. const enum AVCodecID ff_cbs_all_codec_ids[] = {
  52. #if CONFIG_CBS_AV1
  53. AV_CODEC_ID_AV1,
  54. #endif
  55. #if CONFIG_CBS_H264
  56. AV_CODEC_ID_H264,
  57. #endif
  58. #if CONFIG_CBS_H265
  59. AV_CODEC_ID_H265,
  60. #endif
  61. #if CONFIG_CBS_H266
  62. AV_CODEC_ID_H266,
  63. #endif
  64. #if CONFIG_CBS_JPEG
  65. AV_CODEC_ID_MJPEG,
  66. #endif
  67. #if CONFIG_CBS_MPEG2
  68. AV_CODEC_ID_MPEG2VIDEO,
  69. #endif
  70. #if CONFIG_CBS_VP9
  71. AV_CODEC_ID_VP9,
  72. #endif
  73. AV_CODEC_ID_NONE
  74. };
  75. av_cold int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
  76. enum AVCodecID codec_id, void *log_ctx)
  77. {
  78. CodedBitstreamContext *ctx;
  79. const CodedBitstreamType *type;
  80. int i;
  81. type = NULL;
  82. for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
  83. if (cbs_type_table[i]->codec_id == codec_id) {
  84. type = cbs_type_table[i];
  85. break;
  86. }
  87. }
  88. if (!type)
  89. return AVERROR(EINVAL);
  90. ctx = av_mallocz(sizeof(*ctx));
  91. if (!ctx)
  92. return AVERROR(ENOMEM);
  93. ctx->log_ctx = log_ctx;
  94. ctx->codec = type; /* Must be before any error */
  95. if (type->priv_data_size) {
  96. ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
  97. if (!ctx->priv_data) {
  98. av_freep(&ctx);
  99. return AVERROR(ENOMEM);
  100. }
  101. if (type->priv_class) {
  102. *(const AVClass **)ctx->priv_data = type->priv_class;
  103. av_opt_set_defaults(ctx->priv_data);
  104. }
  105. }
  106. ctx->decompose_unit_types = NULL;
  107. ctx->trace_enable = 0;
  108. ctx->trace_level = AV_LOG_TRACE;
  109. ctx->trace_context = ctx;
  110. *ctx_ptr = ctx;
  111. return 0;
  112. }
  113. av_cold void ff_cbs_flush(CodedBitstreamContext *ctx)
  114. {
  115. if (ctx->codec->flush)
  116. ctx->codec->flush(ctx);
  117. }
  118. av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
  119. {
  120. CodedBitstreamContext *ctx = *ctx_ptr;
  121. if (!ctx)
  122. return;
  123. if (ctx->codec->close)
  124. ctx->codec->close(ctx);
  125. av_freep(&ctx->write_buffer);
  126. if (ctx->codec->priv_class && ctx->priv_data)
  127. av_opt_free(ctx->priv_data);
  128. av_freep(&ctx->priv_data);
  129. av_freep(ctx_ptr);
  130. }
  131. static void cbs_unit_uninit(CodedBitstreamUnit *unit)
  132. {
  133. ff_refstruct_unref(&unit->content_ref);
  134. unit->content = NULL;
  135. av_buffer_unref(&unit->data_ref);
  136. unit->data = NULL;
  137. unit->data_size = 0;
  138. unit->data_bit_padding = 0;
  139. }
  140. void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
  141. {
  142. int i;
  143. for (i = 0; i < frag->nb_units; i++)
  144. cbs_unit_uninit(&frag->units[i]);
  145. frag->nb_units = 0;
  146. av_buffer_unref(&frag->data_ref);
  147. frag->data = NULL;
  148. frag->data_size = 0;
  149. frag->data_bit_padding = 0;
  150. }
  151. av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
  152. {
  153. ff_cbs_fragment_reset(frag);
  154. av_freep(&frag->units);
  155. frag->nb_units_allocated = 0;
  156. }
  157. static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
  158. CodedBitstreamFragment *frag)
  159. {
  160. int err, i, j;
  161. for (i = 0; i < frag->nb_units; i++) {
  162. CodedBitstreamUnit *unit = &frag->units[i];
  163. if (ctx->decompose_unit_types) {
  164. for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
  165. if (ctx->decompose_unit_types[j] == unit->type)
  166. break;
  167. }
  168. if (j >= ctx->nb_decompose_unit_types)
  169. continue;
  170. }
  171. ff_refstruct_unref(&unit->content_ref);
  172. unit->content = NULL;
  173. av_assert0(unit->data && unit->data_ref);
  174. err = ctx->codec->read_unit(ctx, unit);
  175. if (err == AVERROR(ENOSYS)) {
  176. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  177. "Decomposition unimplemented for unit %d "
  178. "(type %"PRIu32").\n", i, unit->type);
  179. } else if (err == AVERROR(EAGAIN)) {
  180. av_log(ctx->log_ctx, AV_LOG_VERBOSE,
  181. "Skipping decomposition of unit %d "
  182. "(type %"PRIu32").\n", i, unit->type);
  183. ff_refstruct_unref(&unit->content_ref);
  184. unit->content = NULL;
  185. } else if (err < 0) {
  186. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
  187. "(type %"PRIu32").\n", i, unit->type);
  188. return err;
  189. }
  190. }
  191. return 0;
  192. }
  193. static int cbs_fill_fragment_data(CodedBitstreamFragment *frag,
  194. const uint8_t *data, size_t size)
  195. {
  196. av_assert0(!frag->data && !frag->data_ref);
  197. frag->data_ref =
  198. av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  199. if (!frag->data_ref)
  200. return AVERROR(ENOMEM);
  201. frag->data = frag->data_ref->data;
  202. frag->data_size = size;
  203. memcpy(frag->data, data, size);
  204. memset(frag->data + size, 0,
  205. AV_INPUT_BUFFER_PADDING_SIZE);
  206. return 0;
  207. }
  208. static int cbs_read_data(CodedBitstreamContext *ctx,
  209. CodedBitstreamFragment *frag,
  210. AVBufferRef *buf,
  211. const uint8_t *data, size_t size,
  212. int header)
  213. {
  214. int err;
  215. if (buf) {
  216. frag->data_ref = av_buffer_ref(buf);
  217. if (!frag->data_ref)
  218. return AVERROR(ENOMEM);
  219. frag->data = (uint8_t *)data;
  220. frag->data_size = size;
  221. } else {
  222. err = cbs_fill_fragment_data(frag, data, size);
  223. if (err < 0)
  224. return err;
  225. }
  226. err = ctx->codec->split_fragment(ctx, frag, header);
  227. if (err < 0)
  228. return err;
  229. return cbs_read_fragment_content(ctx, frag);
  230. }
  231. int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
  232. CodedBitstreamFragment *frag,
  233. const AVCodecParameters *par)
  234. {
  235. return cbs_read_data(ctx, frag, NULL,
  236. par->extradata,
  237. par->extradata_size, 1);
  238. }
  239. int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx,
  240. CodedBitstreamFragment *frag,
  241. const AVCodecContext *avctx)
  242. {
  243. return cbs_read_data(ctx, frag, NULL,
  244. avctx->extradata,
  245. avctx->extradata_size, 1);
  246. }
  247. int ff_cbs_read_packet(CodedBitstreamContext *ctx,
  248. CodedBitstreamFragment *frag,
  249. const AVPacket *pkt)
  250. {
  251. return cbs_read_data(ctx, frag, pkt->buf,
  252. pkt->data, pkt->size, 0);
  253. }
  254. int ff_cbs_read_packet_side_data(CodedBitstreamContext *ctx,
  255. CodedBitstreamFragment *frag,
  256. const AVPacket *pkt)
  257. {
  258. size_t side_data_size;
  259. const uint8_t *side_data =
  260. av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  261. &side_data_size);
  262. return cbs_read_data(ctx, frag, NULL,
  263. side_data, side_data_size, 1);
  264. }
  265. int ff_cbs_read(CodedBitstreamContext *ctx,
  266. CodedBitstreamFragment *frag,
  267. const uint8_t *data, size_t size)
  268. {
  269. return cbs_read_data(ctx, frag, NULL,
  270. data, size, 0);
  271. }
  272. /**
  273. * Allocate a new internal data buffer of the given size in the unit.
  274. *
  275. * The data buffer will have input padding.
  276. */
  277. static int cbs_alloc_unit_data(CodedBitstreamUnit *unit,
  278. size_t size)
  279. {
  280. av_assert0(!unit->data && !unit->data_ref);
  281. unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  282. if (!unit->data_ref)
  283. return AVERROR(ENOMEM);
  284. unit->data = unit->data_ref->data;
  285. unit->data_size = size;
  286. memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  287. return 0;
  288. }
  289. static int cbs_write_unit_data(CodedBitstreamContext *ctx,
  290. CodedBitstreamUnit *unit)
  291. {
  292. PutBitContext pbc;
  293. int ret;
  294. if (!ctx->write_buffer) {
  295. // Initial write buffer size is 1MB.
  296. ctx->write_buffer_size = 1024 * 1024;
  297. reallocate_and_try_again:
  298. ret = av_reallocp(&ctx->write_buffer, ctx->write_buffer_size);
  299. if (ret < 0) {
  300. av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
  301. "sufficiently large write buffer (last attempt "
  302. "%"SIZE_SPECIFIER" bytes).\n", ctx->write_buffer_size);
  303. return ret;
  304. }
  305. }
  306. init_put_bits(&pbc, ctx->write_buffer, ctx->write_buffer_size);
  307. ret = ctx->codec->write_unit(ctx, unit, &pbc);
  308. if (ret < 0) {
  309. if (ret == AVERROR(ENOSPC)) {
  310. // Overflow.
  311. if (ctx->write_buffer_size == INT_MAX / 8)
  312. return AVERROR(ENOMEM);
  313. ctx->write_buffer_size = FFMIN(2 * ctx->write_buffer_size, INT_MAX / 8);
  314. goto reallocate_and_try_again;
  315. }
  316. // Write failed for some other reason.
  317. return ret;
  318. }
  319. // Overflow but we didn't notice.
  320. av_assert0(put_bits_count(&pbc) <= 8 * ctx->write_buffer_size);
  321. if (put_bits_count(&pbc) % 8)
  322. unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
  323. else
  324. unit->data_bit_padding = 0;
  325. flush_put_bits(&pbc);
  326. ret = cbs_alloc_unit_data(unit, put_bytes_output(&pbc));
  327. if (ret < 0)
  328. return ret;
  329. memcpy(unit->data, ctx->write_buffer, unit->data_size);
  330. return 0;
  331. }
  332. int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
  333. CodedBitstreamFragment *frag)
  334. {
  335. int err, i;
  336. for (i = 0; i < frag->nb_units; i++) {
  337. CodedBitstreamUnit *unit = &frag->units[i];
  338. if (!unit->content)
  339. continue;
  340. av_buffer_unref(&unit->data_ref);
  341. unit->data = NULL;
  342. err = cbs_write_unit_data(ctx, unit);
  343. if (err < 0) {
  344. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
  345. "(type %"PRIu32").\n", i, unit->type);
  346. return err;
  347. }
  348. av_assert0(unit->data && unit->data_ref);
  349. }
  350. av_buffer_unref(&frag->data_ref);
  351. frag->data = NULL;
  352. err = ctx->codec->assemble_fragment(ctx, frag);
  353. if (err < 0) {
  354. av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
  355. return err;
  356. }
  357. av_assert0(frag->data && frag->data_ref);
  358. return 0;
  359. }
  360. int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
  361. AVCodecParameters *par,
  362. CodedBitstreamFragment *frag)
  363. {
  364. int err;
  365. err = ff_cbs_write_fragment_data(ctx, frag);
  366. if (err < 0)
  367. return err;
  368. av_freep(&par->extradata);
  369. par->extradata_size = 0;
  370. if (!frag->data_size)
  371. return 0;
  372. par->extradata = av_malloc(frag->data_size +
  373. AV_INPUT_BUFFER_PADDING_SIZE);
  374. if (!par->extradata)
  375. return AVERROR(ENOMEM);
  376. memcpy(par->extradata, frag->data, frag->data_size);
  377. memset(par->extradata + frag->data_size, 0,
  378. AV_INPUT_BUFFER_PADDING_SIZE);
  379. par->extradata_size = frag->data_size;
  380. return 0;
  381. }
  382. int ff_cbs_write_packet(CodedBitstreamContext *ctx,
  383. AVPacket *pkt,
  384. CodedBitstreamFragment *frag)
  385. {
  386. AVBufferRef *buf;
  387. int err;
  388. err = ff_cbs_write_fragment_data(ctx, frag);
  389. if (err < 0)
  390. return err;
  391. buf = av_buffer_ref(frag->data_ref);
  392. if (!buf)
  393. return AVERROR(ENOMEM);
  394. av_buffer_unref(&pkt->buf);
  395. pkt->buf = buf;
  396. pkt->data = frag->data;
  397. pkt->size = frag->data_size;
  398. return 0;
  399. }
  400. void ff_cbs_trace_header(CodedBitstreamContext *ctx,
  401. const char *name)
  402. {
  403. if (!ctx->trace_enable)
  404. return;
  405. av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
  406. }
  407. void ff_cbs_trace_read_log(void *trace_context,
  408. GetBitContext *gbc, int length,
  409. const char *str, const int *subscripts,
  410. int64_t value)
  411. {
  412. CodedBitstreamContext *ctx = trace_context;
  413. char name[256];
  414. char bits[256];
  415. size_t name_len, bits_len;
  416. int pad, subs, i, j, k, n;
  417. int position;
  418. av_assert0(value >= INT_MIN && value <= UINT32_MAX);
  419. position = get_bits_count(gbc);
  420. av_assert0(length < 256);
  421. for (i = 0; i < length; i++)
  422. bits[i] = get_bits1(gbc) ? '1' : '0';
  423. bits[length] = 0;
  424. subs = subscripts ? subscripts[0] : 0;
  425. n = 0;
  426. for (i = j = 0; str[i];) {
  427. if (str[i] == '[') {
  428. if (n < subs) {
  429. ++n;
  430. k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
  431. av_assert0(k > 0 && j + k < sizeof(name));
  432. j += k;
  433. for (++i; str[i] && str[i] != ']'; i++);
  434. av_assert0(str[i] == ']');
  435. } else {
  436. while (str[i] && str[i] != ']')
  437. name[j++] = str[i++];
  438. av_assert0(str[i] == ']');
  439. }
  440. } else {
  441. av_assert0(j + 1 < sizeof(name));
  442. name[j++] = str[i++];
  443. }
  444. }
  445. av_assert0(j + 1 < sizeof(name));
  446. name[j] = 0;
  447. av_assert0(n == subs);
  448. name_len = strlen(name);
  449. bits_len = length;
  450. if (name_len + bits_len > 60)
  451. pad = bits_len + 2;
  452. else
  453. pad = 61 - name_len;
  454. av_log(ctx->log_ctx, ctx->trace_level, "%-10d %s%*s = %"PRId64"\n",
  455. position, name, pad, bits, value);
  456. }
  457. void ff_cbs_trace_write_log(void *trace_context,
  458. PutBitContext *pbc, int length,
  459. const char *str, const int *subscripts,
  460. int64_t value)
  461. {
  462. CodedBitstreamContext *ctx = trace_context;
  463. // Ensure that the syntax element is written to the output buffer,
  464. // make a GetBitContext pointed at the start position, then call the
  465. // read log function which can read the bits back to log them.
  466. GetBitContext gbc;
  467. int position;
  468. if (length > 0) {
  469. PutBitContext flush;
  470. flush = *pbc;
  471. flush_put_bits(&flush);
  472. }
  473. position = put_bits_count(pbc);
  474. av_assert0(position >= length);
  475. init_get_bits(&gbc, pbc->buf, position);
  476. skip_bits_long(&gbc, position - length);
  477. ff_cbs_trace_read_log(ctx, &gbc, length, str, subscripts, value);
  478. }
  479. static av_always_inline int cbs_read_unsigned(CodedBitstreamContext *ctx,
  480. GetBitContext *gbc,
  481. int width, const char *name,
  482. const int *subscripts,
  483. uint32_t *write_to,
  484. uint32_t range_min,
  485. uint32_t range_max)
  486. {
  487. uint32_t value;
  488. CBS_TRACE_READ_START();
  489. av_assert0(width > 0 && width <= 32);
  490. if (get_bits_left(gbc) < width) {
  491. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  492. "%s: bitstream ended.\n", name);
  493. return AVERROR_INVALIDDATA;
  494. }
  495. value = get_bits_long(gbc, width);
  496. CBS_TRACE_READ_END();
  497. if (value < range_min || value > range_max) {
  498. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  499. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  500. name, value, range_min, range_max);
  501. return AVERROR_INVALIDDATA;
  502. }
  503. *write_to = value;
  504. return 0;
  505. }
  506. int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  507. int width, const char *name,
  508. const int *subscripts, uint32_t *write_to,
  509. uint32_t range_min, uint32_t range_max)
  510. {
  511. return cbs_read_unsigned(ctx, gbc, width, name, subscripts,
  512. write_to, range_min, range_max);
  513. }
  514. int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
  515. int width, const char *name, uint32_t *write_to)
  516. {
  517. return cbs_read_unsigned(ctx, gbc, width, name, NULL,
  518. write_to, 0, UINT32_MAX);
  519. }
  520. int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  521. int width, const char *name,
  522. const int *subscripts, uint32_t value,
  523. uint32_t range_min, uint32_t range_max)
  524. {
  525. CBS_TRACE_WRITE_START();
  526. av_assert0(width > 0 && width <= 32);
  527. if (value < range_min || value > range_max) {
  528. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  529. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  530. name, value, range_min, range_max);
  531. return AVERROR_INVALIDDATA;
  532. }
  533. if (put_bits_left(pbc) < width)
  534. return AVERROR(ENOSPC);
  535. if (width < 32)
  536. put_bits(pbc, width, value);
  537. else
  538. put_bits32(pbc, value);
  539. CBS_TRACE_WRITE_END();
  540. return 0;
  541. }
  542. int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
  543. int width, const char *name, uint32_t value)
  544. {
  545. return ff_cbs_write_unsigned(ctx, pbc, width, name, NULL,
  546. value, 0, MAX_UINT_BITS(width));
  547. }
  548. int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
  549. int width, const char *name,
  550. const int *subscripts, int32_t *write_to,
  551. int32_t range_min, int32_t range_max)
  552. {
  553. int32_t value;
  554. CBS_TRACE_READ_START();
  555. av_assert0(width > 0 && width <= 32);
  556. if (get_bits_left(gbc) < width) {
  557. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
  558. "%s: bitstream ended.\n", name);
  559. return AVERROR_INVALIDDATA;
  560. }
  561. value = get_sbits_long(gbc, width);
  562. CBS_TRACE_READ_END();
  563. if (value < range_min || value > range_max) {
  564. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  565. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  566. name, value, range_min, range_max);
  567. return AVERROR_INVALIDDATA;
  568. }
  569. *write_to = value;
  570. return 0;
  571. }
  572. int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
  573. int width, const char *name,
  574. const int *subscripts, int32_t value,
  575. int32_t range_min, int32_t range_max)
  576. {
  577. CBS_TRACE_WRITE_START();
  578. av_assert0(width > 0 && width <= 32);
  579. if (value < range_min || value > range_max) {
  580. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  581. "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
  582. name, value, range_min, range_max);
  583. return AVERROR_INVALIDDATA;
  584. }
  585. if (put_bits_left(pbc) < width)
  586. return AVERROR(ENOSPC);
  587. if (width < 32)
  588. put_sbits(pbc, width, value);
  589. else
  590. put_bits32(pbc, value);
  591. CBS_TRACE_WRITE_END();
  592. return 0;
  593. }
  594. static int cbs_insert_unit(CodedBitstreamFragment *frag,
  595. int position)
  596. {
  597. CodedBitstreamUnit *units;
  598. if (frag->nb_units < frag->nb_units_allocated) {
  599. units = frag->units;
  600. if (position < frag->nb_units)
  601. memmove(units + position + 1, units + position,
  602. (frag->nb_units - position) * sizeof(*units));
  603. } else {
  604. units = av_malloc_array(frag->nb_units*2 + 1, sizeof(*units));
  605. if (!units)
  606. return AVERROR(ENOMEM);
  607. frag->nb_units_allocated = 2*frag->nb_units_allocated + 1;
  608. if (position > 0)
  609. memcpy(units, frag->units, position * sizeof(*units));
  610. if (position < frag->nb_units)
  611. memcpy(units + position + 1, frag->units + position,
  612. (frag->nb_units - position) * sizeof(*units));
  613. }
  614. memset(units + position, 0, sizeof(*units));
  615. if (units != frag->units) {
  616. av_free(frag->units);
  617. frag->units = units;
  618. }
  619. ++frag->nb_units;
  620. return 0;
  621. }
  622. int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
  623. int position,
  624. CodedBitstreamUnitType type,
  625. void *content,
  626. void *content_ref)
  627. {
  628. CodedBitstreamUnit *unit;
  629. int err;
  630. if (position == -1)
  631. position = frag->nb_units;
  632. av_assert0(position >= 0 && position <= frag->nb_units);
  633. err = cbs_insert_unit(frag, position);
  634. if (err < 0)
  635. return err;
  636. if (content_ref) {
  637. // Create our own reference out of the user-supplied one.
  638. content_ref = ff_refstruct_ref(content_ref);
  639. }
  640. unit = &frag->units[position];
  641. unit->type = type;
  642. unit->content = content;
  643. unit->content_ref = content_ref;
  644. return 0;
  645. }
  646. static int cbs_insert_unit_data(CodedBitstreamFragment *frag,
  647. CodedBitstreamUnitType type,
  648. uint8_t *data, size_t data_size,
  649. AVBufferRef *data_buf,
  650. int position)
  651. {
  652. CodedBitstreamUnit *unit;
  653. AVBufferRef *data_ref;
  654. int err;
  655. av_assert0(position >= 0 && position <= frag->nb_units);
  656. if (data_buf)
  657. data_ref = av_buffer_ref(data_buf);
  658. else
  659. data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
  660. if (!data_ref) {
  661. if (!data_buf)
  662. av_free(data);
  663. return AVERROR(ENOMEM);
  664. }
  665. err = cbs_insert_unit(frag, position);
  666. if (err < 0) {
  667. av_buffer_unref(&data_ref);
  668. return err;
  669. }
  670. unit = &frag->units[position];
  671. unit->type = type;
  672. unit->data = data;
  673. unit->data_size = data_size;
  674. unit->data_ref = data_ref;
  675. return 0;
  676. }
  677. int ff_cbs_append_unit_data(CodedBitstreamFragment *frag,
  678. CodedBitstreamUnitType type,
  679. uint8_t *data, size_t data_size,
  680. AVBufferRef *data_buf)
  681. {
  682. return cbs_insert_unit_data(frag, type,
  683. data, data_size, data_buf,
  684. frag->nb_units);
  685. }
  686. void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
  687. int position)
  688. {
  689. av_assert0(0 <= position && position < frag->nb_units
  690. && "Unit to be deleted not in fragment.");
  691. cbs_unit_uninit(&frag->units[position]);
  692. --frag->nb_units;
  693. if (frag->nb_units > 0)
  694. memmove(frag->units + position,
  695. frag->units + position + 1,
  696. (frag->nb_units - position) * sizeof(*frag->units));
  697. }
  698. static void cbs_default_free_unit_content(FFRefStructOpaque opaque, void *content)
  699. {
  700. const CodedBitstreamUnitTypeDescriptor *desc = opaque.c;
  701. for (int i = 0; i < desc->type.ref.nb_offsets; i++) {
  702. void **ptr = (void**)((char*)content + desc->type.ref.offsets[i]);
  703. av_buffer_unref((AVBufferRef**)(ptr + 1));
  704. }
  705. }
  706. static const CodedBitstreamUnitTypeDescriptor
  707. *cbs_find_unit_type_desc(CodedBitstreamContext *ctx,
  708. CodedBitstreamUnit *unit)
  709. {
  710. const CodedBitstreamUnitTypeDescriptor *desc;
  711. int i, j;
  712. if (!ctx->codec->unit_types)
  713. return NULL;
  714. for (i = 0;; i++) {
  715. desc = &ctx->codec->unit_types[i];
  716. if (desc->nb_unit_types == 0)
  717. break;
  718. if (desc->nb_unit_types == CBS_UNIT_TYPE_RANGE) {
  719. if (unit->type >= desc->unit_type.range.start &&
  720. unit->type <= desc->unit_type.range.end)
  721. return desc;
  722. } else {
  723. for (j = 0; j < desc->nb_unit_types; j++) {
  724. if (desc->unit_type.list[j] == unit->type)
  725. return desc;
  726. }
  727. }
  728. }
  729. return NULL;
  730. }
  731. static void *cbs_alloc_content(const CodedBitstreamUnitTypeDescriptor *desc)
  732. {
  733. return ff_refstruct_alloc_ext_c(desc->content_size, 0,
  734. (FFRefStructOpaque){ .c = desc },
  735. desc->content_type == CBS_CONTENT_TYPE_COMPLEX
  736. ? desc->type.complex.content_free
  737. : cbs_default_free_unit_content);
  738. }
  739. int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
  740. CodedBitstreamUnit *unit)
  741. {
  742. const CodedBitstreamUnitTypeDescriptor *desc;
  743. av_assert0(!unit->content && !unit->content_ref);
  744. desc = cbs_find_unit_type_desc(ctx, unit);
  745. if (!desc)
  746. return AVERROR(ENOSYS);
  747. unit->content_ref = cbs_alloc_content(desc);
  748. if (!unit->content_ref)
  749. return AVERROR(ENOMEM);
  750. unit->content = unit->content_ref;
  751. return 0;
  752. }
  753. static int cbs_clone_noncomplex_unit_content(void **clonep,
  754. const CodedBitstreamUnit *unit,
  755. const CodedBitstreamUnitTypeDescriptor *desc)
  756. {
  757. const uint8_t *src;
  758. uint8_t *copy;
  759. int err, i;
  760. av_assert0(unit->content);
  761. src = unit->content;
  762. copy = cbs_alloc_content(desc);
  763. if (!copy)
  764. return AVERROR(ENOMEM);
  765. memcpy(copy, src, desc->content_size);
  766. for (int i = 0; i < desc->type.ref.nb_offsets; i++) {
  767. void **ptr = (void**)(copy + desc->type.ref.offsets[i]);
  768. /* Zero all the AVBufferRefs as they are owned by src. */
  769. *(ptr + 1) = NULL;
  770. }
  771. for (i = 0; i < desc->type.ref.nb_offsets; i++) {
  772. const uint8_t *const *src_ptr = (const uint8_t* const*)(src + desc->type.ref.offsets[i]);
  773. const AVBufferRef *src_buf = *(AVBufferRef**)(src_ptr + 1);
  774. uint8_t **copy_ptr = (uint8_t**)(copy + desc->type.ref.offsets[i]);
  775. AVBufferRef **copy_buf = (AVBufferRef**)(copy_ptr + 1);
  776. if (!*src_ptr) {
  777. av_assert0(!src_buf);
  778. continue;
  779. }
  780. if (!src_buf) {
  781. // We can't handle a non-refcounted pointer here - we don't
  782. // have enough information to handle whatever structure lies
  783. // at the other end of it.
  784. err = AVERROR(EINVAL);
  785. goto fail;
  786. }
  787. *copy_buf = av_buffer_ref(src_buf);
  788. if (!*copy_buf) {
  789. err = AVERROR(ENOMEM);
  790. goto fail;
  791. }
  792. }
  793. *clonep = copy;
  794. return 0;
  795. fail:
  796. ff_refstruct_unref(&copy);
  797. return err;
  798. }
  799. /*
  800. * On success, unit->content and unit->content_ref are updated with
  801. * the new content; unit is untouched on failure.
  802. * Any old content_ref is simply overwritten and not freed.
  803. */
  804. static int cbs_clone_unit_content(CodedBitstreamContext *ctx,
  805. CodedBitstreamUnit *unit)
  806. {
  807. const CodedBitstreamUnitTypeDescriptor *desc;
  808. void *new_content;
  809. int err;
  810. desc = cbs_find_unit_type_desc(ctx, unit);
  811. if (!desc)
  812. return AVERROR(ENOSYS);
  813. switch (desc->content_type) {
  814. case CBS_CONTENT_TYPE_INTERNAL_REFS:
  815. err = cbs_clone_noncomplex_unit_content(&new_content, unit, desc);
  816. break;
  817. case CBS_CONTENT_TYPE_COMPLEX:
  818. if (!desc->type.complex.content_clone)
  819. return AVERROR_PATCHWELCOME;
  820. err = desc->type.complex.content_clone(&new_content, unit);
  821. break;
  822. default:
  823. av_assert0(0 && "Invalid content type.");
  824. }
  825. if (err < 0)
  826. return err;
  827. unit->content_ref = new_content;
  828. unit->content = new_content;
  829. return 0;
  830. }
  831. int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx,
  832. CodedBitstreamUnit *unit)
  833. {
  834. av_assert0(unit->content);
  835. if (unit->content_ref)
  836. return 0;
  837. return cbs_clone_unit_content(ctx, unit);
  838. }
  839. int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
  840. CodedBitstreamUnit *unit)
  841. {
  842. void *ref = unit->content_ref;
  843. int err;
  844. av_assert0(unit->content);
  845. if (ref && ff_refstruct_exclusive(ref))
  846. return 0;
  847. err = cbs_clone_unit_content(ctx, unit);
  848. if (err < 0)
  849. return err;
  850. ff_refstruct_unref(&ref);
  851. return 0;
  852. }
  853. void ff_cbs_discard_units(CodedBitstreamContext *ctx,
  854. CodedBitstreamFragment *frag,
  855. enum AVDiscard skip,
  856. int flags)
  857. {
  858. if (!ctx->codec->discarded_unit)
  859. return;
  860. for (int i = frag->nb_units - 1; i >= 0; i--) {
  861. if (ctx->codec->discarded_unit(ctx, &frag->units[i], skip)) {
  862. // discard all units
  863. if (!(flags & DISCARD_FLAG_KEEP_NON_VCL)) {
  864. ff_cbs_fragment_free(frag);
  865. return;
  866. }
  867. ff_cbs_delete_unit(frag, i);
  868. }
  869. }
  870. }