cbs_av1.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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 "libavutil/attributes.h"
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/opt.h"
  21. #include "libavutil/pixfmt.h"
  22. #include "cbs.h"
  23. #include "cbs_internal.h"
  24. #include "cbs_av1.h"
  25. #include "defs.h"
  26. #include "libavutil/refstruct.h"
  27. #if CBS_READ
  28. static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc,
  29. const char *name, uint32_t *write_to,
  30. uint32_t range_min, uint32_t range_max)
  31. {
  32. uint32_t zeroes, bits_value, value;
  33. CBS_TRACE_READ_START();
  34. zeroes = 0;
  35. while (zeroes < 32) {
  36. if (get_bits_left(gbc) < 1) {
  37. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
  38. "%s: bitstream ended.\n", name);
  39. return AVERROR_INVALIDDATA;
  40. }
  41. if (get_bits1(gbc))
  42. break;
  43. ++zeroes;
  44. }
  45. if (zeroes >= 32) {
  46. // The spec allows at least thirty-two zero bits followed by a
  47. // one to mean 2^32-1, with no constraint on the number of
  48. // zeroes. The libaom reference decoder does not match this,
  49. // instead reading thirty-two zeroes but not the following one
  50. // to mean 2^32-1. These two interpretations are incompatible
  51. // and other implementations may follow one or the other.
  52. // Therefore we reject thirty-two zeroes because the intended
  53. // behaviour is not clear.
  54. av_log(ctx->log_ctx, AV_LOG_ERROR, "Thirty-two zero bits in "
  55. "%s uvlc code: considered invalid due to conflicting "
  56. "standard and reference decoder behaviour.\n", name);
  57. return AVERROR_INVALIDDATA;
  58. } else {
  59. if (get_bits_left(gbc) < zeroes) {
  60. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid uvlc code at "
  61. "%s: bitstream ended.\n", name);
  62. return AVERROR_INVALIDDATA;
  63. }
  64. bits_value = get_bits_long(gbc, zeroes);
  65. value = bits_value + (UINT32_C(1) << zeroes) - 1;
  66. }
  67. CBS_TRACE_READ_END_NO_SUBSCRIPTS();
  68. if (value < range_min || value > range_max) {
  69. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  70. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  71. name, value, range_min, range_max);
  72. return AVERROR_INVALIDDATA;
  73. }
  74. *write_to = value;
  75. return 0;
  76. }
  77. #endif
  78. #if CBS_WRITE
  79. static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc,
  80. const char *name, uint32_t value,
  81. uint32_t range_min, uint32_t range_max)
  82. {
  83. uint32_t v;
  84. int zeroes;
  85. CBS_TRACE_WRITE_START();
  86. if (value < range_min || value > range_max) {
  87. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  88. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  89. name, value, range_min, range_max);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. zeroes = av_log2(value + 1);
  93. v = value - (1U << zeroes) + 1;
  94. if (put_bits_left(pbc) < 2 * zeroes + 1)
  95. return AVERROR(ENOSPC);
  96. put_bits(pbc, zeroes, 0);
  97. put_bits(pbc, 1, 1);
  98. put_bits(pbc, zeroes, v);
  99. CBS_TRACE_WRITE_END_NO_SUBSCRIPTS();
  100. return 0;
  101. }
  102. #endif
  103. #if CBS_READ
  104. static int cbs_av1_read_leb128(CodedBitstreamContext *ctx, GetBitContext *gbc,
  105. const char *name, uint64_t *write_to)
  106. {
  107. uint64_t value;
  108. uint32_t byte;
  109. int i;
  110. CBS_TRACE_READ_START();
  111. value = 0;
  112. for (i = 0; i < 8; i++) {
  113. if (get_bits_left(gbc) < 8) {
  114. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid leb128 at "
  115. "%s: bitstream ended.\n", name);
  116. return AVERROR_INVALIDDATA;
  117. }
  118. byte = get_bits(gbc, 8);
  119. value |= (uint64_t)(byte & 0x7f) << (i * 7);
  120. if (!(byte & 0x80))
  121. break;
  122. }
  123. if (value > UINT32_MAX)
  124. return AVERROR_INVALIDDATA;
  125. CBS_TRACE_READ_END_NO_SUBSCRIPTS();
  126. *write_to = value;
  127. return 0;
  128. }
  129. #endif
  130. #if CBS_WRITE
  131. static int cbs_av1_write_leb128(CodedBitstreamContext *ctx, PutBitContext *pbc,
  132. const char *name, uint64_t value, int fixed_length)
  133. {
  134. int len, i;
  135. uint8_t byte;
  136. CBS_TRACE_WRITE_START();
  137. len = (av_log2(value) + 7) / 7;
  138. if (fixed_length) {
  139. if (fixed_length < len) {
  140. av_log(ctx->log_ctx, AV_LOG_ERROR, "OBU is too large for "
  141. "fixed length size field (%d > %d).\n",
  142. len, fixed_length);
  143. return AVERROR(EINVAL);
  144. }
  145. len = fixed_length;
  146. }
  147. for (i = 0; i < len; i++) {
  148. if (put_bits_left(pbc) < 8)
  149. return AVERROR(ENOSPC);
  150. byte = value >> (7 * i) & 0x7f;
  151. if (i < len - 1)
  152. byte |= 0x80;
  153. put_bits(pbc, 8, byte);
  154. }
  155. CBS_TRACE_WRITE_END_NO_SUBSCRIPTS();
  156. return 0;
  157. }
  158. #endif
  159. #if CBS_READ
  160. static int cbs_av1_read_ns(CodedBitstreamContext *ctx, GetBitContext *gbc,
  161. uint32_t n, const char *name,
  162. const int *subscripts, uint32_t *write_to)
  163. {
  164. uint32_t m, v, extra_bit, value;
  165. int w;
  166. CBS_TRACE_READ_START();
  167. av_assert0(n > 0);
  168. w = av_log2(n) + 1;
  169. m = (1 << w) - n;
  170. if (get_bits_left(gbc) < w) {
  171. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid non-symmetric value at "
  172. "%s: bitstream ended.\n", name);
  173. return AVERROR_INVALIDDATA;
  174. }
  175. if (w - 1 > 0)
  176. v = get_bits(gbc, w - 1);
  177. else
  178. v = 0;
  179. if (v < m) {
  180. value = v;
  181. } else {
  182. extra_bit = get_bits1(gbc);
  183. value = (v << 1) - m + extra_bit;
  184. }
  185. CBS_TRACE_READ_END();
  186. *write_to = value;
  187. return 0;
  188. }
  189. #endif
  190. #if CBS_WRITE
  191. static int cbs_av1_write_ns(CodedBitstreamContext *ctx, PutBitContext *pbc,
  192. uint32_t n, const char *name,
  193. const int *subscripts, uint32_t value)
  194. {
  195. uint32_t w, m, v, extra_bit;
  196. CBS_TRACE_WRITE_START();
  197. if (value > n) {
  198. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  199. "%"PRIu32", but must be in [0,%"PRIu32"].\n",
  200. name, value, n);
  201. return AVERROR_INVALIDDATA;
  202. }
  203. w = av_log2(n) + 1;
  204. m = (1 << w) - n;
  205. if (put_bits_left(pbc) < w)
  206. return AVERROR(ENOSPC);
  207. if (value < m) {
  208. v = value;
  209. put_bits(pbc, w - 1, v);
  210. } else {
  211. v = m + ((value - m) >> 1);
  212. extra_bit = (value - m) & 1;
  213. put_bits(pbc, w - 1, v);
  214. put_bits(pbc, 1, extra_bit);
  215. }
  216. CBS_TRACE_WRITE_END();
  217. return 0;
  218. }
  219. #endif
  220. #if CBS_READ
  221. static int cbs_av1_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc,
  222. uint32_t range_min, uint32_t range_max,
  223. const char *name, uint32_t *write_to)
  224. {
  225. uint32_t value;
  226. CBS_TRACE_READ_START();
  227. av_assert0(range_min <= range_max && range_max - range_min < 32);
  228. for (value = range_min; value < range_max;) {
  229. if (get_bits_left(gbc) < 1) {
  230. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
  231. "%s: bitstream ended.\n", name);
  232. return AVERROR_INVALIDDATA;
  233. }
  234. if (get_bits1(gbc))
  235. ++value;
  236. else
  237. break;
  238. }
  239. CBS_TRACE_READ_END_NO_SUBSCRIPTS();
  240. *write_to = value;
  241. return 0;
  242. }
  243. #endif
  244. #if CBS_WRITE
  245. static int cbs_av1_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc,
  246. uint32_t range_min, uint32_t range_max,
  247. const char *name, uint32_t value)
  248. {
  249. int len;
  250. CBS_TRACE_WRITE_START();
  251. av_assert0(range_min <= range_max && range_max - range_min < 32);
  252. if (value < range_min || value > range_max) {
  253. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  254. "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
  255. name, value, range_min, range_max);
  256. return AVERROR_INVALIDDATA;
  257. }
  258. if (value == range_max)
  259. len = range_max - range_min;
  260. else
  261. len = value - range_min + 1;
  262. if (put_bits_left(pbc) < len)
  263. return AVERROR(ENOSPC);
  264. if (len > 0)
  265. put_bits(pbc, len, (1U << len) - 1 - (value != range_max));
  266. CBS_TRACE_WRITE_END_NO_SUBSCRIPTS();
  267. return 0;
  268. }
  269. #endif
  270. #if CBS_READ
  271. static int cbs_av1_read_subexp(CodedBitstreamContext *ctx, GetBitContext *gbc,
  272. uint32_t range_max, const char *name,
  273. const int *subscripts, uint32_t *write_to)
  274. {
  275. uint32_t value, max_len, len, range_offset, range_bits;
  276. int err;
  277. CBS_TRACE_READ_START();
  278. av_assert0(range_max > 0);
  279. max_len = av_log2(range_max - 1) - 3;
  280. err = cbs_av1_read_increment(ctx, gbc, 0, max_len,
  281. "subexp_more_bits", &len);
  282. if (err < 0)
  283. return err;
  284. if (len) {
  285. range_bits = 2 + len;
  286. range_offset = 1 << range_bits;
  287. } else {
  288. range_bits = 3;
  289. range_offset = 0;
  290. }
  291. if (len < max_len) {
  292. err = CBS_FUNC(read_simple_unsigned)(ctx, gbc, range_bits,
  293. "subexp_bits", &value);
  294. if (err < 0)
  295. return err;
  296. } else {
  297. err = cbs_av1_read_ns(ctx, gbc, range_max - range_offset,
  298. "subexp_final_bits", NULL, &value);
  299. if (err < 0)
  300. return err;
  301. }
  302. value += range_offset;
  303. CBS_TRACE_READ_END_VALUE_ONLY();
  304. *write_to = value;
  305. return err;
  306. }
  307. #endif
  308. #if CBS_WRITE
  309. static int cbs_av1_write_subexp(CodedBitstreamContext *ctx, PutBitContext *pbc,
  310. uint32_t range_max, const char *name,
  311. const int *subscripts, uint32_t value)
  312. {
  313. int err;
  314. uint32_t max_len, len, range_offset, range_bits;
  315. CBS_TRACE_WRITE_START();
  316. if (value > range_max) {
  317. av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
  318. "%"PRIu32", but must be in [0,%"PRIu32"].\n",
  319. name, value, range_max);
  320. return AVERROR_INVALIDDATA;
  321. }
  322. av_assert0(range_max > 0);
  323. max_len = av_log2(range_max - 1) - 3;
  324. if (value < 8) {
  325. range_bits = 3;
  326. range_offset = 0;
  327. len = 0;
  328. } else {
  329. range_bits = av_log2(value);
  330. len = range_bits - 2;
  331. if (len > max_len) {
  332. // The top bin is combined with the one below it.
  333. av_assert0(len == max_len + 1);
  334. --range_bits;
  335. len = max_len;
  336. }
  337. range_offset = 1 << range_bits;
  338. }
  339. err = cbs_av1_write_increment(ctx, pbc, 0, max_len,
  340. "subexp_more_bits", len);
  341. if (err < 0)
  342. return err;
  343. if (len < max_len) {
  344. err = CBS_FUNC(write_simple_unsigned)(ctx, pbc, range_bits,
  345. "subexp_bits",
  346. value - range_offset);
  347. if (err < 0)
  348. return err;
  349. } else {
  350. err = cbs_av1_write_ns(ctx, pbc, range_max - range_offset,
  351. "subexp_final_bits", NULL,
  352. value - range_offset);
  353. if (err < 0)
  354. return err;
  355. }
  356. CBS_TRACE_WRITE_END_VALUE_ONLY();
  357. return err;
  358. }
  359. #endif
  360. static int cbs_av1_tile_log2(int blksize, int target)
  361. {
  362. int k;
  363. for (k = 0; (blksize << k) < target; k++);
  364. return k;
  365. }
  366. static int cbs_av1_get_relative_dist(const AV1RawSequenceHeader *seq,
  367. unsigned int a, unsigned int b)
  368. {
  369. unsigned int diff, m;
  370. if (!seq->enable_order_hint)
  371. return 0;
  372. diff = a - b;
  373. m = 1 << seq->order_hint_bits_minus_1;
  374. diff = (diff & (m - 1)) - (diff & m);
  375. return diff;
  376. }
  377. av_unused static size_t cbs_av1_get_payload_bytes_left(GetBitContext *gbc)
  378. {
  379. GetBitContext tmp = *gbc;
  380. size_t size = 0;
  381. for (int i = 0; get_bits_left(&tmp) >= 8; i++) {
  382. if (get_bits(&tmp, 8))
  383. size = i;
  384. }
  385. return size;
  386. }
  387. #define HEADER(name) do { \
  388. CBS_FUNC(trace_header)(ctx, name); \
  389. } while (0)
  390. #define CHECK(call) do { \
  391. err = (call); \
  392. if (err < 0) \
  393. return err; \
  394. } while (0)
  395. #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
  396. #define FUNC_AV1(rw, name) FUNC_NAME(rw, av1, name)
  397. #define FUNC(name) FUNC_AV1(READWRITE, name)
  398. #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
  399. #if CBS_READ
  400. #define fc(width, name, range_min, range_max) \
  401. xf(width, name, current->name, range_min, range_max, 0, )
  402. #define flag(name) fb(1, name)
  403. #define su(width, name) \
  404. xsu(width, name, current->name, 0, )
  405. #define fbs(width, name, subs, ...) \
  406. xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
  407. #define fcs(width, name, range_min, range_max, subs, ...) \
  408. xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
  409. #define flags(name, subs, ...) \
  410. xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
  411. #define sus(width, name, subs, ...) \
  412. xsu(width, name, current->name, subs, __VA_ARGS__)
  413. #define fixed(width, name, value) do { \
  414. av_unused uint32_t fixed_value = value; \
  415. xf(width, name, fixed_value, value, value, 0, ); \
  416. } while (0)
  417. #define READ
  418. #define READWRITE read
  419. #define RWContext GetBitContext
  420. #define fb(width, name) do { \
  421. uint32_t value; \
  422. CHECK(CBS_FUNC(read_simple_unsigned)(ctx, rw, width, \
  423. #name, &value)); \
  424. current->name = value; \
  425. } while (0)
  426. #define xf(width, name, var, range_min, range_max, subs, ...) do { \
  427. uint32_t value; \
  428. CHECK(CBS_FUNC(read_unsigned)(ctx, rw, width, #name, \
  429. SUBSCRIPTS(subs, __VA_ARGS__), \
  430. &value, range_min, range_max)); \
  431. var = value; \
  432. } while (0)
  433. #define xsu(width, name, var, subs, ...) do { \
  434. int32_t value; \
  435. CHECK(CBS_FUNC(read_signed)(ctx, rw, width, #name, \
  436. SUBSCRIPTS(subs, __VA_ARGS__), &value, \
  437. MIN_INT_BITS(width), \
  438. MAX_INT_BITS(width))); \
  439. var = value; \
  440. } while (0)
  441. #define uvlc(name, range_min, range_max) do { \
  442. uint32_t value; \
  443. CHECK(cbs_av1_read_uvlc(ctx, rw, #name, \
  444. &value, range_min, range_max)); \
  445. current->name = value; \
  446. } while (0)
  447. #define ns(max_value, name, subs, ...) do { \
  448. uint32_t value; \
  449. CHECK(cbs_av1_read_ns(ctx, rw, max_value, #name, \
  450. SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
  451. current->name = value; \
  452. } while (0)
  453. #define increment(name, min, max) do { \
  454. uint32_t value; \
  455. CHECK(cbs_av1_read_increment(ctx, rw, min, max, #name, &value)); \
  456. current->name = value; \
  457. } while (0)
  458. #define subexp(name, max, subs, ...) do { \
  459. uint32_t value; \
  460. CHECK(cbs_av1_read_subexp(ctx, rw, max, #name, \
  461. SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
  462. current->name = value; \
  463. } while (0)
  464. #define delta_q(name) do { \
  465. uint8_t delta_coded; \
  466. int8_t delta_q; \
  467. xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
  468. if (delta_coded) \
  469. xsu(1 + 6, name.delta_q, delta_q, 0, ); \
  470. else \
  471. delta_q = 0; \
  472. current->name = delta_q; \
  473. } while (0)
  474. #define leb128(name) do { \
  475. uint64_t value; \
  476. CHECK(cbs_av1_read_leb128(ctx, rw, #name, &value)); \
  477. current->name = value; \
  478. } while (0)
  479. #define infer(name, value) do { \
  480. current->name = value; \
  481. } while (0)
  482. #define byte_alignment(rw) (get_bits_count(rw) % 8)
  483. #include "cbs_av1_syntax_template.c"
  484. #undef READ
  485. #undef READWRITE
  486. #undef RWContext
  487. #undef fb
  488. #undef xf
  489. #undef xsu
  490. #undef uvlc
  491. #undef ns
  492. #undef increment
  493. #undef subexp
  494. #undef delta_q
  495. #undef leb128
  496. #undef infer
  497. #undef byte_alignment
  498. #endif // CBS_READ
  499. #if CBS_WRITE
  500. #define WRITE
  501. #define READWRITE write
  502. #define RWContext PutBitContext
  503. #define fb(width, name) do { \
  504. CHECK(CBS_FUNC(write_simple_unsigned)(ctx, rw, width, #name, \
  505. current->name)); \
  506. } while (0)
  507. #define xf(width, name, var, range_min, range_max, subs, ...) do { \
  508. CHECK(CBS_FUNC(write_unsigned)(ctx, rw, width, #name, \
  509. SUBSCRIPTS(subs, __VA_ARGS__), \
  510. var, range_min, range_max)); \
  511. } while (0)
  512. #define xsu(width, name, var, subs, ...) do { \
  513. CHECK(CBS_FUNC(write_signed)(ctx, rw, width, #name, \
  514. SUBSCRIPTS(subs, __VA_ARGS__), var, \
  515. MIN_INT_BITS(width), \
  516. MAX_INT_BITS(width))); \
  517. } while (0)
  518. #define uvlc(name, range_min, range_max) do { \
  519. CHECK(cbs_av1_write_uvlc(ctx, rw, #name, current->name, \
  520. range_min, range_max)); \
  521. } while (0)
  522. #define ns(max_value, name, subs, ...) do { \
  523. CHECK(cbs_av1_write_ns(ctx, rw, max_value, #name, \
  524. SUBSCRIPTS(subs, __VA_ARGS__), \
  525. current->name)); \
  526. } while (0)
  527. #define increment(name, min, max) do { \
  528. CHECK(cbs_av1_write_increment(ctx, rw, min, max, #name, \
  529. current->name)); \
  530. } while (0)
  531. #define subexp(name, max, subs, ...) do { \
  532. CHECK(cbs_av1_write_subexp(ctx, rw, max, #name, \
  533. SUBSCRIPTS(subs, __VA_ARGS__), \
  534. current->name)); \
  535. } while (0)
  536. #define delta_q(name) do { \
  537. xf(1, name.delta_coded, current->name != 0, 0, 1, 0, ); \
  538. if (current->name) \
  539. xsu(1 + 6, name.delta_q, current->name, 0, ); \
  540. } while (0)
  541. #define leb128(name) do { \
  542. CHECK(cbs_av1_write_leb128(ctx, rw, #name, current->name, 0)); \
  543. } while (0)
  544. #define infer(name, value) do { \
  545. if (current->name != (value)) { \
  546. av_log(ctx->log_ctx, AV_LOG_ERROR, \
  547. "%s does not match inferred value: " \
  548. "%"PRId64", but should be %"PRId64".\n", \
  549. #name, (int64_t)current->name, (int64_t)(value)); \
  550. return AVERROR_INVALIDDATA; \
  551. } \
  552. } while (0)
  553. #define byte_alignment(rw) (put_bits_count(rw) % 8)
  554. #include "cbs_av1_syntax_template.c"
  555. #undef WRITE
  556. #undef READWRITE
  557. #undef RWContext
  558. #undef fb
  559. #undef xf
  560. #undef xsu
  561. #undef uvlc
  562. #undef ns
  563. #undef increment
  564. #undef subexp
  565. #undef delta_q
  566. #undef leb128
  567. #undef infer
  568. #undef byte_alignment
  569. #endif // CBS_WRITE
  570. static int cbs_av1_split_fragment(CodedBitstreamContext *ctx,
  571. CodedBitstreamFragment *frag,
  572. int header)
  573. {
  574. #if CBS_READ
  575. GetBitContext gbc;
  576. uint8_t *data;
  577. size_t size;
  578. uint64_t obu_length;
  579. int pos, err, trace;
  580. // Don't include this parsing in trace output.
  581. trace = ctx->trace_enable;
  582. ctx->trace_enable = 0;
  583. data = frag->data;
  584. size = frag->data_size;
  585. if (INT_MAX / 8 < size) {
  586. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid fragment: "
  587. "too large (%"SIZE_SPECIFIER" bytes).\n", size);
  588. err = AVERROR_INVALIDDATA;
  589. goto fail;
  590. }
  591. if (header && size && data[0] & 0x80) {
  592. // first bit is nonzero, the extradata does not consist purely of
  593. // OBUs. Expect MP4/Matroska AV1CodecConfigurationRecord
  594. int config_record_version = data[0] & 0x7f;
  595. if (config_record_version != 1) {
  596. av_log(ctx->log_ctx, AV_LOG_ERROR,
  597. "Unknown version %d of AV1CodecConfigurationRecord "
  598. "found!\n",
  599. config_record_version);
  600. err = AVERROR_INVALIDDATA;
  601. goto fail;
  602. }
  603. if (size <= 4) {
  604. if (size < 4) {
  605. av_log(ctx->log_ctx, AV_LOG_WARNING,
  606. "Undersized AV1CodecConfigurationRecord v%d found!\n",
  607. config_record_version);
  608. err = AVERROR_INVALIDDATA;
  609. goto fail;
  610. }
  611. goto success;
  612. }
  613. // In AV1CodecConfigurationRecord v1, actual OBUs start after
  614. // four bytes. Thus set the offset as required for properly
  615. // parsing them.
  616. data += 4;
  617. size -= 4;
  618. }
  619. while (size > 0) {
  620. AV1RawOBUHeader obu_header;
  621. uint64_t obu_size;
  622. init_get_bits(&gbc, data, 8 * size);
  623. err = cbs_av1_read_obu_header(ctx, &gbc, &obu_header);
  624. if (err < 0)
  625. goto fail;
  626. if (obu_header.obu_has_size_field) {
  627. if (get_bits_left(&gbc) < 8) {
  628. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU: fragment "
  629. "too short (%"SIZE_SPECIFIER" bytes).\n", size);
  630. err = AVERROR_INVALIDDATA;
  631. goto fail;
  632. }
  633. err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
  634. if (err < 0)
  635. goto fail;
  636. } else
  637. obu_size = size - 1 - obu_header.obu_extension_flag;
  638. pos = get_bits_count(&gbc);
  639. av_assert0(pos % 8 == 0 && pos / 8 <= size);
  640. obu_length = pos / 8 + obu_size;
  641. if (size < obu_length) {
  642. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
  643. "%"PRIu64", but only %"SIZE_SPECIFIER" bytes remaining in fragment.\n",
  644. obu_length, size);
  645. err = AVERROR_INVALIDDATA;
  646. goto fail;
  647. }
  648. err = CBS_FUNC(append_unit_data)(frag, obu_header.obu_type,
  649. data, obu_length, frag->data_ref);
  650. if (err < 0)
  651. goto fail;
  652. data += obu_length;
  653. size -= obu_length;
  654. }
  655. success:
  656. err = 0;
  657. fail:
  658. ctx->trace_enable = trace;
  659. return err;
  660. #else
  661. return AVERROR(ENOSYS);
  662. #endif
  663. }
  664. #if CBS_READ
  665. static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx,
  666. CodedBitstreamUnit *unit,
  667. GetBitContext *gbc,
  668. AVBufferRef **data_ref,
  669. uint8_t **data, size_t *data_size)
  670. {
  671. int pos;
  672. pos = get_bits_count(gbc);
  673. if (pos >= 8 * unit->data_size) {
  674. av_log(ctx->log_ctx, AV_LOG_ERROR, "Bitstream ended before "
  675. "any data in tile group (%d bits read).\n", pos);
  676. return AVERROR_INVALIDDATA;
  677. }
  678. // Must be byte-aligned at this point.
  679. av_assert0(pos % 8 == 0);
  680. *data_ref = av_buffer_ref(unit->data_ref);
  681. if (!*data_ref)
  682. return AVERROR(ENOMEM);
  683. *data = unit->data + pos / 8;
  684. *data_size = unit->data_size - pos / 8;
  685. return 0;
  686. }
  687. #endif
  688. static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
  689. CodedBitstreamUnit *unit)
  690. {
  691. #if CBS_READ
  692. CodedBitstreamAV1Context *priv = ctx->priv_data;
  693. AV1RawOBU *obu;
  694. GetBitContext gbc;
  695. int err, start_pos, end_pos;
  696. err = CBS_FUNC(alloc_unit_content)(ctx, unit);
  697. if (err < 0)
  698. return err;
  699. obu = unit->content;
  700. err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
  701. if (err < 0)
  702. return err;
  703. err = cbs_av1_read_obu_header(ctx, &gbc, &obu->header);
  704. if (err < 0)
  705. return err;
  706. av_assert0(obu->header.obu_type == unit->type);
  707. if (obu->header.obu_has_size_field) {
  708. uint64_t obu_size;
  709. err = cbs_av1_read_leb128(ctx, &gbc, "obu_size", &obu_size);
  710. if (err < 0)
  711. return err;
  712. obu->obu_size = obu_size;
  713. } else {
  714. if (unit->data_size < 1 + obu->header.obu_extension_flag) {
  715. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid OBU length: "
  716. "unit too short (%"SIZE_SPECIFIER").\n", unit->data_size);
  717. return AVERROR_INVALIDDATA;
  718. }
  719. obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
  720. }
  721. start_pos = get_bits_count(&gbc);
  722. if (obu->header.obu_extension_flag) {
  723. if (obu->header.obu_type != AV1_OBU_SEQUENCE_HEADER &&
  724. obu->header.obu_type != AV1_OBU_TEMPORAL_DELIMITER &&
  725. priv->operating_point_idc) {
  726. int in_temporal_layer =
  727. (priv->operating_point_idc >> priv->temporal_id ) & 1;
  728. int in_spatial_layer =
  729. (priv->operating_point_idc >> (priv->spatial_id + 8)) & 1;
  730. if (!in_temporal_layer || !in_spatial_layer) {
  731. return AVERROR(EAGAIN); // drop_obu()
  732. }
  733. }
  734. }
  735. switch (obu->header.obu_type) {
  736. case AV1_OBU_SEQUENCE_HEADER:
  737. {
  738. err = cbs_av1_read_sequence_header_obu(ctx, &gbc,
  739. &obu->obu.sequence_header);
  740. if (err < 0)
  741. return err;
  742. if (priv->operating_point >= 0) {
  743. AV1RawSequenceHeader *sequence_header = &obu->obu.sequence_header;
  744. if (priv->operating_point > sequence_header->operating_points_cnt_minus_1) {
  745. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid Operating Point %d requested. "
  746. "Must not be higher than %u.\n",
  747. priv->operating_point, sequence_header->operating_points_cnt_minus_1);
  748. return AVERROR(EINVAL);
  749. }
  750. priv->operating_point_idc = sequence_header->operating_point_idc[priv->operating_point];
  751. }
  752. av_refstruct_replace(&priv->sequence_header_ref, unit->content_ref);
  753. priv->sequence_header = &obu->obu.sequence_header;
  754. }
  755. break;
  756. case AV1_OBU_TEMPORAL_DELIMITER:
  757. {
  758. err = cbs_av1_read_temporal_delimiter_obu(ctx, &gbc);
  759. if (err < 0)
  760. return err;
  761. }
  762. break;
  763. case AV1_OBU_FRAME_HEADER:
  764. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  765. {
  766. err = cbs_av1_read_frame_header_obu(ctx, &gbc,
  767. &obu->obu.frame_header,
  768. obu->header.obu_type ==
  769. AV1_OBU_REDUNDANT_FRAME_HEADER,
  770. unit->data_ref);
  771. if (err < 0)
  772. return err;
  773. }
  774. break;
  775. case AV1_OBU_FRAME:
  776. err = cbs_av1_read_frame_obu(ctx, &gbc, &obu->obu.frame,
  777. unit->data_ref);
  778. if (err < 0)
  779. return err;
  780. // fall-through
  781. case AV1_OBU_TILE_GROUP:
  782. {
  783. AV1RawTileGroup *tile_group = obu->header.obu_type == AV1_OBU_FRAME ? &obu->obu.frame.tile_group
  784. : &obu->obu.tile_group;
  785. err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
  786. &tile_group->data_ref,
  787. &tile_group->data,
  788. &tile_group->data_size);
  789. if (err < 0)
  790. return err;
  791. err = cbs_av1_read_tile_group_obu(ctx, &gbc, tile_group);
  792. if (err < 0)
  793. return err;
  794. err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
  795. &tile_group->tile_data.data_ref,
  796. &tile_group->tile_data.data,
  797. &tile_group->tile_data.data_size);
  798. if (err < 0)
  799. return err;
  800. }
  801. break;
  802. #if CBS_AV1_OBU_TILE_LIST
  803. case AV1_OBU_TILE_LIST:
  804. {
  805. err = cbs_av1_read_tile_list_obu(ctx, &gbc,
  806. &obu->obu.tile_list);
  807. if (err < 0)
  808. return err;
  809. err = cbs_av1_ref_tile_data(ctx, unit, &gbc,
  810. &obu->obu.tile_list.tile_data.data_ref,
  811. &obu->obu.tile_list.tile_data.data,
  812. &obu->obu.tile_list.tile_data.data_size);
  813. if (err < 0)
  814. return err;
  815. }
  816. break;
  817. #endif
  818. #if CBS_AV1_OBU_METADATA
  819. case AV1_OBU_METADATA:
  820. {
  821. err = cbs_av1_read_metadata_obu(ctx, &gbc, &obu->obu.metadata);
  822. if (err < 0)
  823. return err;
  824. }
  825. break;
  826. #endif
  827. #if CBS_AV1_OBU_PADDING
  828. case AV1_OBU_PADDING:
  829. {
  830. err = cbs_av1_read_padding_obu(ctx, &gbc, &obu->obu.padding);
  831. if (err < 0)
  832. return err;
  833. }
  834. break;
  835. #endif
  836. default:
  837. return AVERROR(ENOSYS);
  838. }
  839. end_pos = get_bits_count(&gbc);
  840. av_assert0(end_pos <= unit->data_size * 8);
  841. if (obu->obu_size > 0 &&
  842. obu->header.obu_type != AV1_OBU_TILE_GROUP &&
  843. obu->header.obu_type != AV1_OBU_TILE_LIST &&
  844. obu->header.obu_type != AV1_OBU_FRAME) {
  845. int nb_bits = obu->obu_size * 8 + start_pos - end_pos;
  846. if (nb_bits <= 0)
  847. return AVERROR_INVALIDDATA;
  848. err = cbs_av1_read_trailing_bits(ctx, &gbc, nb_bits);
  849. if (err < 0)
  850. return err;
  851. }
  852. return 0;
  853. #else
  854. return AVERROR(ENOSYS);
  855. #endif
  856. }
  857. static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
  858. CodedBitstreamUnit *unit,
  859. PutBitContext *pbc)
  860. {
  861. #if CBS_WRITE
  862. CodedBitstreamAV1Context *priv = ctx->priv_data;
  863. AV1RawOBU *obu = unit->content;
  864. PutBitContext pbc_tmp;
  865. AV1RawTileData *td;
  866. size_t header_size;
  867. int err, start_pos, end_pos, data_pos;
  868. CodedBitstreamAV1Context av1ctx;
  869. // OBUs in the normal bitstream format must contain a size field
  870. // in every OBU (in annex B it is optional, but we don't support
  871. // writing that).
  872. obu->header.obu_has_size_field = 1;
  873. av1ctx = *priv;
  874. if (priv->sequence_header_ref) {
  875. av1ctx.sequence_header_ref = av_refstruct_ref(priv->sequence_header_ref);
  876. }
  877. if (priv->frame_header_ref) {
  878. av1ctx.frame_header_ref = av_buffer_ref(priv->frame_header_ref);
  879. if (!av1ctx.frame_header_ref) {
  880. err = AVERROR(ENOMEM);
  881. goto error;
  882. }
  883. }
  884. err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
  885. if (err < 0)
  886. goto error;
  887. if (obu->header.obu_has_size_field) {
  888. pbc_tmp = *pbc;
  889. if (priv->fixed_obu_size_length) {
  890. for (int i = 0; i < priv->fixed_obu_size_length; i++)
  891. put_bits(pbc, 8, 0);
  892. } else {
  893. // Add space for the size field to fill later.
  894. put_bits32(pbc, 0);
  895. put_bits32(pbc, 0);
  896. }
  897. }
  898. td = NULL;
  899. start_pos = put_bits_count(pbc);
  900. switch (obu->header.obu_type) {
  901. case AV1_OBU_SEQUENCE_HEADER:
  902. {
  903. err = cbs_av1_write_sequence_header_obu(ctx, pbc,
  904. &obu->obu.sequence_header);
  905. if (err < 0)
  906. goto error;
  907. av_refstruct_unref(&priv->sequence_header_ref);
  908. priv->sequence_header = NULL;
  909. err = CBS_FUNC(make_unit_refcounted)(ctx, unit);
  910. if (err < 0)
  911. goto error;
  912. priv->sequence_header_ref = av_refstruct_ref(unit->content_ref);
  913. priv->sequence_header = &obu->obu.sequence_header;
  914. }
  915. break;
  916. case AV1_OBU_TEMPORAL_DELIMITER:
  917. {
  918. err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
  919. if (err < 0)
  920. goto error;
  921. }
  922. break;
  923. case AV1_OBU_FRAME_HEADER:
  924. case AV1_OBU_REDUNDANT_FRAME_HEADER:
  925. {
  926. err = cbs_av1_write_frame_header_obu(ctx, pbc,
  927. &obu->obu.frame_header,
  928. obu->header.obu_type ==
  929. AV1_OBU_REDUNDANT_FRAME_HEADER,
  930. NULL);
  931. if (err < 0)
  932. goto error;
  933. }
  934. break;
  935. case AV1_OBU_FRAME:
  936. err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
  937. if (err < 0)
  938. goto error;
  939. // fall-through
  940. case AV1_OBU_TILE_GROUP:
  941. {
  942. AV1RawTileGroup *tile_group = obu->header.obu_type == AV1_OBU_FRAME ? &obu->obu.frame.tile_group
  943. : &obu->obu.tile_group;
  944. err = cbs_av1_write_tile_group_obu(ctx, pbc, tile_group);
  945. if (err < 0)
  946. goto error;
  947. td = &tile_group->tile_data;
  948. }
  949. break;
  950. #if CBS_AV1_OBU_TILE_LIST
  951. case AV1_OBU_TILE_LIST:
  952. {
  953. err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
  954. if (err < 0)
  955. goto error;
  956. td = &obu->obu.tile_list.tile_data;
  957. }
  958. break;
  959. #endif
  960. #if CBS_AV1_OBU_METADATA
  961. case AV1_OBU_METADATA:
  962. {
  963. err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
  964. if (err < 0)
  965. goto error;
  966. }
  967. break;
  968. #endif
  969. #if CBS_AV1_OBU_PADDING
  970. case AV1_OBU_PADDING:
  971. {
  972. err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding);
  973. if (err < 0)
  974. goto error;
  975. }
  976. break;
  977. #endif
  978. default:
  979. err = AVERROR(ENOSYS);
  980. goto error;
  981. }
  982. end_pos = put_bits_count(pbc);
  983. header_size = (end_pos - start_pos + 7) / 8;
  984. if (td) {
  985. obu->obu_size = header_size + td->data_size;
  986. } else if (header_size > 0) {
  987. // Add trailing bits and recalculate.
  988. err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
  989. if (err < 0)
  990. goto error;
  991. end_pos = put_bits_count(pbc);
  992. obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
  993. } else {
  994. // Empty OBU.
  995. obu->obu_size = 0;
  996. }
  997. end_pos = put_bits_count(pbc);
  998. // Must now be byte-aligned.
  999. av_assert0(end_pos % 8 == 0);
  1000. flush_put_bits(pbc);
  1001. start_pos /= 8;
  1002. end_pos /= 8;
  1003. *pbc = pbc_tmp;
  1004. err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size,
  1005. priv->fixed_obu_size_length);
  1006. if (err < 0)
  1007. goto error;
  1008. data_pos = put_bits_count(pbc) / 8;
  1009. flush_put_bits(pbc);
  1010. av_assert0(data_pos <= start_pos);
  1011. if (8 * obu->obu_size > put_bits_left(pbc)) {
  1012. av_refstruct_unref(&priv->sequence_header_ref);
  1013. av_buffer_unref(&priv->frame_header_ref);
  1014. *priv = av1ctx;
  1015. return AVERROR(ENOSPC);
  1016. }
  1017. if (obu->obu_size > 0) {
  1018. if (!priv->fixed_obu_size_length) {
  1019. memmove(pbc->buf + data_pos,
  1020. pbc->buf + start_pos, header_size);
  1021. } else {
  1022. // The size was fixed so the following data was
  1023. // already written in the correct place.
  1024. }
  1025. skip_put_bytes(pbc, header_size);
  1026. if (td) {
  1027. memcpy(pbc->buf + data_pos + header_size,
  1028. td->data, td->data_size);
  1029. skip_put_bytes(pbc, td->data_size);
  1030. }
  1031. }
  1032. // OBU data must be byte-aligned.
  1033. av_assert0(put_bits_count(pbc) % 8 == 0);
  1034. err = 0;
  1035. error:
  1036. av_refstruct_unref(&av1ctx.sequence_header_ref);
  1037. av_buffer_unref(&av1ctx.frame_header_ref);
  1038. return err;
  1039. #else
  1040. return AVERROR(ENOSYS);
  1041. #endif
  1042. }
  1043. static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,
  1044. CodedBitstreamFragment *frag)
  1045. {
  1046. #if CBS_WRITE
  1047. size_t size, pos;
  1048. int i;
  1049. size = 0;
  1050. for (i = 0; i < frag->nb_units; i++)
  1051. size += frag->units[i].data_size;
  1052. frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  1053. if (!frag->data_ref)
  1054. return AVERROR(ENOMEM);
  1055. frag->data = frag->data_ref->data;
  1056. memset(frag->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  1057. pos = 0;
  1058. for (i = 0; i < frag->nb_units; i++) {
  1059. memcpy(frag->data + pos, frag->units[i].data,
  1060. frag->units[i].data_size);
  1061. pos += frag->units[i].data_size;
  1062. }
  1063. av_assert0(pos == size);
  1064. frag->data_size = size;
  1065. return 0;
  1066. #else
  1067. return AVERROR(ENOSYS);
  1068. #endif
  1069. }
  1070. static av_cold void cbs_av1_flush(CodedBitstreamContext *ctx)
  1071. {
  1072. CodedBitstreamAV1Context *priv = ctx->priv_data;
  1073. av_buffer_unref(&priv->frame_header_ref);
  1074. priv->sequence_header = NULL;
  1075. priv->frame_header = NULL;
  1076. memset(priv->ref, 0, sizeof(priv->ref));
  1077. priv->operating_point_idc = 0;
  1078. priv->seen_frame_header = 0;
  1079. priv->tile_num = 0;
  1080. }
  1081. static av_cold void cbs_av1_close(CodedBitstreamContext *ctx)
  1082. {
  1083. CodedBitstreamAV1Context *priv = ctx->priv_data;
  1084. av_refstruct_unref(&priv->sequence_header_ref);
  1085. av_buffer_unref(&priv->frame_header_ref);
  1086. }
  1087. #if CBS_AV1_OBU_METADATA
  1088. static void cbs_av1_free_metadata(AVRefStructOpaque unused, void *content)
  1089. {
  1090. AV1RawOBU *obu = content;
  1091. AV1RawMetadata *md;
  1092. av_assert0(obu->header.obu_type == AV1_OBU_METADATA);
  1093. md = &obu->obu.metadata;
  1094. switch (md->metadata_type) {
  1095. case AV1_METADATA_TYPE_HDR_CLL:
  1096. case AV1_METADATA_TYPE_HDR_MDCV:
  1097. case AV1_METADATA_TYPE_SCALABILITY:
  1098. case AV1_METADATA_TYPE_TIMECODE:
  1099. break;
  1100. case AV1_METADATA_TYPE_ITUT_T35:
  1101. av_buffer_unref(&md->metadata.itut_t35.payload_ref);
  1102. break;
  1103. default:
  1104. av_buffer_unref(&md->metadata.unknown.payload_ref);
  1105. }
  1106. }
  1107. #endif
  1108. static const CodedBitstreamUnitTypeDescriptor cbs_av1_unit_types[] = {
  1109. CBS_UNIT_TYPE_POD(AV1_OBU_SEQUENCE_HEADER, AV1RawOBU),
  1110. CBS_UNIT_TYPE_POD(AV1_OBU_TEMPORAL_DELIMITER, AV1RawOBU),
  1111. CBS_UNIT_TYPE_POD(AV1_OBU_FRAME_HEADER, AV1RawOBU),
  1112. CBS_UNIT_TYPE_POD(AV1_OBU_REDUNDANT_FRAME_HEADER, AV1RawOBU),
  1113. {
  1114. .nb_unit_types = 1,
  1115. .unit_type.list[0] = AV1_OBU_TILE_GROUP,
  1116. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
  1117. .content_size = sizeof(AV1RawOBU),
  1118. .type.ref = {
  1119. .nb_offsets = 2,
  1120. .offsets = { offsetof(AV1RawOBU, obu.tile_group.data),
  1121. offsetof(AV1RawOBU, obu.tile_group.tile_data.data) }
  1122. },
  1123. },
  1124. {
  1125. .nb_unit_types = 1,
  1126. .unit_type.list[0] = AV1_OBU_FRAME,
  1127. .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS,
  1128. .content_size = sizeof(AV1RawOBU),
  1129. .type.ref = {
  1130. .nb_offsets = 2,
  1131. .offsets = { offsetof(AV1RawOBU, obu.frame.tile_group.data),
  1132. offsetof(AV1RawOBU, obu.frame.tile_group.tile_data.data) }
  1133. },
  1134. },
  1135. #if CBS_AV1_OBU_TILE_LIST
  1136. CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_TILE_LIST, AV1RawOBU,
  1137. obu.tile_list.tile_data.data),
  1138. #endif
  1139. #if CBS_AV1_OBU_PADDING
  1140. CBS_UNIT_TYPE_INTERNAL_REF(AV1_OBU_PADDING, AV1RawOBU,
  1141. obu.padding.payload),
  1142. #endif
  1143. #if CBS_AV1_OBU_METADATA
  1144. CBS_UNIT_TYPE_COMPLEX(AV1_OBU_METADATA, AV1RawOBU,
  1145. &cbs_av1_free_metadata),
  1146. #endif
  1147. CBS_UNIT_TYPE_END_OF_LIST
  1148. };
  1149. #define OFFSET(x) offsetof(CodedBitstreamAV1Context, x)
  1150. static const AVOption cbs_av1_options[] = {
  1151. { "operating_point", "Set operating point to select layers to parse from a scalable bitstream",
  1152. OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AV1_MAX_OPERATING_POINTS - 1, 0 },
  1153. { "fixed_obu_size_length", "Set fixed length of the obu_size field",
  1154. OFFSET(fixed_obu_size_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, 0 },
  1155. { NULL }
  1156. };
  1157. static const AVClass cbs_av1_class = {
  1158. .class_name = "cbs_av1",
  1159. .item_name = av_default_item_name,
  1160. .option = cbs_av1_options,
  1161. .version = LIBAVUTIL_VERSION_INT,
  1162. };
  1163. const CodedBitstreamType CBS_FUNC(type_av1) = {
  1164. .codec_id = AV_CODEC_ID_AV1,
  1165. .priv_class = &cbs_av1_class,
  1166. .priv_data_size = sizeof(CodedBitstreamAV1Context),
  1167. .unit_types = cbs_av1_unit_types,
  1168. .split_fragment = &cbs_av1_split_fragment,
  1169. .read_unit = &cbs_av1_read_unit,
  1170. .write_unit = &cbs_av1_write_obu,
  1171. .assemble_fragment = &cbs_av1_assemble_fragment,
  1172. .flush = &cbs_av1_flush,
  1173. .close = &cbs_av1_close,
  1174. };