cfhd.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Cineform HD video decoder
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/buffer.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "codec_internal.h"
  32. #include "decode.h"
  33. #include "get_bits.h"
  34. #include "internal.h"
  35. #include "thread.h"
  36. #include "cfhd.h"
  37. #define ALPHA_COMPAND_DC_OFFSET 256
  38. #define ALPHA_COMPAND_GAIN 9400
  39. static av_cold int cfhd_init(AVCodecContext *avctx)
  40. {
  41. CFHDContext *s = avctx->priv_data;
  42. s->avctx = avctx;
  43. for (int i = 0; i < 64; i++) {
  44. int val = i;
  45. if (val >= 40) {
  46. if (val >= 54) {
  47. val -= 54;
  48. val <<= 2;
  49. val += 54;
  50. }
  51. val -= 40;
  52. val <<= 2;
  53. val += 40;
  54. }
  55. s->lut[0][i] = val;
  56. }
  57. for (int i = 0; i < 256; i++)
  58. s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
  59. return ff_cfhd_init_vlcs(s);
  60. }
  61. static void init_plane_defaults(CFHDContext *s)
  62. {
  63. s->subband_num = 0;
  64. s->level = 0;
  65. s->subband_num_actual = 0;
  66. }
  67. static void init_peak_table_defaults(CFHDContext *s)
  68. {
  69. s->peak.level = 0;
  70. s->peak.offset = 0;
  71. memset(&s->peak.base, 0, sizeof(s->peak.base));
  72. }
  73. static void init_frame_defaults(CFHDContext *s)
  74. {
  75. s->coded_width = 0;
  76. s->coded_height = 0;
  77. s->coded_format = AV_PIX_FMT_YUV422P10;
  78. s->cropped_height = 0;
  79. s->bpc = 10;
  80. s->channel_cnt = 3;
  81. s->subband_cnt = SUBBAND_COUNT;
  82. s->channel_num = 0;
  83. s->lowpass_precision = 16;
  84. s->quantisation = 1;
  85. s->codebook = 0;
  86. s->difference_coding = 0;
  87. s->frame_type = 0;
  88. s->sample_type = 0;
  89. if (s->transform_type != 2)
  90. s->transform_type = -1;
  91. init_plane_defaults(s);
  92. init_peak_table_defaults(s);
  93. }
  94. static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
  95. {
  96. if (codebook == 0 || codebook == 1) {
  97. return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
  98. } else
  99. return level * quantisation;
  100. }
  101. static inline void difference_coding(int16_t *band, int width, int height)
  102. {
  103. int i,j;
  104. for (i = 0; i < height; i++) {
  105. for (j = 1; j < width; j++) {
  106. band[j] += band[j-1];
  107. }
  108. band += width;
  109. }
  110. }
  111. static inline void peak_table(int16_t *band, Peak *peak, int length)
  112. {
  113. int i;
  114. for (i = 0; i < length; i++)
  115. if (abs(band[i]) > peak->level)
  116. band[i] = bytestream2_get_le16(&peak->base);
  117. }
  118. static inline void process_alpha(int16_t *alpha, int width)
  119. {
  120. int i, channel;
  121. for (i = 0; i < width; i++) {
  122. channel = alpha[i];
  123. channel -= ALPHA_COMPAND_DC_OFFSET;
  124. channel <<= 3;
  125. channel *= ALPHA_COMPAND_GAIN;
  126. channel >>= 16;
  127. channel = av_clip_uintp2(channel, 12);
  128. alpha[i] = channel;
  129. }
  130. }
  131. static inline void process_bayer(AVFrame *frame, int bpc)
  132. {
  133. const int linesize = frame->linesize[0];
  134. uint16_t *r = (uint16_t *)frame->data[0];
  135. uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
  136. uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
  137. uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
  138. const int mid = 1 << (bpc - 1);
  139. const int factor = 1 << (16 - bpc);
  140. for (int y = 0; y < frame->height >> 1; y++) {
  141. for (int x = 0; x < frame->width; x += 2) {
  142. int R, G1, G2, B;
  143. int g, rg, bg, gd;
  144. g = r[x];
  145. rg = g1[x];
  146. bg = g2[x];
  147. gd = b[x];
  148. gd -= mid;
  149. R = (rg - mid) * 2 + g;
  150. G1 = g + gd;
  151. G2 = g - gd;
  152. B = (bg - mid) * 2 + g;
  153. R = av_clip_uintp2(R * factor, 16);
  154. G1 = av_clip_uintp2(G1 * factor, 16);
  155. G2 = av_clip_uintp2(G2 * factor, 16);
  156. B = av_clip_uintp2(B * factor, 16);
  157. r[x] = R;
  158. g1[x] = G1;
  159. g2[x] = G2;
  160. b[x] = B;
  161. }
  162. r += linesize;
  163. g1 += linesize;
  164. g2 += linesize;
  165. b += linesize;
  166. }
  167. }
  168. static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
  169. int width, int linesize, int plane)
  170. {
  171. int i;
  172. int16_t even, odd;
  173. for (i = 0; i < width; i++) {
  174. even = (low[i] - high[i])/2;
  175. odd = (low[i] + high[i])/2;
  176. output[i] = av_clip_uintp2(even, 10);
  177. output[i + linesize] = av_clip_uintp2(odd, 10);
  178. }
  179. }
  180. static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
  181. {
  182. for (int i = 0; i < width; i++) {
  183. int even = (low[i] - high[i]) / 2;
  184. int odd = (low[i] + high[i]) / 2;
  185. low[i] = even;
  186. high[i] = odd;
  187. }
  188. }
  189. static void free_buffers(CFHDContext *s)
  190. {
  191. int i, j;
  192. for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  193. Plane *p = &s->plane[i];
  194. av_freep(&s->plane[i].idwt_buf);
  195. av_freep(&s->plane[i].idwt_tmp);
  196. s->plane[i].idwt_size = 0;
  197. for (j = 0; j < SUBBAND_COUNT_3D; j++)
  198. s->plane[i].subband[j] = NULL;
  199. for (j = 0; j < 10; j++)
  200. s->plane[i].l_h[j] = NULL;
  201. for (j = 0; j < DWT_LEVELS_3D; j++)
  202. p->band[j][0].read_ok =
  203. p->band[j][1].read_ok =
  204. p->band[j][2].read_ok =
  205. p->band[j][3].read_ok = 0;
  206. }
  207. s->a_height = 0;
  208. s->a_width = 0;
  209. s->a_transform_type = INT_MIN;
  210. }
  211. static int alloc_buffers(AVCodecContext *avctx)
  212. {
  213. CFHDContext *s = avctx->priv_data;
  214. int i, j, ret, planes, bayer = 0;
  215. int chroma_x_shift, chroma_y_shift;
  216. unsigned k;
  217. if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  218. return ret;
  219. avctx->pix_fmt = s->coded_format;
  220. ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
  221. if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  222. &chroma_x_shift,
  223. &chroma_y_shift)) < 0)
  224. return ret;
  225. planes = av_pix_fmt_count_planes(s->coded_format);
  226. if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
  227. planes = 4;
  228. chroma_x_shift = 1;
  229. chroma_y_shift = 1;
  230. bayer = 1;
  231. }
  232. for (i = 0; i < planes; i++) {
  233. int w8, h8, w4, h4, w2, h2;
  234. int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
  235. int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
  236. ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
  237. if (chroma_y_shift && !bayer)
  238. height = FFALIGN(height / 8, 2) * 8;
  239. s->plane[i].width = width;
  240. s->plane[i].height = height;
  241. s->plane[i].stride = stride;
  242. w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
  243. h8 = FFALIGN(height, 8) / 8;
  244. w4 = w8 * 2;
  245. h4 = h8 * 2;
  246. w2 = w4 * 2;
  247. h2 = h4 * 2;
  248. if (s->transform_type == 0) {
  249. s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
  250. s->plane[i].idwt_buf =
  251. av_calloc(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
  252. s->plane[i].idwt_tmp =
  253. av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
  254. } else {
  255. s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
  256. s->plane[i].idwt_buf =
  257. av_calloc(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
  258. s->plane[i].idwt_tmp =
  259. av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
  260. }
  261. if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  262. return AVERROR(ENOMEM);
  263. s->plane[i].subband[0] = s->plane[i].idwt_buf;
  264. s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  265. s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  266. s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  267. s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  268. s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  269. s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  270. if (s->transform_type == 0) {
  271. s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  272. s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  273. s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  274. } else {
  275. int16_t *frame2 =
  276. s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
  277. s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
  278. s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
  279. s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
  280. s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
  281. s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
  282. s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
  283. s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
  284. s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
  285. s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
  286. }
  287. if (s->transform_type == 0) {
  288. for (j = 0; j < DWT_LEVELS; j++) {
  289. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  290. s->plane[i].band[j][k].a_width = w8 << j;
  291. s->plane[i].band[j][k].a_height = h8 << j;
  292. }
  293. }
  294. } else {
  295. for (j = 0; j < DWT_LEVELS_3D; j++) {
  296. int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
  297. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  298. s->plane[i].band[j][k].a_width = w8 << t;
  299. s->plane[i].band[j][k].a_height = h8 << t;
  300. }
  301. }
  302. }
  303. /* ll2 and ll1 commented out because they are done in-place */
  304. s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  305. s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  306. // s->plane[i].l_h[2] = ll2;
  307. s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  308. s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  309. // s->plane[i].l_h[5] = ll1;
  310. s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  311. s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  312. if (s->transform_type != 0) {
  313. int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
  314. s->plane[i].l_h[8] = frame2;
  315. s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
  316. }
  317. }
  318. s->a_transform_type = s->transform_type;
  319. s->a_height = s->coded_height;
  320. s->a_width = s->coded_width;
  321. s->a_format = s->coded_format;
  322. return 0;
  323. }
  324. static int cfhd_decode(AVCodecContext *avctx, AVFrame *pic,
  325. int *got_frame, AVPacket *avpkt)
  326. {
  327. CFHDContext *s = avctx->priv_data;
  328. CFHDDSPContext *dsp = &s->dsp;
  329. GetByteContext gb;
  330. int ret = 0, i, j, plane, got_buffer = 0;
  331. int16_t *coeff_data;
  332. init_frame_defaults(s);
  333. s->planes = av_pix_fmt_count_planes(s->coded_format);
  334. bytestream2_init(&gb, avpkt->data, avpkt->size);
  335. while (bytestream2_get_bytes_left(&gb) >= 4) {
  336. /* Bit weird but implement the tag parsing as the spec says */
  337. uint16_t tagu = bytestream2_get_be16(&gb);
  338. int16_t tag = (int16_t)tagu;
  339. int8_t tag8 = (int8_t)(tagu >> 8);
  340. uint16_t abstag = abs(tag);
  341. int8_t abs_tag8 = abs(tag8);
  342. uint16_t data = bytestream2_get_be16(&gb);
  343. if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  344. av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  345. } else if (tag == SampleFlags) {
  346. av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
  347. s->progressive = data & 0x0001;
  348. } else if (tag == FrameType) {
  349. s->frame_type = data;
  350. av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
  351. } else if (abstag == VersionMajor) {
  352. av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
  353. } else if (abstag == VersionMinor) {
  354. av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
  355. } else if (abstag == VersionRevision) {
  356. av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
  357. } else if (abstag == VersionEdit) {
  358. av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
  359. } else if (abstag == Version) {
  360. av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
  361. } else if (tag == ImageWidth) {
  362. av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  363. s->coded_width = data;
  364. } else if (tag == ImageHeight) {
  365. av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  366. s->coded_height = data;
  367. } else if (tag == ChannelCount) {
  368. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  369. s->channel_cnt = data;
  370. if (data > 4) {
  371. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  372. ret = AVERROR_PATCHWELCOME;
  373. goto end;
  374. }
  375. } else if (tag == SubbandCount) {
  376. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  377. if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
  378. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  379. ret = AVERROR_PATCHWELCOME;
  380. goto end;
  381. }
  382. } else if (tag == ChannelNumber) {
  383. s->channel_num = data;
  384. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  385. if (s->channel_num >= s->planes) {
  386. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  387. ret = AVERROR(EINVAL);
  388. goto end;
  389. }
  390. init_plane_defaults(s);
  391. } else if (tag == SubbandNumber) {
  392. if (s->subband_num != 0 && data == 1 && (s->transform_type == 0 || s->transform_type == 2)) // hack
  393. s->level++;
  394. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  395. s->subband_num = data;
  396. if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
  397. (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
  398. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  399. ret = AVERROR(EINVAL);
  400. goto end;
  401. }
  402. if (s->subband_num > 3) {
  403. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  404. ret = AVERROR(EINVAL);
  405. goto end;
  406. }
  407. } else if (tag == SubbandBand) {
  408. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  409. if ((s->transform_type == 0 && data >= SUBBAND_COUNT) ||
  410. (s->transform_type == 2 && data >= SUBBAND_COUNT_3D && data != 255)) {
  411. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  412. ret = AVERROR(EINVAL);
  413. goto end;
  414. }
  415. if (s->transform_type == 0 || s->transform_type == 2)
  416. s->subband_num_actual = data;
  417. else
  418. av_log(avctx, AV_LOG_WARNING, "Ignoring subband num actual %"PRIu16"\n", data);
  419. } else if (tag == LowpassPrecision)
  420. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  421. else if (tag == Quantization) {
  422. s->quantisation = data;
  423. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  424. } else if (tag == PrescaleTable) {
  425. for (i = 0; i < 8; i++)
  426. s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
  427. av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
  428. } else if (tag == BandEncoding) {
  429. if (!data || data > 5) {
  430. av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
  431. ret = AVERROR(EINVAL);
  432. goto end;
  433. }
  434. s->band_encoding = data;
  435. av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
  436. } else if (tag == LowpassWidth) {
  437. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  438. s->plane[s->channel_num].band[0][0].width = data;
  439. s->plane[s->channel_num].band[0][0].stride = data;
  440. } else if (tag == LowpassHeight) {
  441. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  442. s->plane[s->channel_num].band[0][0].height = data;
  443. } else if (tag == SampleType) {
  444. s->sample_type = data;
  445. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  446. } else if (tag == TransformType) {
  447. if (data > 2) {
  448. av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
  449. ret = AVERROR(EINVAL);
  450. goto end;
  451. } else if (data == 1) {
  452. av_log(avctx, AV_LOG_ERROR, "unsupported transform type\n");
  453. ret = AVERROR_PATCHWELCOME;
  454. goto end;
  455. }
  456. if (s->transform_type == -1) {
  457. s->transform_type = data;
  458. av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
  459. } else {
  460. av_log(avctx, AV_LOG_DEBUG, "Ignoring additional transform type %"PRIu16"\n", data);
  461. }
  462. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  463. if (abstag == 0x4001)
  464. s->peak.level = 0;
  465. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  466. bytestream2_skipu(&gb, data * 4);
  467. } else if (tag == FrameIndex) {
  468. av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
  469. s->frame_index = data;
  470. } else if (tag == SampleIndexTable) {
  471. av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
  472. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  473. av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
  474. ret = AVERROR_INVALIDDATA;
  475. goto end;
  476. }
  477. for (i = 0; i < data; i++) {
  478. uint32_t offset = bytestream2_get_be32(&gb);
  479. av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
  480. }
  481. } else if (tag == HighpassWidth) {
  482. av_log(avctx, AV_LOG_DEBUG, "Highpass width %i channel %i level %i subband %i\n", data, s->channel_num, s->level, s->subband_num);
  483. if (data < 3) {
  484. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  485. ret = AVERROR(EINVAL);
  486. goto end;
  487. }
  488. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  489. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  490. } else if (tag == HighpassHeight) {
  491. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  492. if (data < 3) {
  493. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  494. ret = AVERROR(EINVAL);
  495. goto end;
  496. }
  497. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  498. } else if (tag == BandWidth) {
  499. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  500. if (data < 3) {
  501. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  502. ret = AVERROR(EINVAL);
  503. goto end;
  504. }
  505. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  506. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  507. } else if (tag == BandHeight) {
  508. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  509. if (data < 3) {
  510. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  511. ret = AVERROR(EINVAL);
  512. goto end;
  513. }
  514. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  515. } else if (tag == InputFormat) {
  516. av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
  517. if (s->coded_format == AV_PIX_FMT_NONE ||
  518. s->coded_format == AV_PIX_FMT_YUV422P10) {
  519. if (data >= 100 && data <= 105) {
  520. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  521. } else if (data >= 122 && data <= 128) {
  522. s->coded_format = AV_PIX_FMT_GBRP12;
  523. } else if (data == 30) {
  524. s->coded_format = AV_PIX_FMT_GBRAP12;
  525. } else {
  526. s->coded_format = AV_PIX_FMT_YUV422P10;
  527. }
  528. s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  529. }
  530. } else if (tag == BandCodingFlags) {
  531. s->codebook = data & 0xf;
  532. s->difference_coding = (data >> 4) & 1;
  533. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  534. } else if (tag == Precision) {
  535. av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
  536. if (!(data == 10 || data == 12)) {
  537. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  538. ret = AVERROR(EINVAL);
  539. goto end;
  540. }
  541. avctx->bits_per_raw_sample = s->bpc = data;
  542. } else if (tag == EncodedFormat) {
  543. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  544. if (data == 1) {
  545. s->coded_format = AV_PIX_FMT_YUV422P10;
  546. } else if (data == 2) {
  547. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  548. } else if (data == 3) {
  549. s->coded_format = AV_PIX_FMT_GBRP12;
  550. } else if (data == 4) {
  551. s->coded_format = AV_PIX_FMT_GBRAP12;
  552. } else {
  553. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  554. ret = AVERROR_PATCHWELCOME;
  555. goto end;
  556. }
  557. s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  558. } else if (tag == -DisplayHeight) {
  559. av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  560. s->cropped_height = data;
  561. } else if (tag == -PeakOffsetLow) {
  562. s->peak.offset &= ~0xffff;
  563. s->peak.offset |= (data & 0xffff);
  564. s->peak.base = gb;
  565. s->peak.level = 0;
  566. } else if (tag == -PeakOffsetHigh) {
  567. s->peak.offset &= 0xffff;
  568. s->peak.offset |= (data & 0xffffU)<<16;
  569. s->peak.base = gb;
  570. s->peak.level = 0;
  571. } else if (tag == -PeakLevel && s->peak.offset) {
  572. s->peak.level = data;
  573. if (s->peak.offset < 4 - bytestream2_tell(&s->peak.base) ||
  574. s->peak.offset > 4 + bytestream2_get_bytes_left(&s->peak.base)
  575. ) {
  576. ret = AVERROR_INVALIDDATA;
  577. goto end;
  578. }
  579. bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
  580. } else
  581. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  582. if (tag == BitstreamMarker && data == 0xf0f &&
  583. s->coded_format != AV_PIX_FMT_NONE) {
  584. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  585. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  586. int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
  587. if (s->coded_width) {
  588. s->coded_width *= factor;
  589. }
  590. if (s->coded_height) {
  591. s->coded_height *= factor;
  592. }
  593. if (!s->a_width && !s->coded_width) {
  594. s->coded_width = lowpass_width * factor * 8;
  595. }
  596. if (!s->a_height && !s->coded_height) {
  597. s->coded_height = lowpass_height * factor * 8;
  598. }
  599. if (s->a_width && !s->coded_width)
  600. s->coded_width = s->a_width;
  601. if (s->a_height && !s->coded_height)
  602. s->coded_height = s->a_height;
  603. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  604. s->a_format != s->coded_format ||
  605. s->transform_type != s->a_transform_type) {
  606. free_buffers(s);
  607. if ((ret = alloc_buffers(avctx)) < 0) {
  608. free_buffers(s);
  609. return ret;
  610. }
  611. }
  612. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  613. if (ret < 0)
  614. return ret;
  615. if (s->cropped_height) {
  616. unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
  617. if (avctx->height < height)
  618. return AVERROR_INVALIDDATA;
  619. avctx->height = height;
  620. }
  621. pic->width = pic->height = 0;
  622. if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0)
  623. return ret;
  624. s->coded_width = 0;
  625. s->coded_height = 0;
  626. s->coded_format = AV_PIX_FMT_NONE;
  627. got_buffer = 1;
  628. } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
  629. pic->width = pic->height = 0;
  630. if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0)
  631. return ret;
  632. s->coded_width = 0;
  633. s->coded_height = 0;
  634. s->coded_format = AV_PIX_FMT_NONE;
  635. got_buffer = 1;
  636. }
  637. if (s->subband_num_actual == 255)
  638. goto finish;
  639. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  640. /* Lowpass coefficients */
  641. if (tag == BitstreamMarker && data == 0xf0f) {
  642. int lowpass_height, lowpass_width, lowpass_a_height, lowpass_a_width;
  643. if (!s->a_width || !s->a_height) {
  644. ret = AVERROR_INVALIDDATA;
  645. goto end;
  646. }
  647. lowpass_height = s->plane[s->channel_num].band[0][0].height;
  648. lowpass_width = s->plane[s->channel_num].band[0][0].width;
  649. lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  650. lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  651. if (lowpass_width < 3 ||
  652. lowpass_width > lowpass_a_width) {
  653. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  654. ret = AVERROR(EINVAL);
  655. goto end;
  656. }
  657. if (lowpass_height < 3 ||
  658. lowpass_height > lowpass_a_height) {
  659. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  660. ret = AVERROR(EINVAL);
  661. goto end;
  662. }
  663. if (!got_buffer) {
  664. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  665. ret = AVERROR(EINVAL);
  666. goto end;
  667. }
  668. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  669. lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  670. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  671. ret = AVERROR(EINVAL);
  672. goto end;
  673. }
  674. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  675. for (i = 0; i < lowpass_height; i++) {
  676. for (j = 0; j < lowpass_width; j++)
  677. coeff_data[j] = bytestream2_get_be16u(&gb);
  678. coeff_data += lowpass_width;
  679. }
  680. /* Align to mod-4 position to continue reading tags */
  681. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  682. /* Copy last line of coefficients if odd height */
  683. if (lowpass_height & 1) {
  684. memcpy(&coeff_data[lowpass_height * lowpass_width],
  685. &coeff_data[(lowpass_height - 1) * lowpass_width],
  686. lowpass_width * sizeof(*coeff_data));
  687. }
  688. s->plane[s->channel_num].band[0][0].read_ok = 1;
  689. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  690. }
  691. av_assert0(s->subband_num_actual != 255);
  692. if (tag == BandHeader || tag == BandSecondPass) {
  693. int highpass_height, highpass_width, highpass_a_width, highpass_a_height, highpass_stride, a_expected;
  694. int expected;
  695. int level, run, coeff;
  696. int count = 0, bytes;
  697. if (!s->a_width || !s->a_height) {
  698. ret = AVERROR_INVALIDDATA;
  699. goto end;
  700. }
  701. highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  702. highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  703. highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  704. highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  705. highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  706. a_expected = highpass_a_height * highpass_a_width;
  707. if (!got_buffer) {
  708. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  709. ret = AVERROR(EINVAL);
  710. goto end;
  711. }
  712. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  713. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  714. ret = AVERROR(EINVAL);
  715. goto end;
  716. }
  717. expected = highpass_height * highpass_stride;
  718. av_log(avctx, AV_LOG_DEBUG, "Start subband coeffs plane %i level %i codebook %i expected %i\n", s->channel_num, s->level, s->codebook, expected);
  719. ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
  720. if (ret < 0)
  721. goto end;
  722. {
  723. OPEN_READER(re, &s->gb);
  724. const int lossless = s->band_encoding == 5;
  725. if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
  726. s->codebook = 1;
  727. if (!s->codebook) {
  728. while (1) {
  729. UPDATE_CACHE(re, &s->gb);
  730. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  731. VLC_BITS, 3, 1);
  732. /* escape */
  733. if (level == 64 && run == 2)
  734. break;
  735. count += run;
  736. if (count > expected)
  737. break;
  738. if (!lossless)
  739. coeff = dequant_and_decompand(s, level, s->quantisation, 0);
  740. else
  741. coeff = level;
  742. if (tag == BandSecondPass) {
  743. const uint16_t q = s->quantisation;
  744. for (i = 0; i < run; i++) {
  745. *coeff_data |= coeff * 256U;
  746. *coeff_data++ *= q;
  747. }
  748. } else {
  749. for (i = 0; i < run; i++)
  750. *coeff_data++ = coeff;
  751. }
  752. }
  753. } else {
  754. while (1) {
  755. UPDATE_CACHE(re, &s->gb);
  756. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  757. VLC_BITS, 3, 1);
  758. /* escape */
  759. if (level == 255 && run == 2)
  760. break;
  761. count += run;
  762. if (count > expected)
  763. break;
  764. if (!lossless)
  765. coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
  766. else
  767. coeff = level;
  768. if (tag == BandSecondPass) {
  769. const uint16_t q = s->quantisation;
  770. for (i = 0; i < run; i++) {
  771. *coeff_data |= coeff * 256U;
  772. *coeff_data++ *= q;
  773. }
  774. } else {
  775. for (i = 0; i < run; i++)
  776. *coeff_data++ = coeff;
  777. }
  778. }
  779. }
  780. CLOSE_READER(re, &s->gb);
  781. }
  782. if (count > expected) {
  783. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  784. ret = AVERROR(EINVAL);
  785. goto end;
  786. }
  787. if (s->peak.level)
  788. peak_table(coeff_data - count, &s->peak, count);
  789. if (s->difference_coding)
  790. difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  791. bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  792. if (bytes > bytestream2_get_bytes_left(&gb)) {
  793. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  794. ret = AVERROR(EINVAL);
  795. goto end;
  796. } else
  797. bytestream2_seek(&gb, bytes, SEEK_CUR);
  798. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  799. s->plane[s->channel_num].band[s->level][s->subband_num].read_ok = 1;
  800. finish:
  801. if (s->subband_num_actual != 255)
  802. s->codebook = 0;
  803. }
  804. }
  805. s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  806. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  807. s->progressive = 1;
  808. s->planes = 4;
  809. }
  810. ff_thread_finish_setup(avctx);
  811. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  812. s->a_transform_type == INT_MIN ||
  813. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  814. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  815. ret = AVERROR(EINVAL);
  816. goto end;
  817. }
  818. if (!got_buffer) {
  819. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  820. ret = AVERROR(EINVAL);
  821. goto end;
  822. }
  823. for (plane = 0; plane < s->planes; plane++) {
  824. int o, level;
  825. for (level = 0; level < (s->transform_type == 0 ? DWT_LEVELS : DWT_LEVELS_3D) ; level++) {
  826. if (s->transform_type == 2)
  827. if (level == 2 || level == 5)
  828. continue;
  829. for (o = !!level; o < 4 ; o++) {
  830. if (!s->plane[plane].band[level][o].read_ok) {
  831. ret = AVERROR_INVALIDDATA;
  832. goto end;
  833. }
  834. }
  835. }
  836. }
  837. if (s->transform_type == 0 && s->sample_type != 1) {
  838. for (plane = 0; plane < s->planes && !ret; plane++) {
  839. /* level 1 */
  840. int lowpass_height = s->plane[plane].band[0][0].height;
  841. int output_stride = s->plane[plane].band[0][0].a_width;
  842. int lowpass_width = s->plane[plane].band[0][0].width;
  843. int highpass_stride = s->plane[plane].band[0][1].stride;
  844. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  845. ptrdiff_t dst_linesize;
  846. int16_t *low, *high, *output, *dst;
  847. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  848. act_plane = 0;
  849. dst_linesize = pic->linesize[act_plane];
  850. } else {
  851. dst_linesize = pic->linesize[act_plane] / 2;
  852. }
  853. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  854. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
  855. lowpass_width < 3 || lowpass_height < 3) {
  856. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  857. ret = AVERROR(EINVAL);
  858. goto end;
  859. }
  860. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  861. low = s->plane[plane].subband[0];
  862. high = s->plane[plane].subband[2];
  863. output = s->plane[plane].l_h[0];
  864. dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
  865. low = s->plane[plane].subband[1];
  866. high = s->plane[plane].subband[3];
  867. output = s->plane[plane].l_h[1];
  868. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  869. low = s->plane[plane].l_h[0];
  870. high = s->plane[plane].l_h[1];
  871. output = s->plane[plane].subband[0];
  872. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  873. if (s->bpc == 12) {
  874. output = s->plane[plane].subband[0];
  875. for (i = 0; i < lowpass_height * 2; i++) {
  876. for (j = 0; j < lowpass_width * 2; j++)
  877. output[j] *= 4;
  878. output += output_stride * 2;
  879. }
  880. }
  881. /* level 2 */
  882. lowpass_height = s->plane[plane].band[1][1].height;
  883. output_stride = s->plane[plane].band[1][1].a_width;
  884. lowpass_width = s->plane[plane].band[1][1].width;
  885. highpass_stride = s->plane[plane].band[1][1].stride;
  886. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  887. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
  888. lowpass_width < 3 || lowpass_height < 3) {
  889. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  890. ret = AVERROR(EINVAL);
  891. goto end;
  892. }
  893. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  894. low = s->plane[plane].subband[0];
  895. high = s->plane[plane].subband[5];
  896. output = s->plane[plane].l_h[3];
  897. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  898. low = s->plane[plane].subband[4];
  899. high = s->plane[plane].subband[6];
  900. output = s->plane[plane].l_h[4];
  901. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  902. low = s->plane[plane].l_h[3];
  903. high = s->plane[plane].l_h[4];
  904. output = s->plane[plane].subband[0];
  905. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  906. output = s->plane[plane].subband[0];
  907. for (i = 0; i < lowpass_height * 2; i++) {
  908. for (j = 0; j < lowpass_width * 2; j++)
  909. output[j] *= 4;
  910. output += output_stride * 2;
  911. }
  912. /* level 3 */
  913. lowpass_height = s->plane[plane].band[2][1].height;
  914. output_stride = s->plane[plane].band[2][1].a_width;
  915. lowpass_width = s->plane[plane].band[2][1].width;
  916. highpass_stride = s->plane[plane].band[2][1].stride;
  917. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  918. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width ||
  919. lowpass_height < 3 || lowpass_width < 3 || lowpass_width * 2 > s->plane[plane].width) {
  920. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  921. ret = AVERROR(EINVAL);
  922. goto end;
  923. }
  924. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  925. if (s->progressive) {
  926. low = s->plane[plane].subband[0];
  927. high = s->plane[plane].subband[8];
  928. output = s->plane[plane].l_h[6];
  929. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  930. low = s->plane[plane].subband[7];
  931. high = s->plane[plane].subband[9];
  932. output = s->plane[plane].l_h[7];
  933. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  934. dst = (int16_t *)pic->data[act_plane];
  935. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  936. if (plane & 1)
  937. dst++;
  938. if (plane > 1)
  939. dst += pic->linesize[act_plane] >> 1;
  940. }
  941. low = s->plane[plane].l_h[6];
  942. high = s->plane[plane].l_h[7];
  943. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  944. (lowpass_height * 2 > avctx->coded_height / 2 ||
  945. lowpass_width * 2 > avctx->coded_width / 2 )
  946. ) {
  947. ret = AVERROR_INVALIDDATA;
  948. goto end;
  949. }
  950. for (i = 0; i < s->plane[act_plane].height; i++) {
  951. dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  952. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
  953. process_alpha(dst, lowpass_width * 2);
  954. low += output_stride;
  955. high += output_stride;
  956. dst += dst_linesize;
  957. }
  958. } else {
  959. av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
  960. pic->interlaced_frame = 1;
  961. low = s->plane[plane].subband[0];
  962. high = s->plane[plane].subband[7];
  963. output = s->plane[plane].l_h[6];
  964. dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  965. low = s->plane[plane].subband[8];
  966. high = s->plane[plane].subband[9];
  967. output = s->plane[plane].l_h[7];
  968. dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  969. dst = (int16_t *)pic->data[act_plane];
  970. low = s->plane[plane].l_h[6];
  971. high = s->plane[plane].l_h[7];
  972. for (i = 0; i < s->plane[act_plane].height / 2; i++) {
  973. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  974. low += output_stride * 2;
  975. high += output_stride * 2;
  976. dst += pic->linesize[act_plane];
  977. }
  978. }
  979. }
  980. } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
  981. for (plane = 0; plane < s->planes && !ret; plane++) {
  982. int lowpass_height = s->plane[plane].band[0][0].height;
  983. int output_stride = s->plane[plane].band[0][0].a_width;
  984. int lowpass_width = s->plane[plane].band[0][0].width;
  985. int highpass_stride = s->plane[plane].band[0][1].stride;
  986. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  987. int16_t *low, *high, *output, *dst;
  988. ptrdiff_t dst_linesize;
  989. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  990. act_plane = 0;
  991. dst_linesize = pic->linesize[act_plane];
  992. } else {
  993. dst_linesize = pic->linesize[act_plane] / 2;
  994. }
  995. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  996. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width ||
  997. lowpass_width < 3 || lowpass_height < 3) {
  998. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  999. ret = AVERROR(EINVAL);
  1000. goto end;
  1001. }
  1002. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1003. low = s->plane[plane].subband[0];
  1004. high = s->plane[plane].subband[2];
  1005. output = s->plane[plane].l_h[0];
  1006. dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
  1007. low = s->plane[plane].subband[1];
  1008. high = s->plane[plane].subband[3];
  1009. output = s->plane[plane].l_h[1];
  1010. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1011. low = s->plane[plane].l_h[0];
  1012. high = s->plane[plane].l_h[1];
  1013. output = s->plane[plane].l_h[7];
  1014. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  1015. if (s->bpc == 12) {
  1016. output = s->plane[plane].l_h[7];
  1017. for (i = 0; i < lowpass_height * 2; i++) {
  1018. for (j = 0; j < lowpass_width * 2; j++)
  1019. output[j] *= 4;
  1020. output += output_stride * 2;
  1021. }
  1022. }
  1023. lowpass_height = s->plane[plane].band[1][1].height;
  1024. output_stride = s->plane[plane].band[1][1].a_width;
  1025. lowpass_width = s->plane[plane].band[1][1].width;
  1026. highpass_stride = s->plane[plane].band[1][1].stride;
  1027. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  1028. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width ||
  1029. lowpass_width < 3 || lowpass_height < 3) {
  1030. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1031. ret = AVERROR(EINVAL);
  1032. goto end;
  1033. }
  1034. av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1035. low = s->plane[plane].l_h[7];
  1036. high = s->plane[plane].subband[5];
  1037. output = s->plane[plane].l_h[3];
  1038. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1039. low = s->plane[plane].subband[4];
  1040. high = s->plane[plane].subband[6];
  1041. output = s->plane[plane].l_h[4];
  1042. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1043. low = s->plane[plane].l_h[3];
  1044. high = s->plane[plane].l_h[4];
  1045. output = s->plane[plane].l_h[7];
  1046. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  1047. output = s->plane[plane].l_h[7];
  1048. for (i = 0; i < lowpass_height * 2; i++) {
  1049. for (j = 0; j < lowpass_width * 2; j++)
  1050. output[j] *= 4;
  1051. output += output_stride * 2;
  1052. }
  1053. low = s->plane[plane].subband[7];
  1054. high = s->plane[plane].subband[9];
  1055. output = s->plane[plane].l_h[3];
  1056. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1057. low = s->plane[plane].subband[8];
  1058. high = s->plane[plane].subband[10];
  1059. output = s->plane[plane].l_h[4];
  1060. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1061. low = s->plane[plane].l_h[3];
  1062. high = s->plane[plane].l_h[4];
  1063. output = s->plane[plane].l_h[9];
  1064. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  1065. lowpass_height = s->plane[plane].band[4][1].height;
  1066. output_stride = s->plane[plane].band[4][1].a_width;
  1067. lowpass_width = s->plane[plane].band[4][1].width;
  1068. highpass_stride = s->plane[plane].band[4][1].stride;
  1069. av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1070. if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
  1071. !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
  1072. lowpass_width < 3 || lowpass_height < 3) {
  1073. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1074. ret = AVERROR(EINVAL);
  1075. goto end;
  1076. }
  1077. low = s->plane[plane].l_h[7];
  1078. high = s->plane[plane].l_h[9];
  1079. output = s->plane[plane].l_h[7];
  1080. for (i = 0; i < lowpass_height; i++) {
  1081. inverse_temporal_filter(low, high, lowpass_width);
  1082. low += output_stride;
  1083. high += output_stride;
  1084. }
  1085. if (s->progressive) {
  1086. low = s->plane[plane].l_h[7];
  1087. high = s->plane[plane].subband[15];
  1088. output = s->plane[plane].l_h[6];
  1089. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1090. low = s->plane[plane].subband[14];
  1091. high = s->plane[plane].subband[16];
  1092. output = s->plane[plane].l_h[7];
  1093. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1094. low = s->plane[plane].l_h[9];
  1095. high = s->plane[plane].subband[12];
  1096. output = s->plane[plane].l_h[8];
  1097. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1098. low = s->plane[plane].subband[11];
  1099. high = s->plane[plane].subband[13];
  1100. output = s->plane[plane].l_h[9];
  1101. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1102. if (s->sample_type == 1)
  1103. continue;
  1104. dst = (int16_t *)pic->data[act_plane];
  1105. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1106. if (plane & 1)
  1107. dst++;
  1108. if (plane > 1)
  1109. dst += pic->linesize[act_plane] >> 1;
  1110. }
  1111. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  1112. (lowpass_height * 2 > avctx->coded_height / 2 ||
  1113. lowpass_width * 2 > avctx->coded_width / 2 )
  1114. ) {
  1115. ret = AVERROR_INVALIDDATA;
  1116. goto end;
  1117. }
  1118. low = s->plane[plane].l_h[6];
  1119. high = s->plane[plane].l_h[7];
  1120. for (i = 0; i < s->plane[act_plane].height; i++) {
  1121. dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1122. low += output_stride;
  1123. high += output_stride;
  1124. dst += dst_linesize;
  1125. }
  1126. } else {
  1127. pic->interlaced_frame = 1;
  1128. low = s->plane[plane].l_h[7];
  1129. high = s->plane[plane].subband[14];
  1130. output = s->plane[plane].l_h[6];
  1131. dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1132. low = s->plane[plane].subband[15];
  1133. high = s->plane[plane].subband[16];
  1134. output = s->plane[plane].l_h[7];
  1135. dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1136. low = s->plane[plane].l_h[9];
  1137. high = s->plane[plane].subband[11];
  1138. output = s->plane[plane].l_h[8];
  1139. dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1140. low = s->plane[plane].subband[12];
  1141. high = s->plane[plane].subband[13];
  1142. output = s->plane[plane].l_h[9];
  1143. dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1144. if (s->sample_type == 1)
  1145. continue;
  1146. dst = (int16_t *)pic->data[act_plane];
  1147. low = s->plane[plane].l_h[6];
  1148. high = s->plane[plane].l_h[7];
  1149. for (i = 0; i < s->plane[act_plane].height / 2; i++) {
  1150. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1151. low += output_stride * 2;
  1152. high += output_stride * 2;
  1153. dst += pic->linesize[act_plane];
  1154. }
  1155. }
  1156. }
  1157. }
  1158. if (s->transform_type == 2 && s->sample_type == 1) {
  1159. int16_t *low, *high, *dst;
  1160. int output_stride, lowpass_height, lowpass_width;
  1161. ptrdiff_t dst_linesize;
  1162. for (plane = 0; plane < s->planes; plane++) {
  1163. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  1164. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1165. act_plane = 0;
  1166. dst_linesize = pic->linesize[act_plane];
  1167. } else {
  1168. dst_linesize = pic->linesize[act_plane] / 2;
  1169. }
  1170. lowpass_height = s->plane[plane].band[4][1].height;
  1171. output_stride = s->plane[plane].band[4][1].a_width;
  1172. lowpass_width = s->plane[plane].band[4][1].width;
  1173. if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
  1174. s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width ||
  1175. lowpass_width < 3 || lowpass_height < 3) {
  1176. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1177. ret = AVERROR(EINVAL);
  1178. goto end;
  1179. }
  1180. if (s->progressive) {
  1181. dst = (int16_t *)pic->data[act_plane];
  1182. low = s->plane[plane].l_h[8];
  1183. high = s->plane[plane].l_h[9];
  1184. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1185. if (plane & 1)
  1186. dst++;
  1187. if (plane > 1)
  1188. dst += pic->linesize[act_plane] >> 1;
  1189. }
  1190. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  1191. (lowpass_height * 2 > avctx->coded_height / 2 ||
  1192. lowpass_width * 2 > avctx->coded_width / 2 )
  1193. ) {
  1194. ret = AVERROR_INVALIDDATA;
  1195. goto end;
  1196. }
  1197. for (i = 0; i < s->plane[act_plane].height; i++) {
  1198. dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1199. low += output_stride;
  1200. high += output_stride;
  1201. dst += dst_linesize;
  1202. }
  1203. } else {
  1204. dst = (int16_t *)pic->data[act_plane];
  1205. low = s->plane[plane].l_h[8];
  1206. high = s->plane[plane].l_h[9];
  1207. for (i = 0; i < s->plane[act_plane].height / 2; i++) {
  1208. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1209. low += output_stride * 2;
  1210. high += output_stride * 2;
  1211. dst += pic->linesize[act_plane];
  1212. }
  1213. }
  1214. }
  1215. }
  1216. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  1217. process_bayer(pic, s->bpc);
  1218. end:
  1219. if (ret < 0)
  1220. return ret;
  1221. *got_frame = 1;
  1222. return avpkt->size;
  1223. }
  1224. static av_cold int cfhd_close(AVCodecContext *avctx)
  1225. {
  1226. CFHDContext *s = avctx->priv_data;
  1227. free_buffers(s);
  1228. ff_free_vlc(&s->vlc_9);
  1229. ff_free_vlc(&s->vlc_18);
  1230. return 0;
  1231. }
  1232. #if HAVE_THREADS
  1233. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1234. {
  1235. CFHDContext *psrc = src->priv_data;
  1236. CFHDContext *pdst = dst->priv_data;
  1237. int ret;
  1238. if (dst == src || psrc->transform_type == 0)
  1239. return 0;
  1240. if (pdst->plane[0].idwt_size != psrc->plane[0].idwt_size ||
  1241. pdst->a_format != psrc->a_format ||
  1242. pdst->a_width != psrc->a_width ||
  1243. pdst->a_height != psrc->a_height ||
  1244. pdst->a_transform_type != psrc->a_transform_type)
  1245. free_buffers(pdst);
  1246. pdst->a_format = psrc->a_format;
  1247. pdst->a_width = psrc->a_width;
  1248. pdst->a_height = psrc->a_height;
  1249. pdst->a_transform_type = psrc->a_transform_type;
  1250. pdst->transform_type = psrc->transform_type;
  1251. pdst->progressive = psrc->progressive;
  1252. pdst->planes = psrc->planes;
  1253. if (!pdst->plane[0].idwt_buf) {
  1254. pdst->coded_width = pdst->a_width;
  1255. pdst->coded_height = pdst->a_height;
  1256. pdst->coded_format = pdst->a_format;
  1257. pdst->transform_type = pdst->a_transform_type;
  1258. ret = alloc_buffers(dst);
  1259. if (ret < 0)
  1260. return ret;
  1261. }
  1262. for (int plane = 0; plane < pdst->planes; plane++) {
  1263. memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
  1264. memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
  1265. pdst->plane[plane].idwt_size * sizeof(int16_t));
  1266. }
  1267. return 0;
  1268. }
  1269. #endif
  1270. const FFCodec ff_cfhd_decoder = {
  1271. .p.name = "cfhd",
  1272. CODEC_LONG_NAME("GoPro CineForm HD"),
  1273. .p.type = AVMEDIA_TYPE_VIDEO,
  1274. .p.id = AV_CODEC_ID_CFHD,
  1275. .priv_data_size = sizeof(CFHDContext),
  1276. .init = cfhd_init,
  1277. .close = cfhd_close,
  1278. FF_CODEC_DECODE_CB(cfhd_decode),
  1279. UPDATE_THREAD_CONTEXT(update_thread_context),
  1280. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1281. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1282. };