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.

1388 lines
56KB

  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 interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
  166. int width, int linesize, int plane)
  167. {
  168. int i;
  169. int16_t even, odd;
  170. for (i = 0; i < width; i++) {
  171. even = (low[i] - high[i])/2;
  172. odd = (low[i] + high[i])/2;
  173. output[i] = av_clip_uintp2(even, 10);
  174. output[i + linesize] = av_clip_uintp2(odd, 10);
  175. }
  176. }
  177. static inline void inverse_temporal_filter(int16_t *low, int16_t *high, int width)
  178. {
  179. for (int i = 0; i < width; i++) {
  180. int even = (low[i] - high[i]) / 2;
  181. int odd = (low[i] + high[i]) / 2;
  182. low[i] = even;
  183. high[i] = odd;
  184. }
  185. }
  186. static void free_buffers(CFHDContext *s)
  187. {
  188. int i, j;
  189. for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  190. av_freep(&s->plane[i].idwt_buf);
  191. av_freep(&s->plane[i].idwt_tmp);
  192. s->plane[i].idwt_size = 0;
  193. for (j = 0; j < SUBBAND_COUNT_3D; j++)
  194. s->plane[i].subband[j] = NULL;
  195. for (j = 0; j < 10; j++)
  196. s->plane[i].l_h[j] = NULL;
  197. }
  198. s->a_height = 0;
  199. s->a_width = 0;
  200. }
  201. static int alloc_buffers(AVCodecContext *avctx)
  202. {
  203. CFHDContext *s = avctx->priv_data;
  204. int i, j, ret, planes, bayer = 0;
  205. int chroma_x_shift, chroma_y_shift;
  206. unsigned k;
  207. if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  208. return ret;
  209. avctx->pix_fmt = s->coded_format;
  210. ff_cfhddsp_init(&s->dsp, s->bpc, avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
  211. if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  212. &chroma_x_shift,
  213. &chroma_y_shift)) < 0)
  214. return ret;
  215. planes = av_pix_fmt_count_planes(s->coded_format);
  216. if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
  217. planes = 4;
  218. chroma_x_shift = 1;
  219. chroma_y_shift = 1;
  220. bayer = 1;
  221. }
  222. for (i = 0; i < planes; i++) {
  223. int w8, h8, w4, h4, w2, h2;
  224. int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
  225. int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
  226. ptrdiff_t stride = (FFALIGN(width / 8, 8) + 64) * 8;
  227. if (chroma_y_shift && !bayer)
  228. height = FFALIGN(height / 8, 2) * 8;
  229. s->plane[i].width = width;
  230. s->plane[i].height = height;
  231. s->plane[i].stride = stride;
  232. w8 = FFALIGN(s->plane[i].width / 8, 8) + 64;
  233. h8 = FFALIGN(height, 8) / 8;
  234. w4 = w8 * 2;
  235. h4 = h8 * 2;
  236. w2 = w4 * 2;
  237. h2 = h4 * 2;
  238. if (s->transform_type == 0) {
  239. s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
  240. s->plane[i].idwt_buf =
  241. av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
  242. s->plane[i].idwt_tmp =
  243. av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
  244. } else {
  245. s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
  246. s->plane[i].idwt_buf =
  247. av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
  248. s->plane[i].idwt_tmp =
  249. av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
  250. }
  251. if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  252. return AVERROR(ENOMEM);
  253. s->plane[i].subband[0] = s->plane[i].idwt_buf;
  254. s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  255. s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  256. s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  257. s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  258. s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  259. s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  260. if (s->transform_type == 0) {
  261. s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  262. s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  263. s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  264. } else {
  265. int16_t *frame2 =
  266. s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
  267. s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
  268. s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
  269. s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
  270. s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
  271. s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
  272. s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
  273. s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
  274. s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
  275. s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
  276. }
  277. if (s->transform_type == 0) {
  278. for (j = 0; j < DWT_LEVELS; j++) {
  279. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  280. s->plane[i].band[j][k].a_width = w8 << j;
  281. s->plane[i].band[j][k].a_height = h8 << j;
  282. }
  283. }
  284. } else {
  285. for (j = 0; j < DWT_LEVELS_3D; j++) {
  286. int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
  287. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  288. s->plane[i].band[j][k].a_width = w8 << t;
  289. s->plane[i].band[j][k].a_height = h8 << t;
  290. }
  291. }
  292. }
  293. /* ll2 and ll1 commented out because they are done in-place */
  294. s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  295. s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  296. // s->plane[i].l_h[2] = ll2;
  297. s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  298. s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  299. // s->plane[i].l_h[5] = ll1;
  300. s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  301. s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  302. if (s->transform_type != 0) {
  303. int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
  304. s->plane[i].l_h[8] = frame2;
  305. s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
  306. }
  307. }
  308. s->a_height = s->coded_height;
  309. s->a_width = s->coded_width;
  310. s->a_format = s->coded_format;
  311. return 0;
  312. }
  313. static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
  314. AVPacket *avpkt)
  315. {
  316. CFHDContext *s = avctx->priv_data;
  317. CFHDDSPContext *dsp = &s->dsp;
  318. GetByteContext gb;
  319. ThreadFrame frame = { .f = data };
  320. AVFrame *pic = data;
  321. int ret = 0, i, j, plane, got_buffer = 0;
  322. int16_t *coeff_data;
  323. init_frame_defaults(s);
  324. s->planes = av_pix_fmt_count_planes(s->coded_format);
  325. bytestream2_init(&gb, avpkt->data, avpkt->size);
  326. while (bytestream2_get_bytes_left(&gb) >= 4) {
  327. /* Bit weird but implement the tag parsing as the spec says */
  328. uint16_t tagu = bytestream2_get_be16(&gb);
  329. int16_t tag = (int16_t)tagu;
  330. int8_t tag8 = (int8_t)(tagu >> 8);
  331. uint16_t abstag = abs(tag);
  332. int8_t abs_tag8 = abs(tag8);
  333. uint16_t data = bytestream2_get_be16(&gb);
  334. if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  335. av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  336. } else if (tag == SampleFlags) {
  337. av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
  338. s->progressive = data & 0x0001;
  339. } else if (tag == FrameType) {
  340. s->frame_type = data;
  341. av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
  342. } else if (abstag == VersionMajor) {
  343. av_log(avctx, AV_LOG_DEBUG, "Version major %"PRIu16"\n", data);
  344. } else if (abstag == VersionMinor) {
  345. av_log(avctx, AV_LOG_DEBUG, "Version minor %"PRIu16"\n", data);
  346. } else if (abstag == VersionRevision) {
  347. av_log(avctx, AV_LOG_DEBUG, "Version revision %"PRIu16"\n", data);
  348. } else if (abstag == VersionEdit) {
  349. av_log(avctx, AV_LOG_DEBUG, "Version edit %"PRIu16"\n", data);
  350. } else if (abstag == Version) {
  351. av_log(avctx, AV_LOG_DEBUG, "Version %"PRIu16"\n", data);
  352. } else if (tag == ImageWidth) {
  353. av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  354. s->coded_width = data;
  355. } else if (tag == ImageHeight) {
  356. av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  357. s->coded_height = data;
  358. } else if (tag == ChannelCount) {
  359. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  360. s->channel_cnt = data;
  361. if (data > 4) {
  362. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  363. ret = AVERROR_PATCHWELCOME;
  364. break;
  365. }
  366. } else if (tag == SubbandCount) {
  367. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  368. if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
  369. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  370. ret = AVERROR_PATCHWELCOME;
  371. break;
  372. }
  373. } else if (tag == ChannelNumber) {
  374. s->channel_num = data;
  375. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  376. if (s->channel_num >= s->planes) {
  377. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  378. ret = AVERROR(EINVAL);
  379. break;
  380. }
  381. init_plane_defaults(s);
  382. } else if (tag == SubbandNumber) {
  383. if (s->subband_num != 0 && data == 1) // hack
  384. s->level++;
  385. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  386. s->subband_num = data;
  387. if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
  388. (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
  389. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  390. ret = AVERROR(EINVAL);
  391. break;
  392. }
  393. if (s->subband_num > 3) {
  394. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  395. ret = AVERROR(EINVAL);
  396. break;
  397. }
  398. } else if (tag == SubbandBand) {
  399. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  400. s->subband_num_actual = data;
  401. if ((s->transform_type == 0 && s->subband_num_actual >= SUBBAND_COUNT) ||
  402. (s->transform_type == 2 && s->subband_num_actual >= SUBBAND_COUNT_3D && s->subband_num_actual != 255)) {
  403. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  404. ret = AVERROR(EINVAL);
  405. break;
  406. }
  407. } else if (tag == LowpassPrecision)
  408. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  409. else if (tag == Quantization) {
  410. s->quantisation = data;
  411. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  412. } else if (tag == PrescaleTable) {
  413. for (i = 0; i < 8; i++)
  414. s->prescale_table[i] = (data >> (14 - i * 2)) & 0x3;
  415. av_log(avctx, AV_LOG_DEBUG, "Prescale table: %x\n", data);
  416. } else if (tag == BandEncoding) {
  417. if (!data || data > 5) {
  418. av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
  419. ret = AVERROR(EINVAL);
  420. break;
  421. }
  422. s->band_encoding = data;
  423. av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
  424. } else if (tag == LowpassWidth) {
  425. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  426. s->plane[s->channel_num].band[0][0].width = data;
  427. s->plane[s->channel_num].band[0][0].stride = data;
  428. } else if (tag == LowpassHeight) {
  429. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  430. s->plane[s->channel_num].band[0][0].height = data;
  431. } else if (tag == SampleType) {
  432. s->sample_type = data;
  433. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  434. } else if (tag == TransformType) {
  435. if (data > 2) {
  436. av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
  437. ret = AVERROR(EINVAL);
  438. break;
  439. }
  440. s->transform_type = data;
  441. av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
  442. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  443. if (abstag == 0x4001)
  444. s->peak.level = 0;
  445. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  446. bytestream2_skipu(&gb, data * 4);
  447. } else if (tag == FrameIndex) {
  448. av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
  449. s->frame_index = data;
  450. } else if (tag == SampleIndexTable) {
  451. av_log(avctx, AV_LOG_DEBUG, "Sample index table - skipping %i values\n", data);
  452. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  453. av_log(avctx, AV_LOG_ERROR, "too many values (%d)\n", data);
  454. ret = AVERROR_INVALIDDATA;
  455. break;
  456. }
  457. for (i = 0; i < data; i++) {
  458. uint32_t offset = bytestream2_get_be32(&gb);
  459. av_log(avctx, AV_LOG_DEBUG, "Offset = %"PRIu32"\n", offset);
  460. }
  461. } else if (tag == HighpassWidth) {
  462. 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);
  463. if (data < 3) {
  464. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  465. ret = AVERROR(EINVAL);
  466. break;
  467. }
  468. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  469. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  470. } else if (tag == HighpassHeight) {
  471. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  472. if (data < 3) {
  473. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  474. ret = AVERROR(EINVAL);
  475. break;
  476. }
  477. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  478. } else if (tag == BandWidth) {
  479. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  480. if (data < 3) {
  481. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  482. ret = AVERROR(EINVAL);
  483. break;
  484. }
  485. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  486. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  487. } else if (tag == BandHeight) {
  488. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  489. if (data < 3) {
  490. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  491. ret = AVERROR(EINVAL);
  492. break;
  493. }
  494. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  495. } else if (tag == InputFormat) {
  496. av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
  497. if (s->coded_format == AV_PIX_FMT_NONE ||
  498. s->coded_format == AV_PIX_FMT_YUV422P10) {
  499. if (data >= 100 && data <= 105) {
  500. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  501. } else if (data >= 122 && data <= 128) {
  502. s->coded_format = AV_PIX_FMT_GBRP12;
  503. } else if (data == 30) {
  504. s->coded_format = AV_PIX_FMT_GBRAP12;
  505. } else {
  506. s->coded_format = AV_PIX_FMT_YUV422P10;
  507. }
  508. s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  509. }
  510. } else if (tag == BandCodingFlags) {
  511. s->codebook = data & 0xf;
  512. s->difference_coding = (data >> 4) & 1;
  513. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  514. } else if (tag == Precision) {
  515. av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
  516. if (!(data == 10 || data == 12)) {
  517. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  518. ret = AVERROR(EINVAL);
  519. break;
  520. }
  521. avctx->bits_per_raw_sample = s->bpc = data;
  522. } else if (tag == EncodedFormat) {
  523. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  524. if (data == 1) {
  525. s->coded_format = AV_PIX_FMT_YUV422P10;
  526. } else if (data == 2) {
  527. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  528. } else if (data == 3) {
  529. s->coded_format = AV_PIX_FMT_GBRP12;
  530. } else if (data == 4) {
  531. s->coded_format = AV_PIX_FMT_GBRAP12;
  532. } else {
  533. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  534. ret = AVERROR_PATCHWELCOME;
  535. break;
  536. }
  537. s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  538. } else if (tag == -85) {
  539. av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  540. s->cropped_height = data;
  541. } else if (tag == -75) {
  542. s->peak.offset &= ~0xffff;
  543. s->peak.offset |= (data & 0xffff);
  544. s->peak.base = gb;
  545. s->peak.level = 0;
  546. } else if (tag == -76) {
  547. s->peak.offset &= 0xffff;
  548. s->peak.offset |= (data & 0xffffU)<<16;
  549. s->peak.base = gb;
  550. s->peak.level = 0;
  551. } else if (tag == -74 && s->peak.offset) {
  552. s->peak.level = data;
  553. bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
  554. } else
  555. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  556. if (tag == BitstreamMarker && data == 0xf0f &&
  557. s->coded_format != AV_PIX_FMT_NONE) {
  558. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  559. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  560. int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
  561. if (s->coded_width) {
  562. s->coded_width *= factor;
  563. }
  564. if (s->coded_height) {
  565. s->coded_height *= factor;
  566. }
  567. if (!s->a_width && !s->coded_width) {
  568. s->coded_width = lowpass_width * factor * 8;
  569. }
  570. if (!s->a_height && !s->coded_height) {
  571. s->coded_height = lowpass_height * factor * 8;
  572. }
  573. if (s->a_width && !s->coded_width)
  574. s->coded_width = s->a_width;
  575. if (s->a_height && !s->coded_height)
  576. s->coded_height = s->a_height;
  577. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  578. s->a_format != s->coded_format) {
  579. free_buffers(s);
  580. if ((ret = alloc_buffers(avctx)) < 0) {
  581. free_buffers(s);
  582. return ret;
  583. }
  584. }
  585. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  586. if (ret < 0)
  587. return ret;
  588. if (s->cropped_height) {
  589. unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
  590. if (avctx->height < height)
  591. return AVERROR_INVALIDDATA;
  592. avctx->height = height;
  593. }
  594. frame.f->width =
  595. frame.f->height = 0;
  596. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  597. return ret;
  598. s->coded_width = 0;
  599. s->coded_height = 0;
  600. s->coded_format = AV_PIX_FMT_NONE;
  601. got_buffer = 1;
  602. } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
  603. frame.f->width =
  604. frame.f->height = 0;
  605. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  606. return ret;
  607. s->coded_width = 0;
  608. s->coded_height = 0;
  609. s->coded_format = AV_PIX_FMT_NONE;
  610. got_buffer = 1;
  611. }
  612. if (s->subband_num_actual == 255)
  613. goto finish;
  614. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  615. /* Lowpass coefficients */
  616. if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
  617. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  618. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  619. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  620. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  621. if (lowpass_width < 3 ||
  622. lowpass_width > lowpass_a_width) {
  623. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  624. ret = AVERROR(EINVAL);
  625. goto end;
  626. }
  627. if (lowpass_height < 3 ||
  628. lowpass_height > lowpass_a_height) {
  629. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  630. ret = AVERROR(EINVAL);
  631. goto end;
  632. }
  633. if (!got_buffer) {
  634. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  635. ret = AVERROR(EINVAL);
  636. goto end;
  637. }
  638. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  639. lowpass_width * lowpass_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  640. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  641. ret = AVERROR(EINVAL);
  642. goto end;
  643. }
  644. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  645. for (i = 0; i < lowpass_height; i++) {
  646. for (j = 0; j < lowpass_width; j++)
  647. coeff_data[j] = bytestream2_get_be16u(&gb);
  648. coeff_data += lowpass_width;
  649. }
  650. /* Align to mod-4 position to continue reading tags */
  651. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  652. /* Copy last line of coefficients if odd height */
  653. if (lowpass_height & 1) {
  654. memcpy(&coeff_data[lowpass_height * lowpass_width],
  655. &coeff_data[(lowpass_height - 1) * lowpass_width],
  656. lowpass_width * sizeof(*coeff_data));
  657. }
  658. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  659. }
  660. if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  661. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  662. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  663. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  664. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  665. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  666. int expected;
  667. int a_expected = highpass_a_height * highpass_a_width;
  668. int level, run, coeff;
  669. int count = 0, bytes;
  670. if (!got_buffer) {
  671. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  672. ret = AVERROR(EINVAL);
  673. goto end;
  674. }
  675. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  676. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  677. ret = AVERROR(EINVAL);
  678. goto end;
  679. }
  680. expected = highpass_height * highpass_stride;
  681. 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);
  682. ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
  683. if (ret < 0)
  684. goto end;
  685. {
  686. OPEN_READER(re, &s->gb);
  687. const int lossless = s->band_encoding == 5;
  688. if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
  689. s->codebook = 1;
  690. if (!s->codebook) {
  691. while (1) {
  692. UPDATE_CACHE(re, &s->gb);
  693. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  694. VLC_BITS, 3, 1);
  695. /* escape */
  696. if (level == 64)
  697. break;
  698. count += run;
  699. if (count > expected)
  700. break;
  701. if (!lossless)
  702. coeff = dequant_and_decompand(s, level, s->quantisation, 0);
  703. else
  704. coeff = level;
  705. if (tag == BandSecondPass) {
  706. const uint16_t q = s->quantisation;
  707. for (i = 0; i < run; i++) {
  708. *coeff_data |= coeff << 8;
  709. *coeff_data++ *= q;
  710. }
  711. } else {
  712. for (i = 0; i < run; i++)
  713. *coeff_data++ = coeff;
  714. }
  715. }
  716. } else {
  717. while (1) {
  718. UPDATE_CACHE(re, &s->gb);
  719. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  720. VLC_BITS, 3, 1);
  721. /* escape */
  722. if (level == 255 && run == 2)
  723. break;
  724. count += run;
  725. if (count > expected)
  726. break;
  727. if (!lossless)
  728. coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
  729. else
  730. coeff = level;
  731. if (tag == BandSecondPass) {
  732. const uint16_t q = s->quantisation;
  733. for (i = 0; i < run; i++) {
  734. *coeff_data |= coeff << 8;
  735. *coeff_data++ *= q;
  736. }
  737. } else {
  738. for (i = 0; i < run; i++)
  739. *coeff_data++ = coeff;
  740. }
  741. }
  742. }
  743. CLOSE_READER(re, &s->gb);
  744. }
  745. if (count > expected) {
  746. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  747. ret = AVERROR(EINVAL);
  748. goto end;
  749. }
  750. if (s->peak.level)
  751. peak_table(coeff_data - count, &s->peak, count);
  752. if (s->difference_coding)
  753. difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  754. bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  755. if (bytes > bytestream2_get_bytes_left(&gb)) {
  756. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  757. ret = AVERROR(EINVAL);
  758. goto end;
  759. } else
  760. bytestream2_seek(&gb, bytes, SEEK_CUR);
  761. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  762. finish:
  763. if (s->subband_num_actual != 255)
  764. s->codebook = 0;
  765. }
  766. }
  767. s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  768. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  769. s->progressive = 1;
  770. s->planes = 4;
  771. }
  772. ff_thread_finish_setup(avctx);
  773. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  774. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  775. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  776. ret = AVERROR(EINVAL);
  777. goto end;
  778. }
  779. if (!got_buffer) {
  780. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  781. ret = AVERROR(EINVAL);
  782. goto end;
  783. }
  784. if (s->transform_type == 0 && s->sample_type != 1) {
  785. for (plane = 0; plane < s->planes && !ret; plane++) {
  786. /* level 1 */
  787. int lowpass_height = s->plane[plane].band[0][0].height;
  788. int output_stride = s->plane[plane].band[0][0].a_width;
  789. int lowpass_width = s->plane[plane].band[0][0].width;
  790. int highpass_stride = s->plane[plane].band[0][1].stride;
  791. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  792. ptrdiff_t dst_linesize;
  793. int16_t *low, *high, *output, *dst;
  794. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  795. act_plane = 0;
  796. dst_linesize = pic->linesize[act_plane];
  797. } else {
  798. dst_linesize = pic->linesize[act_plane] / 2;
  799. }
  800. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  801. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  802. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  803. ret = AVERROR(EINVAL);
  804. goto end;
  805. }
  806. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  807. low = s->plane[plane].subband[0];
  808. high = s->plane[plane].subband[2];
  809. output = s->plane[plane].l_h[0];
  810. dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
  811. low = s->plane[plane].subband[1];
  812. high = s->plane[plane].subband[3];
  813. output = s->plane[plane].l_h[1];
  814. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  815. low = s->plane[plane].l_h[0];
  816. high = s->plane[plane].l_h[1];
  817. output = s->plane[plane].subband[0];
  818. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  819. if (s->bpc == 12) {
  820. output = s->plane[plane].subband[0];
  821. for (i = 0; i < lowpass_height * 2; i++) {
  822. for (j = 0; j < lowpass_width * 2; j++)
  823. output[j] *= 4;
  824. output += output_stride * 2;
  825. }
  826. }
  827. /* level 2 */
  828. lowpass_height = s->plane[plane].band[1][1].height;
  829. output_stride = s->plane[plane].band[1][1].a_width;
  830. lowpass_width = s->plane[plane].band[1][1].width;
  831. highpass_stride = s->plane[plane].band[1][1].stride;
  832. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  833. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  834. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  835. ret = AVERROR(EINVAL);
  836. goto end;
  837. }
  838. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  839. low = s->plane[plane].subband[0];
  840. high = s->plane[plane].subband[5];
  841. output = s->plane[plane].l_h[3];
  842. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  843. low = s->plane[plane].subband[4];
  844. high = s->plane[plane].subband[6];
  845. output = s->plane[plane].l_h[4];
  846. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  847. low = s->plane[plane].l_h[3];
  848. high = s->plane[plane].l_h[4];
  849. output = s->plane[plane].subband[0];
  850. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  851. output = s->plane[plane].subband[0];
  852. for (i = 0; i < lowpass_height * 2; i++) {
  853. for (j = 0; j < lowpass_width * 2; j++)
  854. output[j] *= 4;
  855. output += output_stride * 2;
  856. }
  857. /* level 3 */
  858. lowpass_height = s->plane[plane].band[2][1].height;
  859. output_stride = s->plane[plane].band[2][1].a_width;
  860. lowpass_width = s->plane[plane].band[2][1].width;
  861. highpass_stride = s->plane[plane].band[2][1].stride;
  862. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  863. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  864. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  865. ret = AVERROR(EINVAL);
  866. goto end;
  867. }
  868. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  869. if (s->progressive) {
  870. low = s->plane[plane].subband[0];
  871. high = s->plane[plane].subband[8];
  872. output = s->plane[plane].l_h[6];
  873. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  874. low = s->plane[plane].subband[7];
  875. high = s->plane[plane].subband[9];
  876. output = s->plane[plane].l_h[7];
  877. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  878. dst = (int16_t *)pic->data[act_plane];
  879. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  880. if (plane & 1)
  881. dst++;
  882. if (plane > 1)
  883. dst += pic->linesize[act_plane] >> 1;
  884. }
  885. low = s->plane[plane].l_h[6];
  886. high = s->plane[plane].l_h[7];
  887. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  888. (lowpass_height * 2 > avctx->coded_height / 2 ||
  889. lowpass_width * 2 > avctx->coded_width / 2 )
  890. ) {
  891. ret = AVERROR_INVALIDDATA;
  892. goto end;
  893. }
  894. for (i = 0; i < lowpass_height * 2; i++) {
  895. dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  896. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
  897. process_alpha(dst, lowpass_width * 2);
  898. low += output_stride;
  899. high += output_stride;
  900. dst += dst_linesize;
  901. }
  902. } else {
  903. av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
  904. pic->interlaced_frame = 1;
  905. low = s->plane[plane].subband[0];
  906. high = s->plane[plane].subband[7];
  907. output = s->plane[plane].l_h[6];
  908. dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  909. low = s->plane[plane].subband[8];
  910. high = s->plane[plane].subband[9];
  911. output = s->plane[plane].l_h[7];
  912. dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  913. dst = (int16_t *)pic->data[act_plane];
  914. low = s->plane[plane].l_h[6];
  915. high = s->plane[plane].l_h[7];
  916. for (i = 0; i < lowpass_height; i++) {
  917. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  918. low += output_stride * 2;
  919. high += output_stride * 2;
  920. dst += pic->linesize[act_plane];
  921. }
  922. }
  923. }
  924. } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
  925. for (plane = 0; plane < s->planes && !ret; plane++) {
  926. int lowpass_height = s->plane[plane].band[0][0].height;
  927. int output_stride = s->plane[plane].band[0][0].a_width;
  928. int lowpass_width = s->plane[plane].band[0][0].width;
  929. int highpass_stride = s->plane[plane].band[0][1].stride;
  930. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  931. int16_t *low, *high, *output, *dst;
  932. ptrdiff_t dst_linesize;
  933. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  934. act_plane = 0;
  935. dst_linesize = pic->linesize[act_plane];
  936. } else {
  937. dst_linesize = pic->linesize[act_plane] / 2;
  938. }
  939. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  940. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  941. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  942. ret = AVERROR(EINVAL);
  943. goto end;
  944. }
  945. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  946. low = s->plane[plane].subband[0];
  947. high = s->plane[plane].subband[2];
  948. output = s->plane[plane].l_h[0];
  949. dsp->vert_filter(output, output_stride, low, lowpass_width, high, highpass_stride, lowpass_width, lowpass_height);
  950. low = s->plane[plane].subband[1];
  951. high = s->plane[plane].subband[3];
  952. output = s->plane[plane].l_h[1];
  953. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  954. low = s->plane[plane].l_h[0];
  955. high = s->plane[plane].l_h[1];
  956. output = s->plane[plane].l_h[7];
  957. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  958. if (s->bpc == 12) {
  959. output = s->plane[plane].l_h[7];
  960. for (i = 0; i < lowpass_height * 2; i++) {
  961. for (j = 0; j < lowpass_width * 2; j++)
  962. output[j] *= 4;
  963. output += output_stride * 2;
  964. }
  965. }
  966. lowpass_height = s->plane[plane].band[1][1].height;
  967. output_stride = s->plane[plane].band[1][1].a_width;
  968. lowpass_width = s->plane[plane].band[1][1].width;
  969. highpass_stride = s->plane[plane].band[1][1].stride;
  970. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  971. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  972. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  973. ret = AVERROR(EINVAL);
  974. goto end;
  975. }
  976. av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  977. low = s->plane[plane].l_h[7];
  978. high = s->plane[plane].subband[5];
  979. output = s->plane[plane].l_h[3];
  980. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  981. low = s->plane[plane].subband[4];
  982. high = s->plane[plane].subband[6];
  983. output = s->plane[plane].l_h[4];
  984. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  985. low = s->plane[plane].l_h[3];
  986. high = s->plane[plane].l_h[4];
  987. output = s->plane[plane].l_h[7];
  988. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  989. output = s->plane[plane].l_h[7];
  990. for (i = 0; i < lowpass_height * 2; i++) {
  991. for (j = 0; j < lowpass_width * 2; j++)
  992. output[j] *= 4;
  993. output += output_stride * 2;
  994. }
  995. low = s->plane[plane].subband[7];
  996. high = s->plane[plane].subband[9];
  997. output = s->plane[plane].l_h[3];
  998. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  999. low = s->plane[plane].subband[8];
  1000. high = s->plane[plane].subband[10];
  1001. output = s->plane[plane].l_h[4];
  1002. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1003. low = s->plane[plane].l_h[3];
  1004. high = s->plane[plane].l_h[4];
  1005. output = s->plane[plane].l_h[9];
  1006. dsp->horiz_filter(output, output_stride, low, output_stride, high, output_stride, lowpass_width, lowpass_height * 2);
  1007. lowpass_height = s->plane[plane].band[4][1].height;
  1008. output_stride = s->plane[plane].band[4][1].a_width;
  1009. lowpass_width = s->plane[plane].band[4][1].width;
  1010. highpass_stride = s->plane[plane].band[4][1].stride;
  1011. av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1012. if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
  1013. !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
  1014. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1015. ret = AVERROR(EINVAL);
  1016. goto end;
  1017. }
  1018. low = s->plane[plane].l_h[7];
  1019. high = s->plane[plane].l_h[9];
  1020. output = s->plane[plane].l_h[7];
  1021. for (i = 0; i < lowpass_height; i++) {
  1022. inverse_temporal_filter(low, high, lowpass_width);
  1023. low += output_stride;
  1024. high += output_stride;
  1025. }
  1026. if (s->progressive) {
  1027. low = s->plane[plane].l_h[7];
  1028. high = s->plane[plane].subband[15];
  1029. output = s->plane[plane].l_h[6];
  1030. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1031. low = s->plane[plane].subband[14];
  1032. high = s->plane[plane].subband[16];
  1033. output = s->plane[plane].l_h[7];
  1034. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1035. low = s->plane[plane].l_h[9];
  1036. high = s->plane[plane].subband[12];
  1037. output = s->plane[plane].l_h[8];
  1038. dsp->vert_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1039. low = s->plane[plane].subband[11];
  1040. high = s->plane[plane].subband[13];
  1041. output = s->plane[plane].l_h[9];
  1042. dsp->vert_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1043. if (s->sample_type == 1)
  1044. continue;
  1045. dst = (int16_t *)pic->data[act_plane];
  1046. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1047. if (plane & 1)
  1048. dst++;
  1049. if (plane > 1)
  1050. dst += pic->linesize[act_plane] >> 1;
  1051. }
  1052. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  1053. (lowpass_height * 2 > avctx->coded_height / 2 ||
  1054. lowpass_width * 2 > avctx->coded_width / 2 )
  1055. ) {
  1056. ret = AVERROR_INVALIDDATA;
  1057. goto end;
  1058. }
  1059. low = s->plane[plane].l_h[6];
  1060. high = s->plane[plane].l_h[7];
  1061. for (i = 0; i < lowpass_height * 2; i++) {
  1062. dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1063. low += output_stride;
  1064. high += output_stride;
  1065. dst += dst_linesize;
  1066. }
  1067. } else {
  1068. pic->interlaced_frame = 1;
  1069. low = s->plane[plane].l_h[7];
  1070. high = s->plane[plane].subband[14];
  1071. output = s->plane[plane].l_h[6];
  1072. dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1073. low = s->plane[plane].subband[15];
  1074. high = s->plane[plane].subband[16];
  1075. output = s->plane[plane].l_h[7];
  1076. dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1077. low = s->plane[plane].l_h[9];
  1078. high = s->plane[plane].subband[11];
  1079. output = s->plane[plane].l_h[8];
  1080. dsp->horiz_filter(output, output_stride, low, output_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1081. low = s->plane[plane].subband[12];
  1082. high = s->plane[plane].subband[13];
  1083. output = s->plane[plane].l_h[9];
  1084. dsp->horiz_filter(output, output_stride, low, highpass_stride, high, highpass_stride, lowpass_width, lowpass_height);
  1085. if (s->sample_type == 1)
  1086. continue;
  1087. dst = (int16_t *)pic->data[act_plane];
  1088. low = s->plane[plane].l_h[6];
  1089. high = s->plane[plane].l_h[7];
  1090. for (i = 0; i < lowpass_height; i++) {
  1091. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1092. low += output_stride * 2;
  1093. high += output_stride * 2;
  1094. dst += pic->linesize[act_plane];
  1095. }
  1096. }
  1097. }
  1098. }
  1099. if (s->transform_type == 2 && s->sample_type == 1) {
  1100. int16_t *low, *high, *dst;
  1101. int output_stride, lowpass_height, lowpass_width, highpass_stride;
  1102. ptrdiff_t dst_linesize;
  1103. for (plane = 0; plane < s->planes; plane++) {
  1104. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  1105. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1106. act_plane = 0;
  1107. dst_linesize = pic->linesize[act_plane];
  1108. } else {
  1109. dst_linesize = pic->linesize[act_plane] / 2;
  1110. }
  1111. lowpass_height = s->plane[plane].band[4][1].height;
  1112. output_stride = s->plane[plane].band[4][1].a_width;
  1113. lowpass_width = s->plane[plane].band[4][1].width;
  1114. highpass_stride = s->plane[plane].band[4][1].stride;
  1115. if (s->progressive) {
  1116. dst = (int16_t *)pic->data[act_plane];
  1117. low = s->plane[plane].l_h[8];
  1118. high = s->plane[plane].l_h[9];
  1119. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1120. if (plane & 1)
  1121. dst++;
  1122. if (plane > 1)
  1123. dst += pic->linesize[act_plane] >> 1;
  1124. }
  1125. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  1126. (lowpass_height * 2 > avctx->coded_height / 2 ||
  1127. lowpass_width * 2 > avctx->coded_width / 2 )
  1128. ) {
  1129. ret = AVERROR_INVALIDDATA;
  1130. goto end;
  1131. }
  1132. for (i = 0; i < lowpass_height * 2; i++) {
  1133. dsp->horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1134. low += output_stride;
  1135. high += output_stride;
  1136. dst += dst_linesize;
  1137. }
  1138. } else {
  1139. dst = (int16_t *)pic->data[act_plane];
  1140. low = s->plane[plane].l_h[8];
  1141. high = s->plane[plane].l_h[9];
  1142. for (i = 0; i < lowpass_height; i++) {
  1143. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1144. low += output_stride * 2;
  1145. high += output_stride * 2;
  1146. dst += pic->linesize[act_plane];
  1147. }
  1148. }
  1149. }
  1150. }
  1151. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  1152. process_bayer(pic, s->bpc);
  1153. end:
  1154. if (ret < 0)
  1155. return ret;
  1156. *got_frame = 1;
  1157. return avpkt->size;
  1158. }
  1159. static av_cold int cfhd_close(AVCodecContext *avctx)
  1160. {
  1161. CFHDContext *s = avctx->priv_data;
  1162. free_buffers(s);
  1163. ff_free_vlc(&s->vlc_9);
  1164. ff_free_vlc(&s->vlc_18);
  1165. return 0;
  1166. }
  1167. #if HAVE_THREADS
  1168. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1169. {
  1170. CFHDContext *psrc = src->priv_data;
  1171. CFHDContext *pdst = dst->priv_data;
  1172. int ret;
  1173. if (dst == src || psrc->transform_type == 0)
  1174. return 0;
  1175. pdst->a_format = psrc->a_format;
  1176. pdst->a_width = psrc->a_width;
  1177. pdst->a_height = psrc->a_height;
  1178. pdst->transform_type = psrc->transform_type;
  1179. pdst->progressive = psrc->progressive;
  1180. pdst->planes = psrc->planes;
  1181. if (!pdst->plane[0].idwt_buf) {
  1182. pdst->coded_width = pdst->a_width;
  1183. pdst->coded_height = pdst->a_height;
  1184. pdst->coded_format = pdst->a_format;
  1185. ret = alloc_buffers(dst);
  1186. if (ret < 0)
  1187. return ret;
  1188. }
  1189. for (int plane = 0; plane < pdst->planes; plane++) {
  1190. memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
  1191. memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
  1192. pdst->plane[plane].idwt_size * sizeof(int16_t));
  1193. }
  1194. return 0;
  1195. }
  1196. #endif
  1197. AVCodec ff_cfhd_decoder = {
  1198. .name = "cfhd",
  1199. .long_name = NULL_IF_CONFIG_SMALL("GoPro CineForm HD"),
  1200. .type = AVMEDIA_TYPE_VIDEO,
  1201. .id = AV_CODEC_ID_CFHD,
  1202. .priv_data_size = sizeof(CFHDContext),
  1203. .init = cfhd_init,
  1204. .close = cfhd_close,
  1205. .decode = cfhd_decode,
  1206. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  1207. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1208. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  1209. };