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.

961 lines
37KB

  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. enum CFHDParam {
  39. ChannelCount = 12,
  40. SubbandCount = 14,
  41. ImageWidth = 20,
  42. ImageHeight = 21,
  43. LowpassPrecision = 35,
  44. SubbandNumber = 48,
  45. Quantization = 53,
  46. ChannelNumber = 62,
  47. SampleFlags = 68,
  48. BitsPerComponent = 101,
  49. ChannelWidth = 104,
  50. ChannelHeight = 105,
  51. PrescaleShift = 109,
  52. };
  53. static av_cold int cfhd_init(AVCodecContext *avctx)
  54. {
  55. CFHDContext *s = avctx->priv_data;
  56. avctx->bits_per_raw_sample = 10;
  57. s->avctx = avctx;
  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->cropped_height = 0;
  77. s->bpc = 10;
  78. s->channel_cnt = 4;
  79. s->subband_cnt = SUBBAND_COUNT;
  80. s->channel_num = 0;
  81. s->lowpass_precision = 16;
  82. s->quantisation = 1;
  83. s->wavelet_depth = 3;
  84. s->pshift = 1;
  85. s->codebook = 0;
  86. s->difference_coding = 0;
  87. s->progressive = 0;
  88. init_plane_defaults(s);
  89. init_peak_table_defaults(s);
  90. }
  91. /* TODO: merge with VLC tables or use LUT */
  92. static inline int dequant_and_decompand(int level, int quantisation, int codebook)
  93. {
  94. if (codebook == 0 || codebook == 1) {
  95. int64_t abslevel = abs(level);
  96. if (level < 264)
  97. return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
  98. FFSIGN(level) * quantisation;
  99. else
  100. return level * quantisation;
  101. } else
  102. return level * quantisation;
  103. }
  104. static inline void difference_coding(int16_t *band, int width, int height)
  105. {
  106. int i,j;
  107. for (i = 0; i < height; i++) {
  108. for (j = 1; j < width; j++) {
  109. band[j] += band[j-1];
  110. }
  111. band += width;
  112. }
  113. }
  114. static inline void peak_table(int16_t *band, Peak *peak, int length)
  115. {
  116. int i;
  117. for (i = 0; i < length; i++)
  118. if (abs(band[i]) > peak->level)
  119. band[i] = bytestream2_get_le16(&peak->base);
  120. }
  121. static inline void process_alpha(int16_t *alpha, int width)
  122. {
  123. int i, channel;
  124. for (i = 0; i < width; i++) {
  125. channel = alpha[i];
  126. channel -= ALPHA_COMPAND_DC_OFFSET;
  127. channel <<= 3;
  128. channel *= ALPHA_COMPAND_GAIN;
  129. channel >>= 16;
  130. channel = av_clip_uintp2(channel, 12);
  131. alpha[i] = channel;
  132. }
  133. }
  134. static inline void filter(int16_t *output, ptrdiff_t out_stride,
  135. int16_t *low, ptrdiff_t low_stride,
  136. int16_t *high, ptrdiff_t high_stride,
  137. int len, int clip)
  138. {
  139. int16_t tmp;
  140. int i;
  141. for (i = 0; i < len; i++) {
  142. if (i == 0) {
  143. tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
  144. output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
  145. if (clip)
  146. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  147. tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
  148. output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
  149. if (clip)
  150. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  151. } else if (i == len-1) {
  152. tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
  153. output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
  154. if (clip)
  155. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  156. tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
  157. output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
  158. if (clip)
  159. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  160. } else {
  161. tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
  162. output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
  163. if (clip)
  164. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  165. tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
  166. output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
  167. if (clip)
  168. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  169. }
  170. }
  171. }
  172. static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
  173. int width, int linesize, int plane)
  174. {
  175. int i;
  176. int16_t even, odd;
  177. for (i = 0; i < width; i++) {
  178. even = (low[i] - high[i])/2;
  179. odd = (low[i] + high[i])/2;
  180. output[i] = av_clip_uintp2(even, 10);
  181. output[i + linesize] = av_clip_uintp2(odd, 10);
  182. }
  183. }
  184. static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
  185. int width)
  186. {
  187. filter(output, 1, low, 1, high, 1, width, 0);
  188. }
  189. static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
  190. int width, int clip)
  191. {
  192. filter(output, 1, low, 1, high, 1, width, clip);
  193. }
  194. static void vert_filter(int16_t *output, ptrdiff_t out_stride,
  195. int16_t *low, ptrdiff_t low_stride,
  196. int16_t *high, ptrdiff_t high_stride, int len)
  197. {
  198. filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
  199. }
  200. static void free_buffers(CFHDContext *s)
  201. {
  202. int i, j;
  203. for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  204. av_freep(&s->plane[i].idwt_buf);
  205. av_freep(&s->plane[i].idwt_tmp);
  206. for (j = 0; j < 9; j++)
  207. s->plane[i].subband[j] = NULL;
  208. for (j = 0; j < 8; j++)
  209. s->plane[i].l_h[j] = NULL;
  210. }
  211. s->a_height = 0;
  212. s->a_width = 0;
  213. }
  214. static int alloc_buffers(AVCodecContext *avctx)
  215. {
  216. CFHDContext *s = avctx->priv_data;
  217. int i, j, ret, planes;
  218. int chroma_x_shift, chroma_y_shift;
  219. unsigned k;
  220. if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  221. return ret;
  222. avctx->pix_fmt = s->coded_format;
  223. if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  224. &chroma_x_shift,
  225. &chroma_y_shift)) < 0)
  226. return ret;
  227. planes = av_pix_fmt_count_planes(s->coded_format);
  228. for (i = 0; i < planes; i++) {
  229. int w8, h8, w4, h4, w2, h2;
  230. int width = i ? avctx->width >> chroma_x_shift : avctx->width;
  231. int height = i ? avctx->height >> chroma_y_shift : avctx->height;
  232. ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
  233. if (chroma_y_shift)
  234. height = FFALIGN(height / 8, 2) * 8;
  235. s->plane[i].width = width;
  236. s->plane[i].height = height;
  237. s->plane[i].stride = stride;
  238. w8 = FFALIGN(s->plane[i].width / 8, 8);
  239. h8 = height / 8;
  240. w4 = w8 * 2;
  241. h4 = h8 * 2;
  242. w2 = w4 * 2;
  243. h2 = h4 * 2;
  244. s->plane[i].idwt_buf =
  245. av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
  246. s->plane[i].idwt_tmp =
  247. av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
  248. if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  249. return AVERROR(ENOMEM);
  250. s->plane[i].subband[0] = s->plane[i].idwt_buf;
  251. s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  252. s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  253. s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  254. s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  255. s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  256. s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  257. s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  258. s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  259. s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  260. for (j = 0; j < DWT_LEVELS; j++) {
  261. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  262. s->plane[i].band[j][k].a_width = w8 << j;
  263. s->plane[i].band[j][k].a_height = h8 << j;
  264. }
  265. }
  266. /* ll2 and ll1 commented out because they are done in-place */
  267. s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  268. s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  269. // s->plane[i].l_h[2] = ll2;
  270. s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  271. s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  272. // s->plane[i].l_h[5] = ll1;
  273. s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  274. s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  275. }
  276. s->a_height = s->coded_height;
  277. s->a_width = s->coded_width;
  278. s->a_format = s->coded_format;
  279. return 0;
  280. }
  281. static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
  282. AVPacket *avpkt)
  283. {
  284. CFHDContext *s = avctx->priv_data;
  285. GetByteContext gb;
  286. ThreadFrame frame = { .f = data };
  287. AVFrame *pic = data;
  288. int ret = 0, i, j, planes, plane, got_buffer = 0;
  289. int16_t *coeff_data;
  290. s->coded_format = AV_PIX_FMT_YUV422P10;
  291. init_frame_defaults(s);
  292. planes = av_pix_fmt_count_planes(s->coded_format);
  293. bytestream2_init(&gb, avpkt->data, avpkt->size);
  294. while (bytestream2_get_bytes_left(&gb) > 4) {
  295. /* Bit weird but implement the tag parsing as the spec says */
  296. uint16_t tagu = bytestream2_get_be16(&gb);
  297. int16_t tag = (int16_t)tagu;
  298. int8_t tag8 = (int8_t)(tagu >> 8);
  299. uint16_t abstag = abs(tag);
  300. int8_t abs_tag8 = abs(tag8);
  301. uint16_t data = bytestream2_get_be16(&gb);
  302. if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  303. av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  304. } else if (tag == SampleFlags) {
  305. av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
  306. s->progressive = data & 0x0001;
  307. } else if (tag == ImageWidth) {
  308. av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  309. s->coded_width = data;
  310. } else if (tag == ImageHeight) {
  311. av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  312. s->coded_height = data;
  313. } else if (tag == 101) {
  314. av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
  315. if (data < 1 || data > 31) {
  316. av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
  317. ret = AVERROR(EINVAL);
  318. break;
  319. }
  320. s->bpc = data;
  321. } else if (tag == ChannelCount) {
  322. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  323. s->channel_cnt = data;
  324. if (data > 4) {
  325. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  326. ret = AVERROR_PATCHWELCOME;
  327. break;
  328. }
  329. } else if (tag == SubbandCount) {
  330. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  331. if (data != SUBBAND_COUNT) {
  332. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  333. ret = AVERROR_PATCHWELCOME;
  334. break;
  335. }
  336. } else if (tag == ChannelNumber) {
  337. s->channel_num = data;
  338. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  339. if (s->channel_num >= planes) {
  340. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  341. ret = AVERROR(EINVAL);
  342. break;
  343. }
  344. init_plane_defaults(s);
  345. } else if (tag == SubbandNumber) {
  346. if (s->subband_num != 0 && data == 1) // hack
  347. s->level++;
  348. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  349. s->subband_num = data;
  350. if (s->level >= DWT_LEVELS) {
  351. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  352. ret = AVERROR(EINVAL);
  353. break;
  354. }
  355. if (s->subband_num > 3) {
  356. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  357. ret = AVERROR(EINVAL);
  358. break;
  359. }
  360. } else if (tag == 51) {
  361. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  362. s->subband_num_actual = data;
  363. if (s->subband_num_actual >= 10) {
  364. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  365. ret = AVERROR(EINVAL);
  366. break;
  367. }
  368. } else if (tag == LowpassPrecision)
  369. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  370. else if (tag == Quantization) {
  371. s->quantisation = data;
  372. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  373. } else if (tag == PrescaleShift) {
  374. s->prescale_shift[0] = (data >> 0) & 0x7;
  375. s->prescale_shift[1] = (data >> 3) & 0x7;
  376. s->prescale_shift[2] = (data >> 6) & 0x7;
  377. av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
  378. } else if (tag == 27) {
  379. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  380. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
  381. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  382. ret = AVERROR(EINVAL);
  383. break;
  384. }
  385. s->plane[s->channel_num].band[0][0].width = data;
  386. s->plane[s->channel_num].band[0][0].stride = data;
  387. } else if (tag == 28) {
  388. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  389. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
  390. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  391. ret = AVERROR(EINVAL);
  392. break;
  393. }
  394. s->plane[s->channel_num].band[0][0].height = data;
  395. } else if (tag == 1)
  396. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  397. else if (tag == 10) {
  398. if (data != 0) {
  399. avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
  400. ret = AVERROR_PATCHWELCOME;
  401. break;
  402. }
  403. av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
  404. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  405. if (abstag == 0x4001)
  406. s->peak.level = 0;
  407. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  408. bytestream2_skipu(&gb, data * 4);
  409. } else if (tag == 23) {
  410. av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
  411. avpriv_report_missing_feature(avctx, "Skip frame");
  412. ret = AVERROR_PATCHWELCOME;
  413. break;
  414. } else if (tag == 2) {
  415. av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
  416. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  417. av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
  418. ret = AVERROR_INVALIDDATA;
  419. break;
  420. }
  421. for (i = 0; i < data; i++) {
  422. uint16_t tag2 = bytestream2_get_be16(&gb);
  423. uint16_t val2 = bytestream2_get_be16(&gb);
  424. av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
  425. }
  426. } else if (tag == 41) {
  427. 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);
  428. if (data < 3) {
  429. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  430. ret = AVERROR(EINVAL);
  431. break;
  432. }
  433. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  434. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  435. } else if (tag == 42) {
  436. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  437. if (data < 3) {
  438. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  439. ret = AVERROR(EINVAL);
  440. break;
  441. }
  442. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  443. } else if (tag == 49) {
  444. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  445. if (data < 3) {
  446. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  447. ret = AVERROR(EINVAL);
  448. break;
  449. }
  450. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  451. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  452. } else if (tag == 50) {
  453. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  454. if (data < 3) {
  455. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  456. ret = AVERROR(EINVAL);
  457. break;
  458. }
  459. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  460. } else if (tag == 71) {
  461. s->codebook = data;
  462. av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
  463. } else if (tag == 72) {
  464. s->codebook = data & 0xf;
  465. s->difference_coding = (data >> 4) & 1;
  466. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  467. } else if (tag == 70) {
  468. av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
  469. if (!(data == 10 || data == 12)) {
  470. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  471. ret = AVERROR(EINVAL);
  472. break;
  473. }
  474. s->bpc = data;
  475. } else if (tag == 84) {
  476. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  477. if (data == 1)
  478. s->coded_format = AV_PIX_FMT_YUV422P10;
  479. else if (data == 3)
  480. s->coded_format = AV_PIX_FMT_GBRP12;
  481. else if (data == 4)
  482. s->coded_format = AV_PIX_FMT_GBRAP12;
  483. else {
  484. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  485. ret = AVERROR_PATCHWELCOME;
  486. break;
  487. }
  488. planes = av_pix_fmt_count_planes(s->coded_format);
  489. } else if (tag == -85) {
  490. av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  491. s->cropped_height = data;
  492. } else if (tag == -75) {
  493. s->peak.offset &= ~0xffff;
  494. s->peak.offset |= (data & 0xffff);
  495. s->peak.base = gb;
  496. s->peak.level = 0;
  497. } else if (tag == -76) {
  498. s->peak.offset &= 0xffff;
  499. s->peak.offset |= (data & 0xffffU)<<16;
  500. s->peak.base = gb;
  501. s->peak.level = 0;
  502. } else if (tag == -74 && s->peak.offset) {
  503. s->peak.level = data;
  504. bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
  505. } else
  506. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  507. /* Some kind of end of header tag */
  508. if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
  509. s->coded_format != AV_PIX_FMT_NONE) {
  510. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  511. s->a_format != s->coded_format) {
  512. free_buffers(s);
  513. if ((ret = alloc_buffers(avctx)) < 0) {
  514. free_buffers(s);
  515. return ret;
  516. }
  517. }
  518. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  519. if (ret < 0)
  520. return ret;
  521. if (s->cropped_height)
  522. avctx->height = s->cropped_height;
  523. frame.f->width =
  524. frame.f->height = 0;
  525. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  526. return ret;
  527. s->coded_width = 0;
  528. s->coded_height = 0;
  529. s->coded_format = AV_PIX_FMT_NONE;
  530. got_buffer = 1;
  531. }
  532. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  533. /* Lowpass coefficients */
  534. if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
  535. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  536. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  537. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  538. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  539. if (!got_buffer) {
  540. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  541. ret = AVERROR(EINVAL);
  542. goto end;
  543. }
  544. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  545. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  546. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  547. ret = AVERROR(EINVAL);
  548. goto end;
  549. }
  550. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  551. for (i = 0; i < lowpass_height; i++) {
  552. for (j = 0; j < lowpass_width; j++)
  553. coeff_data[j] = bytestream2_get_be16u(&gb);
  554. coeff_data += lowpass_width;
  555. }
  556. /* Align to mod-4 position to continue reading tags */
  557. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  558. /* Copy last line of coefficients if odd height */
  559. if (lowpass_height & 1) {
  560. memcpy(&coeff_data[lowpass_height * lowpass_width],
  561. &coeff_data[(lowpass_height - 1) * lowpass_width],
  562. lowpass_width * sizeof(*coeff_data));
  563. }
  564. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  565. }
  566. if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  567. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  568. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  569. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  570. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  571. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  572. int expected;
  573. int a_expected = highpass_a_height * highpass_a_width;
  574. int level, run, coeff;
  575. int count = 0, bytes;
  576. if (!got_buffer) {
  577. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  578. ret = AVERROR(EINVAL);
  579. goto end;
  580. }
  581. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  582. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  583. ret = AVERROR(EINVAL);
  584. goto end;
  585. }
  586. expected = highpass_height * highpass_stride;
  587. 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);
  588. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  589. {
  590. OPEN_READER(re, &s->gb);
  591. if (!s->codebook) {
  592. while (1) {
  593. UPDATE_CACHE(re, &s->gb);
  594. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  595. VLC_BITS, 3, 1);
  596. /* escape */
  597. if (level == 64)
  598. break;
  599. count += run;
  600. if (count > expected)
  601. break;
  602. coeff = dequant_and_decompand(level, s->quantisation, 0);
  603. for (i = 0; i < run; i++)
  604. *coeff_data++ = coeff;
  605. }
  606. } else {
  607. while (1) {
  608. UPDATE_CACHE(re, &s->gb);
  609. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  610. VLC_BITS, 3, 1);
  611. /* escape */
  612. if (level == 255 && run == 2)
  613. break;
  614. count += run;
  615. if (count > expected)
  616. break;
  617. coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
  618. for (i = 0; i < run; i++)
  619. *coeff_data++ = coeff;
  620. }
  621. }
  622. CLOSE_READER(re, &s->gb);
  623. }
  624. if (count > expected) {
  625. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  626. ret = AVERROR(EINVAL);
  627. goto end;
  628. }
  629. if (s->peak.level)
  630. peak_table(coeff_data - expected, &s->peak, expected);
  631. if (s->difference_coding)
  632. difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  633. bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  634. if (bytes > bytestream2_get_bytes_left(&gb)) {
  635. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  636. ret = AVERROR(EINVAL);
  637. goto end;
  638. } else
  639. bytestream2_seek(&gb, bytes, SEEK_CUR);
  640. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  641. s->codebook = 0;
  642. /* Copy last line of coefficients if odd height */
  643. if (highpass_height & 1) {
  644. memcpy(&coeff_data[highpass_height * highpass_stride],
  645. &coeff_data[(highpass_height - 1) * highpass_stride],
  646. highpass_stride * sizeof(*coeff_data));
  647. }
  648. }
  649. }
  650. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  651. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  652. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  653. ret = AVERROR(EINVAL);
  654. goto end;
  655. }
  656. if (!got_buffer) {
  657. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  658. ret = AVERROR(EINVAL);
  659. goto end;
  660. }
  661. planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  662. for (plane = 0; plane < planes && !ret; plane++) {
  663. /* level 1 */
  664. int lowpass_height = s->plane[plane].band[0][0].height;
  665. int lowpass_width = s->plane[plane].band[0][0].width;
  666. int highpass_stride = s->plane[plane].band[0][1].stride;
  667. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  668. int16_t *low, *high, *output, *dst;
  669. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  670. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  671. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  672. ret = AVERROR(EINVAL);
  673. goto end;
  674. }
  675. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  676. low = s->plane[plane].subband[0];
  677. high = s->plane[plane].subband[2];
  678. output = s->plane[plane].l_h[0];
  679. for (i = 0; i < lowpass_width; i++) {
  680. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  681. low++;
  682. high++;
  683. output++;
  684. }
  685. low = s->plane[plane].subband[1];
  686. high = s->plane[plane].subband[3];
  687. output = s->plane[plane].l_h[1];
  688. for (i = 0; i < lowpass_width; i++) {
  689. // note the stride of "low" is highpass_stride
  690. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  691. low++;
  692. high++;
  693. output++;
  694. }
  695. low = s->plane[plane].l_h[0];
  696. high = s->plane[plane].l_h[1];
  697. output = s->plane[plane].subband[0];
  698. for (i = 0; i < lowpass_height * 2; i++) {
  699. horiz_filter(output, low, high, lowpass_width);
  700. low += lowpass_width;
  701. high += lowpass_width;
  702. output += lowpass_width * 2;
  703. }
  704. if (s->bpc == 12) {
  705. output = s->plane[plane].subband[0];
  706. for (i = 0; i < lowpass_height * 2; i++) {
  707. for (j = 0; j < lowpass_width * 2; j++)
  708. output[j] *= 4;
  709. output += lowpass_width * 2;
  710. }
  711. }
  712. /* level 2 */
  713. lowpass_height = s->plane[plane].band[1][1].height;
  714. lowpass_width = s->plane[plane].band[1][1].width;
  715. highpass_stride = s->plane[plane].band[1][1].stride;
  716. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  717. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  718. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  719. ret = AVERROR(EINVAL);
  720. goto end;
  721. }
  722. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  723. low = s->plane[plane].subband[0];
  724. high = s->plane[plane].subband[5];
  725. output = s->plane[plane].l_h[3];
  726. for (i = 0; i < lowpass_width; i++) {
  727. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  728. low++;
  729. high++;
  730. output++;
  731. }
  732. low = s->plane[plane].subband[4];
  733. high = s->plane[plane].subband[6];
  734. output = s->plane[plane].l_h[4];
  735. for (i = 0; i < lowpass_width; i++) {
  736. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  737. low++;
  738. high++;
  739. output++;
  740. }
  741. low = s->plane[plane].l_h[3];
  742. high = s->plane[plane].l_h[4];
  743. output = s->plane[plane].subband[0];
  744. for (i = 0; i < lowpass_height * 2; i++) {
  745. horiz_filter(output, low, high, lowpass_width);
  746. low += lowpass_width;
  747. high += lowpass_width;
  748. output += lowpass_width * 2;
  749. }
  750. output = s->plane[plane].subband[0];
  751. for (i = 0; i < lowpass_height * 2; i++) {
  752. for (j = 0; j < lowpass_width * 2; j++)
  753. output[j] *= 4;
  754. output += lowpass_width * 2;
  755. }
  756. /* level 3 */
  757. lowpass_height = s->plane[plane].band[2][1].height;
  758. lowpass_width = s->plane[plane].band[2][1].width;
  759. highpass_stride = s->plane[plane].band[2][1].stride;
  760. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  761. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  762. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  763. ret = AVERROR(EINVAL);
  764. goto end;
  765. }
  766. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  767. if (s->progressive) {
  768. low = s->plane[plane].subband[0];
  769. high = s->plane[plane].subband[8];
  770. output = s->plane[plane].l_h[6];
  771. for (i = 0; i < lowpass_width; i++) {
  772. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  773. low++;
  774. high++;
  775. output++;
  776. }
  777. low = s->plane[plane].subband[7];
  778. high = s->plane[plane].subband[9];
  779. output = s->plane[plane].l_h[7];
  780. for (i = 0; i < lowpass_width; i++) {
  781. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  782. low++;
  783. high++;
  784. output++;
  785. }
  786. dst = (int16_t *)pic->data[act_plane];
  787. low = s->plane[plane].l_h[6];
  788. high = s->plane[plane].l_h[7];
  789. for (i = 0; i < lowpass_height * 2; i++) {
  790. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  791. low += lowpass_width;
  792. high += lowpass_width;
  793. dst += pic->linesize[act_plane] / 2;
  794. }
  795. } else {
  796. av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
  797. pic->interlaced_frame = 1;
  798. low = s->plane[plane].subband[0];
  799. high = s->plane[plane].subband[7];
  800. output = s->plane[plane].l_h[6];
  801. for (i = 0; i < lowpass_height; i++) {
  802. horiz_filter(output, low, high, lowpass_width);
  803. low += lowpass_width;
  804. high += lowpass_width;
  805. output += lowpass_width * 2;
  806. }
  807. low = s->plane[plane].subband[8];
  808. high = s->plane[plane].subband[9];
  809. output = s->plane[plane].l_h[7];
  810. for (i = 0; i < lowpass_height; i++) {
  811. horiz_filter(output, low, high, lowpass_width);
  812. low += lowpass_width;
  813. high += lowpass_width;
  814. output += lowpass_width * 2;
  815. }
  816. dst = (int16_t *)pic->data[act_plane];
  817. low = s->plane[plane].l_h[6];
  818. high = s->plane[plane].l_h[7];
  819. for (i = 0; i < lowpass_height; i++) {
  820. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  821. low += lowpass_width * 2;
  822. high += lowpass_width * 2;
  823. dst += pic->linesize[act_plane];
  824. }
  825. }
  826. }
  827. end:
  828. if (ret < 0)
  829. return ret;
  830. *got_frame = 1;
  831. return avpkt->size;
  832. }
  833. static av_cold int cfhd_close(AVCodecContext *avctx)
  834. {
  835. CFHDContext *s = avctx->priv_data;
  836. free_buffers(s);
  837. if (!avctx->internal->is_copy) {
  838. ff_free_vlc(&s->vlc_9);
  839. ff_free_vlc(&s->vlc_18);
  840. }
  841. return 0;
  842. }
  843. AVCodec ff_cfhd_decoder = {
  844. .name = "cfhd",
  845. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  846. .type = AVMEDIA_TYPE_VIDEO,
  847. .id = AV_CODEC_ID_CFHD,
  848. .priv_data_size = sizeof(CFHDContext),
  849. .init = cfhd_init,
  850. .close = cfhd_close,
  851. .decode = cfhd_decode,
  852. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  853. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  854. };