You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1597 lines
63KB

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