j2kenc.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. /*
  2. * JPEG2000 image encoder
  3. * Copyright (c) 2007 Kamil Nowosad
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * **********************************************************************************************************************
  22. *
  23. *
  24. *
  25. * This source code incorporates work covered by the following copyright and
  26. * permission notice:
  27. *
  28. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  29. * Copyright (c) 2002-2007, Professor Benoit Macq
  30. * Copyright (c) 2001-2003, David Janssens
  31. * Copyright (c) 2002-2003, Yannick Verschueren
  32. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  33. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  34. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  35. * Copyright (c) 2020, Gautam Ramakrishnan <gautamramk@gmail.com>
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. * 1. Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in the
  45. * documentation and/or other materials provided with the distribution.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  48. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  57. * POSSIBILITY OF SUCH DAMAGE.
  58. */
  59. /**
  60. * JPEG2000 image encoder
  61. * @file
  62. * @author Kamil Nowosad
  63. */
  64. #include <float.h>
  65. #include "avcodec.h"
  66. #include "codec_internal.h"
  67. #include "encode.h"
  68. #include "bytestream.h"
  69. #include "jpeg2000.h"
  70. #include "version.h"
  71. #include "libavutil/common.h"
  72. #include "libavutil/mem.h"
  73. #include "libavutil/pixdesc.h"
  74. #include "libavutil/opt.h"
  75. #include "libavutil/intreadwrite.h"
  76. #include "libavutil/avstring.h"
  77. #include "libavutil/thread.h"
  78. #define NMSEDEC_BITS 7
  79. #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
  80. #define WMSEDEC_SHIFT 13 ///< must be >= 13
  81. #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
  82. #define CODEC_JP2 1
  83. #define CODEC_J2K 0
  84. static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
  85. lut_nmsedec_ref0[1<<NMSEDEC_BITS],
  86. lut_nmsedec_sig [1<<NMSEDEC_BITS],
  87. lut_nmsedec_sig0[1<<NMSEDEC_BITS];
  88. static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
  89. {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
  90. {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
  91. {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
  92. {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
  93. {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
  94. {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
  95. {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
  96. { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
  97. };
  98. typedef struct {
  99. Jpeg2000Component *comp;
  100. double *layer_rates;
  101. } Jpeg2000Tile;
  102. typedef struct {
  103. AVClass *class;
  104. AVCodecContext *avctx;
  105. const AVFrame *picture;
  106. int width, height; ///< image width and height
  107. uint8_t cbps[4]; ///< bits per sample in particular components
  108. uint8_t comp_remap[4];
  109. int chroma_shift[2];
  110. uint8_t planar;
  111. int ncomponents;
  112. int tile_width, tile_height; ///< tile size
  113. int numXtiles, numYtiles;
  114. uint8_t *buf_start;
  115. uint8_t *buf;
  116. uint8_t *buf_end;
  117. int bit_index;
  118. uint64_t lambda;
  119. Jpeg2000CodingStyle codsty;
  120. Jpeg2000QuantStyle qntsty;
  121. Jpeg2000Tile *tile;
  122. int layer_rates[100];
  123. uint8_t compression_rate_enc; ///< Is compression done using compression ratio?
  124. int format;
  125. int pred;
  126. int sop;
  127. int eph;
  128. int prog;
  129. int nlayers;
  130. char *lr_str;
  131. } Jpeg2000EncoderContext;
  132. /* debug */
  133. #if 0
  134. #undef ifprintf
  135. #undef printf
  136. static void nspaces(FILE *fd, int n)
  137. {
  138. while(n--) putc(' ', fd);
  139. }
  140. static void printcomp(Jpeg2000Component *comp)
  141. {
  142. int i;
  143. for (i = 0; i < comp->y1 - comp->y0; i++)
  144. ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
  145. }
  146. static void dump(Jpeg2000EncoderContext *s, FILE *fd)
  147. {
  148. int tileno, compno, reslevelno, bandno, precno;
  149. fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
  150. "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
  151. "tiles:\n",
  152. s->width, s->height, s->tile_width, s->tile_height,
  153. s->numXtiles, s->numYtiles, s->ncomponents);
  154. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  155. Jpeg2000Tile *tile = s->tile + tileno;
  156. nspaces(fd, 2);
  157. fprintf(fd, "tile %d:\n", tileno);
  158. for(compno = 0; compno < s->ncomponents; compno++){
  159. Jpeg2000Component *comp = tile->comp + compno;
  160. nspaces(fd, 4);
  161. fprintf(fd, "component %d:\n", compno);
  162. nspaces(fd, 4);
  163. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
  164. comp->x0, comp->x1, comp->y0, comp->y1);
  165. for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
  166. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  167. nspaces(fd, 6);
  168. fprintf(fd, "reslevel %d:\n", reslevelno);
  169. nspaces(fd, 6);
  170. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
  171. reslevel->x0, reslevel->x1, reslevel->y0,
  172. reslevel->y1, reslevel->nbands);
  173. for(bandno = 0; bandno < reslevel->nbands; bandno++){
  174. Jpeg2000Band *band = reslevel->band + bandno;
  175. nspaces(fd, 8);
  176. fprintf(fd, "band %d:\n", bandno);
  177. nspaces(fd, 8);
  178. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
  179. "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
  180. band->x0, band->x1,
  181. band->y0, band->y1,
  182. band->codeblock_width, band->codeblock_height,
  183. band->cblknx, band->cblkny);
  184. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  185. Jpeg2000Prec *prec = band->prec + precno;
  186. nspaces(fd, 10);
  187. fprintf(fd, "prec %d:\n", precno);
  188. nspaces(fd, 10);
  189. fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
  190. prec->xi0, prec->xi1, prec->yi0, prec->yi1);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. #endif
  198. /* bitstream routines */
  199. /** put n times val bit */
  200. static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
  201. {
  202. while (n-- > 0){
  203. if (s->bit_index == 8)
  204. {
  205. s->bit_index = *s->buf == 0xff;
  206. *(++s->buf) = 0;
  207. }
  208. *s->buf |= val << (7 - s->bit_index++);
  209. }
  210. }
  211. /** put n least significant bits of a number num */
  212. static void put_num(Jpeg2000EncoderContext *s, int num, int n)
  213. {
  214. while(--n >= 0)
  215. put_bits(s, (num >> n) & 1, 1);
  216. }
  217. /** flush the bitstream */
  218. static void j2k_flush(Jpeg2000EncoderContext *s)
  219. {
  220. if (s->bit_index){
  221. s->bit_index = 0;
  222. s->buf++;
  223. }
  224. }
  225. /* tag tree routines */
  226. /** code the value stored in node */
  227. static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
  228. {
  229. Jpeg2000TgtNode *stack[30];
  230. int sp = -1, curval = 0;
  231. while(node->parent){
  232. stack[++sp] = node;
  233. node = node->parent;
  234. }
  235. while (1) {
  236. if (curval > node->temp_val)
  237. node->temp_val = curval;
  238. else {
  239. curval = node->temp_val;
  240. }
  241. if (node->val >= threshold) {
  242. put_bits(s, 0, threshold - curval);
  243. curval = threshold;
  244. } else {
  245. put_bits(s, 0, node->val - curval);
  246. curval = node->val;
  247. if (!node->vis) {
  248. put_bits(s, 1, 1);
  249. node->vis = 1;
  250. }
  251. }
  252. node->temp_val = curval;
  253. if (sp < 0)
  254. break;
  255. node = stack[sp--];
  256. }
  257. }
  258. /** update the value in node */
  259. static void tag_tree_update(Jpeg2000TgtNode *node)
  260. {
  261. while (node->parent){
  262. if (node->parent->val <= node->val)
  263. break;
  264. node->parent->val = node->val;
  265. node = node->parent;
  266. }
  267. }
  268. static int put_siz(Jpeg2000EncoderContext *s)
  269. {
  270. int i;
  271. if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
  272. return -1;
  273. bytestream_put_be16(&s->buf, JPEG2000_SIZ);
  274. bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
  275. bytestream_put_be16(&s->buf, 0); // Rsiz
  276. bytestream_put_be32(&s->buf, s->width); // width
  277. bytestream_put_be32(&s->buf, s->height); // height
  278. bytestream_put_be32(&s->buf, 0); // X0Siz
  279. bytestream_put_be32(&s->buf, 0); // Y0Siz
  280. bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
  281. bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
  282. bytestream_put_be32(&s->buf, 0); // XT0Siz
  283. bytestream_put_be32(&s->buf, 0); // YT0Siz
  284. bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
  285. for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
  286. bytestream_put_byte(&s->buf, s->cbps[i] - 1);
  287. bytestream_put_byte(&s->buf, (i+1&2)?1<<s->chroma_shift[0]:1);
  288. bytestream_put_byte(&s->buf, (i+1&2)?1<<s->chroma_shift[1]:1);
  289. }
  290. return 0;
  291. }
  292. static int put_cod(Jpeg2000EncoderContext *s)
  293. {
  294. Jpeg2000CodingStyle *codsty = &s->codsty;
  295. uint8_t scod = 0;
  296. if (s->buf_end - s->buf < 14)
  297. return -1;
  298. bytestream_put_be16(&s->buf, JPEG2000_COD);
  299. bytestream_put_be16(&s->buf, 12); // Lcod
  300. if (s->sop)
  301. scod |= JPEG2000_CSTY_SOP;
  302. if (s->eph)
  303. scod |= JPEG2000_CSTY_EPH;
  304. bytestream_put_byte(&s->buf, scod); // Scod
  305. // SGcod
  306. bytestream_put_byte(&s->buf, s->prog); // progression level
  307. bytestream_put_be16(&s->buf, s->nlayers); // num of layers
  308. if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
  309. bytestream_put_byte(&s->buf, 0); // unspecified
  310. }else{
  311. bytestream_put_byte(&s->buf, 0); // unspecified
  312. }
  313. // SPcod
  314. bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
  315. bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
  316. bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
  317. bytestream_put_byte(&s->buf, 0); // cblk style
  318. bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
  319. return 0;
  320. }
  321. static int put_qcd(Jpeg2000EncoderContext *s, int compno)
  322. {
  323. int i, size;
  324. Jpeg2000CodingStyle *codsty = &s->codsty;
  325. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  326. if (qntsty->quantsty == JPEG2000_QSTY_NONE)
  327. size = 4 + 3 * (codsty->nreslevels-1);
  328. else // QSTY_SE
  329. size = 5 + 6 * (codsty->nreslevels-1);
  330. if (s->buf_end - s->buf < size + 2)
  331. return -1;
  332. bytestream_put_be16(&s->buf, JPEG2000_QCD);
  333. bytestream_put_be16(&s->buf, size); // LQcd
  334. bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
  335. if (qntsty->quantsty == JPEG2000_QSTY_NONE)
  336. for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
  337. bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
  338. else // QSTY_SE
  339. for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
  340. bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
  341. return 0;
  342. }
  343. static int put_com(Jpeg2000EncoderContext *s, int compno)
  344. {
  345. int size = 4 + strlen(LIBAVCODEC_IDENT);
  346. if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
  347. return 0;
  348. if (s->buf_end - s->buf < size + 2)
  349. return -1;
  350. bytestream_put_be16(&s->buf, JPEG2000_COM);
  351. bytestream_put_be16(&s->buf, size);
  352. bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
  353. bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
  354. return 0;
  355. }
  356. static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
  357. {
  358. uint8_t *psotptr;
  359. if (s->buf_end - s->buf < 12)
  360. return NULL;
  361. bytestream_put_be16(&s->buf, JPEG2000_SOT);
  362. bytestream_put_be16(&s->buf, 10); // Lsot
  363. bytestream_put_be16(&s->buf, tileno); // Isot
  364. psotptr = s->buf;
  365. bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
  366. bytestream_put_byte(&s->buf, 0); // TPsot
  367. bytestream_put_byte(&s->buf, 1); // TNsot
  368. return psotptr;
  369. }
  370. static void compute_rates(Jpeg2000EncoderContext* s)
  371. {
  372. int i, j;
  373. int layno, compno;
  374. for (i = 0; i < s->numYtiles; i++) {
  375. for (j = 0; j < s->numXtiles; j++) {
  376. Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
  377. for (compno = 0; compno < s->ncomponents; compno++) {
  378. int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
  379. int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
  380. int scale = ((compno+1&2)?1 << s->chroma_shift[0]:1) * ((compno+1&2)?1 << s->chroma_shift[1]:1);
  381. for (layno = 0; layno < s->nlayers; layno++) {
  382. if (s->layer_rates[layno] > 0) {
  383. tile->layer_rates[layno] += (double)(tilew * tileh) * s->ncomponents * s->cbps[compno] /
  384. (double)(s->layer_rates[layno] * 8 * scale);
  385. } else {
  386. tile->layer_rates[layno] = 0.0;
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. /**
  394. * compute the sizes of tiles, resolution levels, bands, etc.
  395. * allocate memory for them
  396. * divide the input image into tile-components
  397. */
  398. static int init_tiles(Jpeg2000EncoderContext *s)
  399. {
  400. int tileno, tilex, tiley, compno;
  401. Jpeg2000CodingStyle *codsty = &s->codsty;
  402. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  403. s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
  404. s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
  405. s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
  406. if (!s->tile)
  407. return AVERROR(ENOMEM);
  408. for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
  409. for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
  410. Jpeg2000Tile *tile = s->tile + tileno;
  411. tile->comp = av_calloc(s->ncomponents, sizeof(*tile->comp));
  412. if (!tile->comp)
  413. return AVERROR(ENOMEM);
  414. tile->layer_rates = av_calloc(s->nlayers, sizeof(*tile->layer_rates));
  415. if (!tile->layer_rates)
  416. return AVERROR(ENOMEM);
  417. for (compno = 0; compno < s->ncomponents; compno++){
  418. Jpeg2000Component *comp = tile->comp + compno;
  419. int ret, i, j;
  420. comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
  421. comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
  422. comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
  423. comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
  424. if (compno + 1 & 2)
  425. for (i = 0; i < 2; i++)
  426. for (j = 0; j < 2; j++)
  427. comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
  428. if ((ret = ff_jpeg2000_init_component(comp,
  429. codsty,
  430. qntsty,
  431. s->cbps[compno],
  432. (compno+1&2)?1<<s->chroma_shift[0]:1,
  433. (compno+1&2)?1<<s->chroma_shift[1]:1,
  434. s->avctx
  435. )) < 0)
  436. return ret;
  437. }
  438. }
  439. compute_rates(s);
  440. return 0;
  441. }
  442. #define COPY_FRAME(D, PIXEL) \
  443. static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
  444. { \
  445. int tileno, compno, i, y, x; \
  446. const PIXEL *line; \
  447. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
  448. Jpeg2000Tile *tile = s->tile + tileno; \
  449. if (s->planar){ \
  450. for (compno = 0; compno < s->ncomponents; compno++){ \
  451. int icompno = s->comp_remap[compno]; \
  452. Jpeg2000Component *comp = tile->comp + compno; \
  453. int *dst = comp->i_data; \
  454. int cbps = s->cbps[compno]; \
  455. line = (const PIXEL*)s->picture->data[icompno] \
  456. + comp->coord[1][0] * (s->picture->linesize[icompno] / sizeof(PIXEL)) \
  457. + comp->coord[0][0]; \
  458. for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
  459. const PIXEL *ptr = line; \
  460. for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
  461. *dst++ = *ptr++ - (1 << (cbps - 1)); \
  462. line += s->picture->linesize[icompno] / sizeof(PIXEL); \
  463. } \
  464. } \
  465. } else{ \
  466. line = (const PIXEL*)(s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]) \
  467. + tile->comp[0].coord[0][0] * s->ncomponents; \
  468. \
  469. i = 0; \
  470. for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
  471. const PIXEL *ptr = line; \
  472. for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
  473. for (compno = 0; compno < s->ncomponents; compno++){ \
  474. int cbps = s->cbps[compno]; \
  475. tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
  476. } \
  477. } \
  478. line += s->picture->linesize[0] / sizeof(PIXEL); \
  479. } \
  480. } \
  481. } \
  482. }
  483. COPY_FRAME(8, uint8_t)
  484. COPY_FRAME(16, uint16_t)
  485. static void init_quantization(Jpeg2000EncoderContext *s)
  486. {
  487. int compno, reslevelno, bandno;
  488. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  489. Jpeg2000CodingStyle *codsty = &s->codsty;
  490. for (compno = 0; compno < s->ncomponents; compno++){
  491. int gbandno = 0;
  492. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  493. int nbands, lev = codsty->nreslevels - reslevelno - 1;
  494. nbands = reslevelno ? 3 : 1;
  495. for (bandno = 0; bandno < nbands; bandno++, gbandno++){
  496. int expn, mant = 0;
  497. if (codsty->transform == FF_DWT97_INT){
  498. int bandpos = bandno + (reslevelno>0),
  499. ss = 81920000 / dwt_norms[0][bandpos][lev],
  500. log = av_log2(ss);
  501. mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
  502. expn = s->cbps[compno] - log + 13;
  503. } else
  504. expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
  505. qntsty->expn[gbandno] = expn;
  506. qntsty->mant[gbandno] = mant;
  507. }
  508. }
  509. }
  510. }
  511. static void init_luts(void)
  512. {
  513. int i, a,
  514. mask = ~((1<<NMSEDEC_FRACBITS)-1);
  515. for (i = 0; i < (1 << NMSEDEC_BITS); i++){
  516. lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
  517. lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
  518. a = (i >> (NMSEDEC_BITS-2)&2) + 1;
  519. lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
  520. (1 << 13) - (a * a << 11), 0);
  521. lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
  522. << 1, 0);
  523. }
  524. ff_jpeg2000_init_tier1_luts();
  525. }
  526. /* tier-1 routines */
  527. static int getnmsedec_sig(int x, int bpno)
  528. {
  529. if (bpno > NMSEDEC_FRACBITS)
  530. return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  531. return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
  532. }
  533. static int getnmsedec_ref(int x, int bpno)
  534. {
  535. if (bpno > NMSEDEC_FRACBITS)
  536. return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  537. return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
  538. }
  539. static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  540. {
  541. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  542. for (y0 = 0; y0 < height; y0 += 4)
  543. for (x = 0; x < width; x++)
  544. for (y = y0; y < height && y < y0+4; y++){
  545. if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
  546. int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
  547. bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
  548. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
  549. if (bit){
  550. int xorbit;
  551. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
  552. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
  553. *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  554. ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
  555. }
  556. t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
  557. }
  558. }
  559. }
  560. static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
  561. {
  562. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  563. for (y0 = 0; y0 < height; y0 += 4)
  564. for (x = 0; x < width; x++)
  565. for (y = y0; y < height && y < y0+4; y++)
  566. if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
  567. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
  568. *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  569. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
  570. t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
  571. }
  572. }
  573. static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  574. {
  575. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  576. for (y0 = 0; y0 < height; y0 += 4)
  577. for (x = 0; x < width; x++){
  578. if (y0 + 3 < height && !(
  579. (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  580. (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  581. (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  582. (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
  583. {
  584. // aggregation mode
  585. int rlen;
  586. for (rlen = 0; rlen < 4; rlen++)
  587. if (t1->data[(y0+rlen) * t1->stride + x] & mask)
  588. break;
  589. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
  590. if (rlen == 4)
  591. continue;
  592. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
  593. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
  594. for (y = y0 + rlen; y < y0 + 4; y++){
  595. if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
  596. int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
  597. if (y > y0 + rlen)
  598. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
  599. if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
  600. int xorbit;
  601. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
  602. *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  603. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
  604. ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
  605. }
  606. }
  607. t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
  608. }
  609. } else{
  610. for (y = y0; y < y0 + 4 && y < height; y++){
  611. if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
  612. int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
  613. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
  614. if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
  615. int xorbit;
  616. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
  617. *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  618. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
  619. ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
  620. }
  621. }
  622. t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
  623. }
  624. }
  625. }
  626. }
  627. static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
  628. int width, int height, int bandpos, int lev)
  629. {
  630. int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
  631. int64_t wmsedec = 0;
  632. memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
  633. for (y = 0; y < height; y++){
  634. for (x = 0; x < width; x++){
  635. if (t1->data[(y) * t1->stride + x] < 0){
  636. t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
  637. t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
  638. }
  639. max = FFMAX(max, t1->data[(y) * t1->stride + x]);
  640. }
  641. }
  642. if (max == 0){
  643. cblk->nonzerobits = 0;
  644. } else{
  645. cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
  646. }
  647. bpno = cblk->nonzerobits - 1;
  648. cblk->data[0] = 0;
  649. ff_mqc_initenc(&t1->mqc, cblk->data + 1);
  650. for (passno = 0; bpno >= 0; passno++){
  651. nmsedec=0;
  652. switch(pass_t){
  653. case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
  654. break;
  655. case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
  656. break;
  657. case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
  658. break;
  659. }
  660. cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
  661. cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
  662. wmsedec += (int64_t)nmsedec << (2*bpno);
  663. cblk->passes[passno].disto = wmsedec;
  664. if (++pass_t == 3){
  665. pass_t = 0;
  666. bpno--;
  667. }
  668. }
  669. cblk->npasses = passno;
  670. cblk->ninclpasses = passno;
  671. if (passno) {
  672. cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
  673. cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
  674. }
  675. }
  676. /* tier-2 routines: */
  677. static void putnumpasses(Jpeg2000EncoderContext *s, int n)
  678. {
  679. if (n == 1)
  680. put_num(s, 0, 1);
  681. else if (n == 2)
  682. put_num(s, 2, 2);
  683. else if (n <= 5)
  684. put_num(s, 0xc | (n-3), 4);
  685. else if (n <= 36)
  686. put_num(s, 0x1e0 | (n-6), 9);
  687. else
  688. put_num(s, 0xff80 | (n-37), 16);
  689. }
  690. static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
  691. int precno, const uint8_t *expn, int numgbits, int packetno,
  692. int nlayers)
  693. {
  694. int bandno, empty = 1;
  695. int i;
  696. // init bitstream
  697. *s->buf = 0;
  698. s->bit_index = 0;
  699. if (s->sop) {
  700. bytestream_put_be16(&s->buf, JPEG2000_SOP);
  701. bytestream_put_be16(&s->buf, 4);
  702. bytestream_put_be16(&s->buf, packetno);
  703. }
  704. // header
  705. if (!layno) {
  706. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  707. Jpeg2000Band *band = rlevel->band + bandno;
  708. if (band->coord[0][0] < band->coord[0][1]
  709. && band->coord[1][0] < band->coord[1][1]) {
  710. Jpeg2000Prec *prec = band->prec + precno;
  711. int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  712. int pos;
  713. ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
  714. ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
  715. for (pos = 0; pos < nb_cblks; pos++) {
  716. Jpeg2000Cblk *cblk = &prec->cblk[pos];
  717. prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
  718. cblk->incl = 0;
  719. cblk->lblock = 3;
  720. tag_tree_update(prec->zerobits + pos);
  721. for (i = 0; i < nlayers; i++) {
  722. if (cblk->layers[i].npasses > 0) {
  723. prec->cblkincl[pos].val = i;
  724. break;
  725. }
  726. }
  727. if (i == nlayers)
  728. prec->cblkincl[pos].val = i;
  729. tag_tree_update(prec->cblkincl + pos);
  730. }
  731. }
  732. }
  733. }
  734. // is the packet empty?
  735. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  736. Jpeg2000Band *band = rlevel->band + bandno;
  737. if (band->coord[0][0] < band->coord[0][1]
  738. && band->coord[1][0] < band->coord[1][1]) {
  739. Jpeg2000Prec *prec = band->prec + precno;
  740. int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
  741. int pos;
  742. for (pos = 0; pos < nb_cblks; pos++) {
  743. Jpeg2000Cblk *cblk = &prec->cblk[pos];
  744. if (cblk->layers[layno].npasses) {
  745. empty = 0;
  746. break;
  747. }
  748. }
  749. if (!empty)
  750. break;
  751. }
  752. }
  753. put_bits(s, !empty, 1);
  754. if (empty){
  755. j2k_flush(s);
  756. if (s->eph)
  757. bytestream_put_be16(&s->buf, JPEG2000_EPH);
  758. return 0;
  759. }
  760. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  761. Jpeg2000Band *band = rlevel->band + bandno;
  762. Jpeg2000Prec *prec = band->prec + precno;
  763. int yi, xi, pos;
  764. int cblknw = prec->nb_codeblocks_width;
  765. if (band->coord[0][0] == band->coord[0][1]
  766. || band->coord[1][0] == band->coord[1][1])
  767. continue;
  768. for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
  769. for (xi = 0; xi < cblknw; xi++, pos++){
  770. int llen = 0, length;
  771. Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
  772. if (s->buf_end - s->buf < 20) // approximately
  773. return -1;
  774. // inclusion information
  775. if (!cblk->incl)
  776. tag_tree_code(s, prec->cblkincl + pos, layno + 1);
  777. else {
  778. put_bits(s, cblk->layers[layno].npasses > 0, 1);
  779. }
  780. if (!cblk->layers[layno].npasses)
  781. continue;
  782. // zerobits information
  783. if (!cblk->incl) {
  784. tag_tree_code(s, prec->zerobits + pos, 100);
  785. cblk->incl = 1;
  786. }
  787. // number of passes
  788. putnumpasses(s, cblk->layers[layno].npasses);
  789. length = cblk->layers[layno].data_len;
  790. if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
  791. length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
  792. }
  793. if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
  794. llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
  795. }
  796. // length of code block
  797. cblk->lblock += llen;
  798. put_bits(s, 1, llen);
  799. put_bits(s, 0, 1);
  800. put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
  801. }
  802. }
  803. }
  804. j2k_flush(s);
  805. if (s->eph) {
  806. bytestream_put_be16(&s->buf, JPEG2000_EPH);
  807. }
  808. for (bandno = 0; bandno < rlevel->nbands; bandno++) {
  809. Jpeg2000Band *band = rlevel->band + bandno;
  810. Jpeg2000Prec *prec = band->prec + precno;
  811. int yi, cblknw = prec->nb_codeblocks_width;
  812. for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
  813. int xi;
  814. for (xi = 0; xi < cblknw; xi++){
  815. Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
  816. if (cblk->layers[layno].npasses) {
  817. if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
  818. return -1;
  819. bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
  820. if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
  821. bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
  822. cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
  823. }
  824. }
  825. }
  826. }
  827. }
  828. return 0;
  829. }
  830. static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
  831. {
  832. int compno, reslevelno, layno, ret;
  833. Jpeg2000CodingStyle *codsty = &s->codsty;
  834. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  835. int packetno = 0;
  836. int step_x, step_y;
  837. int x, y;
  838. int tile_coord[2][2];
  839. int col = tileno % s->numXtiles;
  840. int row = tileno / s->numXtiles;
  841. tile_coord[0][0] = col * s->tile_width;
  842. tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
  843. tile_coord[1][0] = row * s->tile_height;
  844. tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
  845. av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
  846. // lay-rlevel-comp-pos progression
  847. switch (s->prog) {
  848. case JPEG2000_PGOD_LRCP:
  849. for (layno = 0; layno < nlayers; layno++) {
  850. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  851. for (compno = 0; compno < s->ncomponents; compno++){
  852. int precno;
  853. Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
  854. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  855. if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  856. qntsty->nguardbits, packetno++, nlayers)) < 0)
  857. return ret;
  858. }
  859. }
  860. }
  861. }
  862. break;
  863. case JPEG2000_PGOD_RLCP:
  864. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  865. for (layno = 0; layno < nlayers; layno++) {
  866. for (compno = 0; compno < s->ncomponents; compno++){
  867. int precno;
  868. Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
  869. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  870. if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  871. qntsty->nguardbits, packetno++, nlayers)) < 0)
  872. return ret;
  873. }
  874. }
  875. }
  876. }
  877. break;
  878. case JPEG2000_PGOD_RPCL:
  879. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  880. int precno;
  881. step_x = 30;
  882. step_y = 30;
  883. for (compno = 0; compno < s->ncomponents; compno++) {
  884. Jpeg2000Component *comp = tile->comp + compno;
  885. if (reslevelno < codsty->nreslevels) {
  886. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  887. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  888. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  889. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  890. }
  891. }
  892. step_x = 1<<step_x;
  893. step_y = 1<<step_y;
  894. for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
  895. for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
  896. for (compno = 0; compno < s->ncomponents; compno++) {
  897. Jpeg2000Component *comp = tile->comp + compno;
  898. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  899. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  900. int log_subsampling[2] = { (compno+1&2)?s->chroma_shift[0]:0, (compno+1&2)?s->chroma_shift[1]:0};
  901. unsigned prcx, prcy;
  902. int trx0, try0;
  903. trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
  904. try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
  905. if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
  906. (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
  907. continue;
  908. if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
  909. (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
  910. continue;
  911. // check if a precinct exists
  912. prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
  913. prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
  914. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
  915. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
  916. precno = prcx + reslevel->num_precincts_x * prcy;
  917. if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
  918. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  919. prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
  920. continue;
  921. }
  922. for (layno = 0; layno < nlayers; layno++) {
  923. if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  924. qntsty->nguardbits, packetno++, nlayers)) < 0)
  925. return ret;
  926. }
  927. }
  928. }
  929. }
  930. }
  931. break;
  932. case JPEG2000_PGOD_PCRL:
  933. step_x = 32;
  934. step_y = 32;
  935. for (compno = 0; compno < s->ncomponents; compno++) {
  936. Jpeg2000Component *comp = tile->comp + compno;
  937. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  938. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  939. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  940. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  941. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  942. }
  943. }
  944. if (step_x >= 31 || step_y >= 31){
  945. avpriv_request_sample(s->avctx, "PCRL with large step");
  946. return AVERROR_PATCHWELCOME;
  947. }
  948. step_x = 1<<step_x;
  949. step_y = 1<<step_y;
  950. for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
  951. for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
  952. for (compno = 0; compno < s->ncomponents; compno++) {
  953. Jpeg2000Component *comp = tile->comp + compno;
  954. int log_subsampling[2] = { (compno+1&2)?s->chroma_shift[0]:0, (compno+1&2)?s->chroma_shift[1]:0};
  955. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  956. unsigned prcx, prcy;
  957. int precno;
  958. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  959. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  960. int trx0, try0;
  961. trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
  962. try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
  963. if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
  964. (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
  965. continue;
  966. if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
  967. (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
  968. continue;
  969. // check if a precinct exists
  970. prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
  971. prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
  972. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
  973. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
  974. precno = prcx + reslevel->num_precincts_x * prcy;
  975. if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
  976. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  977. prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
  978. continue;
  979. }
  980. for (layno = 0; layno < nlayers; layno++) {
  981. if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  982. qntsty->nguardbits, packetno++, nlayers)) < 0)
  983. return ret;
  984. }
  985. }
  986. }
  987. }
  988. }
  989. break;
  990. case JPEG2000_PGOD_CPRL:
  991. for (compno = 0; compno < s->ncomponents; compno++) {
  992. Jpeg2000Component *comp = tile->comp + compno;
  993. int log_subsampling[2] = { (compno+1&2)?s->chroma_shift[0]:0, (compno+1&2)?s->chroma_shift[1]:0};
  994. step_x = 32;
  995. step_y = 32;
  996. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  997. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  998. Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
  999. step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
  1000. step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
  1001. }
  1002. if (step_x >= 31 || step_y >= 31){
  1003. avpriv_request_sample(s->avctx, "CPRL with large step");
  1004. return AVERROR_PATCHWELCOME;
  1005. }
  1006. step_x = 1<<step_x;
  1007. step_y = 1<<step_y;
  1008. for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
  1009. for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
  1010. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
  1011. unsigned prcx, prcy;
  1012. int precno;
  1013. int trx0, try0;
  1014. uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
  1015. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  1016. trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
  1017. try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
  1018. if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
  1019. (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
  1020. continue;
  1021. if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
  1022. (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
  1023. continue;
  1024. // check if a precinct exists
  1025. prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
  1026. prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
  1027. prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
  1028. prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
  1029. precno = prcx + reslevel->num_precincts_x * prcy;
  1030. if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
  1031. av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
  1032. prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
  1033. continue;
  1034. }
  1035. for (layno = 0; layno < nlayers; layno++) {
  1036. if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  1037. qntsty->nguardbits, packetno++, nlayers)) < 0)
  1038. return ret;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
  1046. return 0;
  1047. }
  1048. static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
  1049. {
  1050. int compno, resno, bandno, precno, cblkno;
  1051. int passno;
  1052. for (compno = 0; compno < s->ncomponents; compno++) {
  1053. Jpeg2000Component *comp = &tile->comp[compno];
  1054. for (resno = 0; resno < s->codsty.nreslevels; resno++) {
  1055. Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
  1056. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  1057. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  1058. Jpeg2000Band *band = reslevel->band + bandno;
  1059. Jpeg2000Prec *prec = band->prec + precno;
  1060. for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
  1061. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  1062. Jpeg2000Layer *layer = &cblk->layers[layno];
  1063. int n;
  1064. if (layno == 0) {
  1065. cblk->ninclpasses = 0;
  1066. }
  1067. n = cblk->ninclpasses;
  1068. if (thresh < 0) {
  1069. n = cblk->npasses;
  1070. } else {
  1071. for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
  1072. int32_t dr;
  1073. double dd;
  1074. Jpeg2000Pass *pass = &cblk->passes[passno];
  1075. if (n == 0) {
  1076. dr = pass->rate;
  1077. dd = pass->disto;
  1078. } else {
  1079. dr = pass->rate - cblk->passes[n - 1].rate;
  1080. dd = pass->disto - cblk->passes[n-1].disto;
  1081. }
  1082. if (!dr) {
  1083. if (dd != 0.0) {
  1084. n = passno + 1;
  1085. }
  1086. continue;
  1087. }
  1088. if (thresh - (dd / dr) < DBL_EPSILON)
  1089. n = passno + 1;
  1090. }
  1091. }
  1092. layer->npasses = n - cblk->ninclpasses;
  1093. layer->cum_passes = n;
  1094. if (layer->npasses == 0) {
  1095. layer->disto = 0;
  1096. layer->data_len = 0;
  1097. continue;
  1098. }
  1099. if (cblk->ninclpasses == 0) {
  1100. layer->data_len = cblk->passes[n - 1].rate;
  1101. layer->data_start = cblk->data;
  1102. layer->disto = cblk->passes[n - 1].disto;
  1103. } else {
  1104. layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
  1105. layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
  1106. layer->disto = cblk->passes[n - 1].disto -
  1107. cblk->passes[cblk->ninclpasses - 1].disto;
  1108. }
  1109. if (final) {
  1110. cblk->ninclpasses = n;
  1111. }
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }
  1118. static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
  1119. {
  1120. int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
  1121. int i;
  1122. double min = DBL_MAX;
  1123. double max = 0;
  1124. double thresh;
  1125. Jpeg2000CodingStyle *codsty = &s->codsty;
  1126. for (compno = 0; compno < s->ncomponents; compno++){
  1127. Jpeg2000Component *comp = tile->comp + compno;
  1128. for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
  1129. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  1130. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  1131. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  1132. Jpeg2000Band *band = reslevel->band + bandno;
  1133. Jpeg2000Prec *prec = band->prec + precno;
  1134. for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
  1135. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  1136. for (passno = 0; passno < cblk->npasses; passno++) {
  1137. Jpeg2000Pass *pass = &cblk->passes[passno];
  1138. int dr;
  1139. double dd, drslope;
  1140. if (passno == 0) {
  1141. dr = (int32_t)pass->rate;
  1142. dd = pass->disto;
  1143. } else {
  1144. dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
  1145. dd = pass->disto - cblk->passes[passno - 1].disto;
  1146. }
  1147. if (dr <= 0)
  1148. continue;
  1149. drslope = dd / dr;
  1150. if (drslope < min)
  1151. min = drslope;
  1152. if (drslope > max)
  1153. max = drslope;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. for (layno = 0; layno < s->nlayers; layno++) {
  1161. double lo = min;
  1162. double hi = max;
  1163. double stable_thresh = 0.0;
  1164. double good_thresh = 0.0;
  1165. if (!s->layer_rates[layno]) {
  1166. good_thresh = -1.0;
  1167. } else {
  1168. for (i = 0; i < 128; i++) {
  1169. uint8_t *stream_pos = s->buf;
  1170. int ret;
  1171. thresh = (lo + hi) / 2;
  1172. makelayer(s, layno, thresh, tile, 0);
  1173. ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
  1174. memset(stream_pos, 0, s->buf - stream_pos);
  1175. if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
  1176. lo = thresh;
  1177. s->buf = stream_pos;
  1178. continue;
  1179. }
  1180. hi = thresh;
  1181. stable_thresh = thresh;
  1182. s->buf = stream_pos;
  1183. }
  1184. }
  1185. if (good_thresh >= 0.0)
  1186. good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
  1187. makelayer(s, layno, good_thresh, tile, 1);
  1188. }
  1189. }
  1190. static int getcut(Jpeg2000Cblk *cblk, uint64_t lambda)
  1191. {
  1192. int passno, res = 0;
  1193. for (passno = 0; passno < cblk->npasses; passno++){
  1194. int dr;
  1195. int64_t dd;
  1196. dr = cblk->passes[passno].rate
  1197. - (res ? cblk->passes[res-1].rate : 0);
  1198. dd = cblk->passes[passno].disto
  1199. - (res ? cblk->passes[res-1].disto : 0);
  1200. if (dd >= dr * lambda)
  1201. res = passno+1;
  1202. }
  1203. return res;
  1204. }
  1205. static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
  1206. {
  1207. int precno, compno, reslevelno, bandno, cblkno, lev;
  1208. Jpeg2000CodingStyle *codsty = &s->codsty;
  1209. for (compno = 0; compno < s->ncomponents; compno++){
  1210. Jpeg2000Component *comp = tile->comp + compno;
  1211. for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
  1212. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  1213. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  1214. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  1215. int bandpos = bandno + (reslevelno > 0);
  1216. Jpeg2000Band *band = reslevel->band + bandno;
  1217. Jpeg2000Prec *prec = band->prec + precno;
  1218. int64_t dwt_norm = dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15;
  1219. int64_t lambda_prime = av_rescale(s->lambda, 1 << WMSEDEC_SHIFT, dwt_norm * dwt_norm);
  1220. for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
  1221. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  1222. cblk->ninclpasses = getcut(cblk, lambda_prime);
  1223. cblk->layers[0].data_start = cblk->data;
  1224. cblk->layers[0].cum_passes = cblk->ninclpasses;
  1225. cblk->layers[0].npasses = cblk->ninclpasses;
  1226. if (cblk->ninclpasses)
  1227. cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
  1228. }
  1229. }
  1230. }
  1231. }
  1232. }
  1233. }
  1234. static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
  1235. {
  1236. int compno, reslevelno, bandno, ret;
  1237. Jpeg2000T1Context t1;
  1238. Jpeg2000CodingStyle *codsty = &s->codsty;
  1239. for (compno = 0; compno < s->ncomponents; compno++){
  1240. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1241. t1.stride = (1<<codsty->log2_cblk_width) + 2;
  1242. av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
  1243. if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
  1244. return ret;
  1245. av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
  1246. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  1247. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  1248. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  1249. Jpeg2000Band *band = reslevel->band + bandno;
  1250. Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
  1251. int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
  1252. yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  1253. y0 = yy0;
  1254. yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
  1255. band->coord[1][1]) - band->coord[1][0] + yy0;
  1256. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  1257. continue;
  1258. bandpos = bandno + (reslevelno > 0);
  1259. for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
  1260. if (reslevelno == 0 || bandno == 1)
  1261. xx0 = 0;
  1262. else
  1263. xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  1264. x0 = xx0;
  1265. xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
  1266. band->coord[0][1]) - band->coord[0][0] + xx0;
  1267. for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
  1268. int y, x;
  1269. if (codsty->transform == FF_DWT53){
  1270. for (y = yy0; y < yy1; y++){
  1271. int *ptr = t1.data + (y-yy0)*t1.stride;
  1272. for (x = xx0; x < xx1; x++){
  1273. *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
  1274. }
  1275. }
  1276. } else{
  1277. for (y = yy0; y < yy1; y++){
  1278. int *ptr = t1.data + (y-yy0)*t1.stride;
  1279. for (x = xx0; x < xx1; x++){
  1280. *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
  1281. *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
  1282. ptr++;
  1283. }
  1284. }
  1285. }
  1286. if (!prec->cblk[cblkno].data)
  1287. prec->cblk[cblkno].data = av_malloc(1 + 8192);
  1288. if (!prec->cblk[cblkno].passes)
  1289. prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
  1290. if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
  1291. return AVERROR(ENOMEM);
  1292. encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
  1293. bandpos, codsty->nreslevels - reslevelno - 1);
  1294. xx0 = xx1;
  1295. xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
  1296. }
  1297. yy0 = yy1;
  1298. yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
  1299. }
  1300. }
  1301. }
  1302. av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
  1303. }
  1304. av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
  1305. if (s->compression_rate_enc)
  1306. makelayers(s, tile);
  1307. else
  1308. truncpasses(s, tile);
  1309. if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
  1310. return ret;
  1311. av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
  1312. return 0;
  1313. }
  1314. static void cleanup(Jpeg2000EncoderContext *s)
  1315. {
  1316. int tileno, compno;
  1317. Jpeg2000CodingStyle *codsty = &s->codsty;
  1318. if (!s->tile)
  1319. return;
  1320. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  1321. if (s->tile[tileno].comp) {
  1322. for (compno = 0; compno < s->ncomponents; compno++){
  1323. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  1324. ff_jpeg2000_cleanup(comp, codsty);
  1325. }
  1326. av_freep(&s->tile[tileno].comp);
  1327. }
  1328. av_freep(&s->tile[tileno].layer_rates);
  1329. }
  1330. av_freep(&s->tile);
  1331. }
  1332. static void reinit(Jpeg2000EncoderContext *s)
  1333. {
  1334. int tileno, compno;
  1335. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  1336. Jpeg2000Tile *tile = s->tile + tileno;
  1337. for (compno = 0; compno < s->ncomponents; compno++)
  1338. ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
  1339. }
  1340. }
  1341. static void update_size(uint8_t *size, const uint8_t *end)
  1342. {
  1343. AV_WB32(size, end-size);
  1344. }
  1345. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1346. const AVFrame *pict, int *got_packet)
  1347. {
  1348. int tileno, ret;
  1349. Jpeg2000EncoderContext *s = avctx->priv_data;
  1350. uint8_t *chunkstart, *jp2cstart, *jp2hstart;
  1351. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1352. if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
  1353. return ret;
  1354. // init:
  1355. s->buf = s->buf_start = pkt->data;
  1356. s->buf_end = pkt->data + pkt->size;
  1357. s->picture = pict;
  1358. s->lambda = s->picture->quality * LAMBDA_SCALE;
  1359. if (s->cbps[0] > 8)
  1360. copy_frame_16(s);
  1361. else
  1362. copy_frame_8(s);
  1363. reinit(s);
  1364. if (s->format == CODEC_JP2) {
  1365. av_assert0(s->buf == pkt->data);
  1366. bytestream_put_be32(&s->buf, 0x0000000C);
  1367. bytestream_put_be32(&s->buf, 0x6A502020);
  1368. bytestream_put_be32(&s->buf, 0x0D0A870A);
  1369. chunkstart = s->buf;
  1370. bytestream_put_be32(&s->buf, 0);
  1371. bytestream_put_buffer(&s->buf, "ftyp", 4);
  1372. bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
  1373. bytestream_put_be32(&s->buf, 0);
  1374. bytestream_put_buffer(&s->buf, "jp2\040", 4);
  1375. update_size(chunkstart, s->buf);
  1376. jp2hstart = s->buf;
  1377. bytestream_put_be32(&s->buf, 0);
  1378. bytestream_put_buffer(&s->buf, "jp2h", 4);
  1379. chunkstart = s->buf;
  1380. bytestream_put_be32(&s->buf, 0);
  1381. bytestream_put_buffer(&s->buf, "ihdr", 4);
  1382. bytestream_put_be32(&s->buf, avctx->height);
  1383. bytestream_put_be32(&s->buf, avctx->width);
  1384. bytestream_put_be16(&s->buf, s->ncomponents);
  1385. bytestream_put_byte(&s->buf, s->cbps[0]);
  1386. bytestream_put_byte(&s->buf, 7);
  1387. bytestream_put_byte(&s->buf, 0);
  1388. bytestream_put_byte(&s->buf, 0);
  1389. update_size(chunkstart, s->buf);
  1390. chunkstart = s->buf;
  1391. bytestream_put_be32(&s->buf, 0);
  1392. bytestream_put_buffer(&s->buf, "colr", 4);
  1393. bytestream_put_byte(&s->buf, 1);
  1394. bytestream_put_byte(&s->buf, 0);
  1395. bytestream_put_byte(&s->buf, 0);
  1396. if ((desc->flags & AV_PIX_FMT_FLAG_RGB) || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  1397. bytestream_put_be32(&s->buf, 16);
  1398. } else if (s->ncomponents == 1) {
  1399. bytestream_put_be32(&s->buf, 17);
  1400. } else {
  1401. bytestream_put_be32(&s->buf, 18);
  1402. }
  1403. update_size(chunkstart, s->buf);
  1404. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  1405. int i;
  1406. const uint8_t *palette = pict->data[1];
  1407. chunkstart = s->buf;
  1408. bytestream_put_be32(&s->buf, 0);
  1409. bytestream_put_buffer(&s->buf, "pclr", 4);
  1410. bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
  1411. bytestream_put_byte(&s->buf, 3); // colour channels
  1412. bytestream_put_be24(&s->buf, 0x070707); //colour depths
  1413. for (i = 0; i < AVPALETTE_COUNT; i++) {
  1414. bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
  1415. palette += 4;
  1416. }
  1417. update_size(chunkstart, s->buf);
  1418. chunkstart = s->buf;
  1419. bytestream_put_be32(&s->buf, 0);
  1420. bytestream_put_buffer(&s->buf, "cmap", 4);
  1421. for (i = 0; i < 3; i++) {
  1422. bytestream_put_be16(&s->buf, 0); // component
  1423. bytestream_put_byte(&s->buf, 1); // palette mapping
  1424. bytestream_put_byte(&s->buf, i); // index
  1425. }
  1426. update_size(chunkstart, s->buf);
  1427. }
  1428. update_size(jp2hstart, s->buf);
  1429. jp2cstart = s->buf;
  1430. bytestream_put_be32(&s->buf, 0);
  1431. bytestream_put_buffer(&s->buf, "jp2c", 4);
  1432. }
  1433. if (s->buf_end - s->buf < 2)
  1434. return -1;
  1435. bytestream_put_be16(&s->buf, JPEG2000_SOC);
  1436. if ((ret = put_siz(s)) < 0)
  1437. return ret;
  1438. if ((ret = put_cod(s)) < 0)
  1439. return ret;
  1440. if ((ret = put_qcd(s, 0)) < 0)
  1441. return ret;
  1442. if ((ret = put_com(s, 0)) < 0)
  1443. return ret;
  1444. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  1445. uint8_t *psotptr;
  1446. if (!(psotptr = put_sot(s, tileno)))
  1447. return -1;
  1448. if (s->buf_end - s->buf < 2)
  1449. return -1;
  1450. bytestream_put_be16(&s->buf, JPEG2000_SOD);
  1451. if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
  1452. return ret;
  1453. bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
  1454. }
  1455. if (s->buf_end - s->buf < 2)
  1456. return -1;
  1457. bytestream_put_be16(&s->buf, JPEG2000_EOC);
  1458. if (s->format == CODEC_JP2)
  1459. update_size(jp2cstart, s->buf);
  1460. av_log(s->avctx, AV_LOG_DEBUG, "end\n");
  1461. pkt->size = s->buf - s->buf_start;
  1462. *got_packet = 1;
  1463. return 0;
  1464. }
  1465. static int parse_layer_rates(Jpeg2000EncoderContext *s)
  1466. {
  1467. int i;
  1468. char *token;
  1469. char *saveptr = NULL;
  1470. int rate;
  1471. int nlayers = 0;
  1472. if (!s->lr_str) {
  1473. s->nlayers = 1;
  1474. s->layer_rates[0] = 0;
  1475. s->compression_rate_enc = 0;
  1476. return 0;
  1477. }
  1478. token = av_strtok(s->lr_str, ",", &saveptr);
  1479. if (token && (rate = strtol(token, NULL, 10))) {
  1480. s->layer_rates[0] = rate <= 1 ? 0:rate;
  1481. nlayers++;
  1482. } else {
  1483. return AVERROR_INVALIDDATA;
  1484. }
  1485. while (1) {
  1486. token = av_strtok(NULL, ",", &saveptr);
  1487. if (!token)
  1488. break;
  1489. if (rate = strtol(token, NULL, 10)) {
  1490. if (nlayers >= 100) {
  1491. return AVERROR_INVALIDDATA;
  1492. }
  1493. s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
  1494. nlayers++;
  1495. } else {
  1496. return AVERROR_INVALIDDATA;
  1497. }
  1498. }
  1499. for (i = 1; i < nlayers; i++) {
  1500. if (s->layer_rates[i] >= s->layer_rates[i-1]) {
  1501. return AVERROR_INVALIDDATA;
  1502. }
  1503. }
  1504. s->nlayers = nlayers;
  1505. s->compression_rate_enc = 1;
  1506. return 0;
  1507. }
  1508. static av_cold int j2kenc_init(AVCodecContext *avctx)
  1509. {
  1510. static AVOnce init_static_once = AV_ONCE_INIT;
  1511. int i, ret;
  1512. Jpeg2000EncoderContext *s = avctx->priv_data;
  1513. Jpeg2000CodingStyle *codsty = &s->codsty;
  1514. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  1515. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1516. s->avctx = avctx;
  1517. av_log(s->avctx, AV_LOG_DEBUG, "init\n");
  1518. if (parse_layer_rates(s)) {
  1519. av_log(avctx, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
  1520. s->nlayers = 1;
  1521. s->layer_rates[0] = 0;
  1522. s->compression_rate_enc = 0;
  1523. }
  1524. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
  1525. av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
  1526. s->pred = 1;
  1527. s->format = CODEC_JP2;
  1528. }
  1529. // defaults:
  1530. // TODO: implement setting non-standard precinct size
  1531. memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
  1532. memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
  1533. codsty->nreslevels2decode=
  1534. codsty->nreslevels = 7;
  1535. codsty->nlayers = s->nlayers;
  1536. codsty->log2_cblk_width = 4;
  1537. codsty->log2_cblk_height = 4;
  1538. codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
  1539. qntsty->nguardbits = 1;
  1540. if ((s->tile_width & (s->tile_width -1)) ||
  1541. (s->tile_height & (s->tile_height-1))) {
  1542. av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
  1543. }
  1544. if (codsty->transform == FF_DWT53)
  1545. qntsty->quantsty = JPEG2000_QSTY_NONE;
  1546. else
  1547. qntsty->quantsty = JPEG2000_QSTY_SE;
  1548. s->width = avctx->width;
  1549. s->height = avctx->height;
  1550. s->ncomponents = desc->nb_components;
  1551. for (i = 0; i < 4; i++) {
  1552. s->cbps[i] = desc->comp[i].depth;
  1553. s->comp_remap[i] = i; //default
  1554. }
  1555. if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && s->ncomponents > 1) {
  1556. s->planar = 1;
  1557. ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
  1558. s->chroma_shift, s->chroma_shift + 1);
  1559. if (ret)
  1560. return ret;
  1561. if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  1562. s->comp_remap[0] = 2;
  1563. s->comp_remap[1] = 0;
  1564. s->comp_remap[2] = 1;
  1565. }
  1566. }
  1567. ff_thread_once(&init_static_once, init_luts);
  1568. init_quantization(s);
  1569. if ((ret=init_tiles(s)) < 0)
  1570. return ret;
  1571. av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
  1572. return 0;
  1573. }
  1574. static int j2kenc_destroy(AVCodecContext *avctx)
  1575. {
  1576. Jpeg2000EncoderContext *s = avctx->priv_data;
  1577. cleanup(s);
  1578. return 0;
  1579. }
  1580. // taken from the libopenjpeg wrapper so it matches
  1581. #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
  1582. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1583. static const AVOption options[] = {
  1584. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, .unit = "format" },
  1585. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, .unit = "format" },
  1586. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, .unit = "format" },
  1587. { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
  1588. { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
  1589. { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, .unit = "pred" },
  1590. { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
  1591. { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
  1592. { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
  1593. { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
  1594. { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, .unit = "prog" },
  1595. { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, .unit = "prog" },
  1596. { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, .unit = "prog" },
  1597. { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, .unit = "prog" },
  1598. { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, .unit = "prog" },
  1599. { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, .unit = "prog" },
  1600. { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
  1601. { NULL }
  1602. };
  1603. static const AVClass j2k_class = {
  1604. .class_name = "jpeg 2000 encoder",
  1605. .item_name = av_default_item_name,
  1606. .option = options,
  1607. .version = LIBAVUTIL_VERSION_INT,
  1608. };
  1609. const FFCodec ff_jpeg2000_encoder = {
  1610. .p.name = "jpeg2000",
  1611. CODEC_LONG_NAME("JPEG 2000"),
  1612. .p.type = AVMEDIA_TYPE_VIDEO,
  1613. .p.id = AV_CODEC_ID_JPEG2000,
  1614. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
  1615. AV_CODEC_CAP_FRAME_THREADS,
  1616. .priv_data_size = sizeof(Jpeg2000EncoderContext),
  1617. .init = j2kenc_init,
  1618. FF_CODEC_ENCODE_CB(encode_frame),
  1619. .close = j2kenc_destroy,
  1620. CODEC_PIXFMTS(
  1621. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48,
  1622. AV_PIX_FMT_GBR24P,AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  1623. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  1624. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
  1625. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
  1626. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
  1627. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
  1628. AV_PIX_FMT_YUV411P,
  1629. AV_PIX_FMT_YUV410P,
  1630. AV_PIX_FMT_YA8, AV_PIX_FMT_YA16,
  1631. AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA64,
  1632. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  1633. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  1634. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P16,
  1635. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
  1636. AV_PIX_FMT_PAL8),
  1637. .color_ranges = AVCOL_RANGE_MPEG,
  1638. .p.priv_class = &j2k_class,
  1639. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1640. };