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.

425 lines
12KB

  1. /*
  2. * CRI image decoder
  3. *
  4. * Copyright (c) 2020 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Cintel RAW image decoder
  25. */
  26. #define BITSTREAM_READER_LE
  27. #include "libavutil/intfloat.h"
  28. #include "libavutil/display.h"
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "get_bits.h"
  32. #include "internal.h"
  33. #include "thread.h"
  34. typedef struct CRIContext {
  35. AVCodecContext *jpeg_avctx; // wrapper context for MJPEG
  36. AVFrame *jpgframe; // decoded JPEG tile
  37. GetByteContext gb;
  38. int color_model;
  39. const uint8_t *data;
  40. unsigned data_size;
  41. uint64_t tile_size[4];
  42. } CRIContext;
  43. static av_cold int cri_decode_init(AVCodecContext *avctx)
  44. {
  45. CRIContext *s = avctx->priv_data;
  46. const AVCodec *codec;
  47. int ret;
  48. s->jpgframe = av_frame_alloc();
  49. if (!s->jpgframe)
  50. return AVERROR(ENOMEM);
  51. codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  52. if (!codec)
  53. return AVERROR_BUG;
  54. s->jpeg_avctx = avcodec_alloc_context3(codec);
  55. if (!s->jpeg_avctx)
  56. return AVERROR(ENOMEM);
  57. s->jpeg_avctx->flags = avctx->flags;
  58. s->jpeg_avctx->flags2 = avctx->flags2;
  59. s->jpeg_avctx->dct_algo = avctx->dct_algo;
  60. s->jpeg_avctx->idct_algo = avctx->idct_algo;
  61. ret = avcodec_open2(s->jpeg_avctx, codec, NULL);
  62. if (ret < 0)
  63. return ret;
  64. return 0;
  65. }
  66. static void unpack_10bit(GetByteContext *gb, uint16_t *dst, int shift,
  67. int w, int h, ptrdiff_t stride)
  68. {
  69. int count = w * h;
  70. int pos = 0;
  71. while (count > 0) {
  72. uint32_t a0 = bytestream2_get_le32(gb);
  73. uint32_t a1 = bytestream2_get_le32(gb);
  74. uint32_t a2 = bytestream2_get_le32(gb);
  75. uint32_t a3 = bytestream2_get_le32(gb);
  76. dst[pos] = (((a0 >> 1) & 0xE00) | (a0 & 0x1FF)) << shift;
  77. pos++;
  78. if (pos >= w) {
  79. if (count == 1)
  80. break;
  81. dst += stride;
  82. pos = 0;
  83. }
  84. dst[pos] = (((a0 >> 13) & 0x3F) | ((a0 >> 14) & 0xFC0)) << shift;
  85. pos++;
  86. if (pos >= w) {
  87. if (count == 2)
  88. break;
  89. dst += stride;
  90. pos = 0;
  91. }
  92. dst[pos] = (((a0 >> 26) & 7) | ((a1 & 0x1FF) << 3)) << shift;
  93. pos++;
  94. if (pos >= w) {
  95. if (count == 3)
  96. break;
  97. dst += stride;
  98. pos = 0;
  99. }
  100. dst[pos] = (((a1 >> 10) & 0x1FF) | ((a1 >> 11) & 0xE00)) << shift;
  101. pos++;
  102. if (pos >= w) {
  103. if (count == 4)
  104. break;
  105. dst += stride;
  106. pos = 0;
  107. }
  108. dst[pos] = (((a1 >> 23) & 0x3F) | ((a2 & 0x3F) << 6)) << shift;
  109. pos++;
  110. if (pos >= w) {
  111. if (count == 5)
  112. break;
  113. dst += stride;
  114. pos = 0;
  115. }
  116. dst[pos] = (((a2 >> 7) & 0xFF8) | ((a2 >> 6) & 7)) << shift;
  117. pos++;
  118. if (pos >= w) {
  119. if (count == 6)
  120. break;
  121. dst += stride;
  122. pos = 0;
  123. }
  124. dst[pos] = (((a3 & 7) << 9) | ((a2 >> 20) & 0x1FF)) << shift;
  125. pos++;
  126. if (pos >= w) {
  127. if (count == 7)
  128. break;
  129. dst += stride;
  130. pos = 0;
  131. }
  132. dst[pos] = (((a3 >> 4) & 0xFC0) | ((a3 >> 3) & 0x3F)) << shift;
  133. pos++;
  134. if (pos >= w) {
  135. if (count == 8)
  136. break;
  137. dst += stride;
  138. pos = 0;
  139. }
  140. dst[pos] = (((a3 >> 16) & 7) | ((a3 >> 17) & 0xFF8)) << shift;
  141. pos++;
  142. if (pos >= w) {
  143. if (count == 9)
  144. break;
  145. dst += stride;
  146. pos = 0;
  147. }
  148. count -= 9;
  149. }
  150. }
  151. static int cri_decode_frame(AVCodecContext *avctx, void *data,
  152. int *got_frame, AVPacket *avpkt)
  153. {
  154. CRIContext *s = avctx->priv_data;
  155. GetByteContext *gb = &s->gb;
  156. ThreadFrame frame = { .f = data };
  157. int ret, bps, hflip = 0, vflip = 0;
  158. AVFrameSideData *rotation;
  159. int compressed = 0;
  160. AVFrame *p = data;
  161. s->data = NULL;
  162. s->data_size = 0;
  163. bytestream2_init(gb, avpkt->data, avpkt->size);
  164. while (bytestream2_get_bytes_left(gb) > 8) {
  165. char codec_name[1024];
  166. uint32_t key, length;
  167. float framerate;
  168. key = bytestream2_get_le32(gb);
  169. length = bytestream2_get_le32(gb);
  170. switch (key) {
  171. case 1:
  172. if (length != 4)
  173. return AVERROR_INVALIDDATA;
  174. if (bytestream2_get_le32(gb) != MKTAG('D', 'V', 'C', 'C'))
  175. return AVERROR_INVALIDDATA;
  176. break;
  177. case 100:
  178. if (length < 16)
  179. return AVERROR_INVALIDDATA;
  180. avctx->width = bytestream2_get_le32(gb);
  181. avctx->height = bytestream2_get_le32(gb);
  182. s->color_model = bytestream2_get_le32(gb);
  183. if (bytestream2_get_le32(gb) != 1)
  184. return AVERROR_INVALIDDATA;
  185. length -= 16;
  186. goto skip;
  187. case 101:
  188. if (length != 4)
  189. return AVERROR_INVALIDDATA;
  190. if (bytestream2_get_le32(gb) != 0)
  191. return AVERROR_INVALIDDATA;
  192. break;
  193. case 102:
  194. bytestream2_get_buffer(gb, codec_name, FFMIN(length, sizeof(codec_name) - 1));
  195. length -= FFMIN(length, sizeof(codec_name) - 1);
  196. if (strncmp(codec_name, "cintel_craw", FFMIN(length, sizeof(codec_name) - 1)))
  197. return AVERROR_INVALIDDATA;
  198. compressed = 1;
  199. goto skip;
  200. case 103:
  201. if (bytestream2_get_bytes_left(gb) < length)
  202. return AVERROR_INVALIDDATA;
  203. s->data = gb->buffer;
  204. s->data_size = length;
  205. goto skip;
  206. case 105:
  207. hflip = bytestream2_get_byte(gb) != 0;
  208. length--;
  209. goto skip;
  210. case 106:
  211. vflip = bytestream2_get_byte(gb) != 0;
  212. length--;
  213. goto skip;
  214. case 107:
  215. if (length != 4)
  216. return AVERROR_INVALIDDATA;
  217. framerate = av_int2float(bytestream2_get_le32(gb));
  218. avctx->framerate.num = framerate * 1000;
  219. avctx->framerate.den = 1000;
  220. break;
  221. case 119:
  222. if (length != 32)
  223. return AVERROR_INVALIDDATA;
  224. for (int i = 0; i < 4; i++)
  225. s->tile_size[i] = bytestream2_get_le64(gb);
  226. break;
  227. default:
  228. av_log(avctx, AV_LOG_DEBUG, "skipping unknown key %u of length %u\n", key, length);
  229. skip:
  230. bytestream2_skip(gb, length);
  231. }
  232. }
  233. switch (s->color_model) {
  234. case 76:
  235. case 88:
  236. avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
  237. break;
  238. case 77:
  239. case 89:
  240. avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
  241. break;
  242. case 78:
  243. case 90:
  244. avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
  245. break;
  246. case 45:
  247. case 79:
  248. case 91:
  249. avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
  250. break;
  251. }
  252. switch (s->color_model) {
  253. case 45:
  254. bps = 10;
  255. break;
  256. case 76:
  257. case 77:
  258. case 78:
  259. case 79:
  260. bps = 12;
  261. break;
  262. case 88:
  263. case 89:
  264. case 90:
  265. case 91:
  266. bps = 16;
  267. break;
  268. default:
  269. return AVERROR_INVALIDDATA;
  270. }
  271. if (compressed) {
  272. for (int i = 0; i < 4; i++) {
  273. if (s->tile_size[i] >= s->data_size)
  274. return AVERROR_INVALIDDATA;
  275. }
  276. if (s->tile_size[0] + s->tile_size[1] + s->tile_size[2] + s->tile_size[3] !=
  277. s->data_size)
  278. return AVERROR_INVALIDDATA;
  279. }
  280. if (!s->data || !s->data_size)
  281. return AVERROR_INVALIDDATA;
  282. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  283. return ret;
  284. avctx->bits_per_raw_sample = bps;
  285. if (!compressed && s->color_model == 45) {
  286. uint16_t *dst = (uint16_t *)p->data[0];
  287. GetByteContext gb;
  288. bytestream2_init(&gb, s->data, s->data_size);
  289. unpack_10bit(&gb, dst, 4, avctx->width, avctx->height, p->linesize[0] / 2);
  290. } else if (!compressed) {
  291. GetBitContext gbit;
  292. const int shift = 16 - bps;
  293. ret = init_get_bits8(&gbit, s->data, s->data_size);
  294. if (ret < 0)
  295. return ret;
  296. for (int y = 0; y < avctx->height; y++) {
  297. uint16_t *dst = (uint16_t *)(p->data[0] + y * p->linesize[0]);
  298. for (int x = 0; x < avctx->width; x++)
  299. dst[x] = get_bits(&gbit, bps) << shift;
  300. }
  301. } else {
  302. unsigned offset = 0;
  303. for (int tile = 0; tile < 4; tile++) {
  304. AVPacket jpkt;
  305. av_init_packet(&jpkt);
  306. jpkt.data = (uint8_t *)s->data + offset;
  307. jpkt.size = s->tile_size[tile];
  308. ret = avcodec_send_packet(s->jpeg_avctx, &jpkt);
  309. if (ret < 0) {
  310. av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  311. return ret;
  312. }
  313. ret = avcodec_receive_frame(s->jpeg_avctx, s->jpgframe);
  314. if (ret < 0 || s->jpgframe->format != AV_PIX_FMT_GRAY16 ||
  315. s->jpeg_avctx->width * 2 != avctx->width ||
  316. s->jpeg_avctx->height * 2 != avctx->height) {
  317. if (ret < 0) {
  318. av_log(avctx, AV_LOG_ERROR,
  319. "JPEG decoding error (%d).\n", ret);
  320. } else {
  321. av_log(avctx, AV_LOG_ERROR,
  322. "JPEG invalid format.\n");
  323. ret = AVERROR_INVALIDDATA;
  324. }
  325. /* Normally skip, if error explode */
  326. if (avctx->err_recognition & AV_EF_EXPLODE)
  327. return ret;
  328. else
  329. return 0;
  330. }
  331. for (int y = 0; y < s->jpeg_avctx->height; y++) {
  332. const int hw = s->jpgframe->width / 2;
  333. uint16_t *dst = (uint16_t *)(p->data[0] + (y * 2) * p->linesize[0] + tile * hw * 2);
  334. const uint16_t *src = (const uint16_t *)(s->jpgframe->data[0] + y * s->jpgframe->linesize[0]);
  335. memcpy(dst, src, hw * 2);
  336. src += hw;
  337. dst += p->linesize[0] / 2;
  338. memcpy(dst, src, hw * 2);
  339. }
  340. av_frame_unref(s->jpgframe);
  341. offset += s->tile_size[tile];
  342. }
  343. }
  344. if (hflip || vflip) {
  345. rotation = av_frame_new_side_data(p, AV_FRAME_DATA_DISPLAYMATRIX,
  346. sizeof(int32_t) * 9);
  347. if (rotation) {
  348. av_display_rotation_set((int32_t *)rotation->data, 0.f);
  349. av_display_matrix_flip((int32_t *)rotation->data, hflip, vflip);
  350. }
  351. }
  352. p->pict_type = AV_PICTURE_TYPE_I;
  353. p->key_frame = 1;
  354. *got_frame = 1;
  355. return 0;
  356. }
  357. static av_cold int cri_decode_close(AVCodecContext *avctx)
  358. {
  359. CRIContext *s = avctx->priv_data;
  360. av_frame_free(&s->jpgframe);
  361. avcodec_free_context(&s->jpeg_avctx);
  362. return 0;
  363. }
  364. AVCodec ff_cri_decoder = {
  365. .name = "cri",
  366. .type = AVMEDIA_TYPE_VIDEO,
  367. .id = AV_CODEC_ID_CRI,
  368. .priv_data_size = sizeof(CRIContext),
  369. .init = cri_decode_init,
  370. .decode = cri_decode_frame,
  371. .close = cri_decode_close,
  372. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  373. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  374. .long_name = NULL_IF_CONFIG_SMALL("Cintel RAW"),
  375. };