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.

338 lines
9.8KB

  1. /*
  2. * CDXL video decoder
  3. * Copyright (c) 2011-2012 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Commodore CDXL video decoder
  24. * @author Paul B Mahol
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/imgutils.h"
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "get_bits.h"
  32. #include "internal.h"
  33. #define BIT_PLANAR 0x00
  34. #define CHUNKY 0x20
  35. #define BYTE_PLANAR 0x40
  36. #define BIT_LINE 0x80
  37. #define BYTE_LINE 0xC0
  38. typedef struct CDXLVideoContext {
  39. AVCodecContext *avctx;
  40. int bpp;
  41. int format;
  42. int padded_bits;
  43. const uint8_t *palette;
  44. int palette_size;
  45. const uint8_t *video;
  46. int video_size;
  47. uint8_t *new_video;
  48. int new_video_size;
  49. } CDXLVideoContext;
  50. static av_cold int cdxl_decode_init(AVCodecContext *avctx)
  51. {
  52. CDXLVideoContext *c = avctx->priv_data;
  53. c->new_video_size = 0;
  54. c->avctx = avctx;
  55. return 0;
  56. }
  57. static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
  58. {
  59. int i;
  60. for (i = 0; i < c->palette_size / 2; i++) {
  61. unsigned rgb = AV_RB16(&c->palette[i * 2]);
  62. unsigned r = ((rgb >> 8) & 0xF) * 0x11;
  63. unsigned g = ((rgb >> 4) & 0xF) * 0x11;
  64. unsigned b = (rgb & 0xF) * 0x11;
  65. AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
  66. }
  67. }
  68. static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
  69. {
  70. GetBitContext gb;
  71. int x, y, plane;
  72. if (init_get_bits8(&gb, c->video, c->video_size) < 0)
  73. return;
  74. for (plane = 0; plane < c->bpp; plane++) {
  75. for (y = 0; y < c->avctx->height; y++) {
  76. for (x = 0; x < c->avctx->width; x++)
  77. out[linesize * y + x] |= get_bits1(&gb) << plane;
  78. skip_bits(&gb, c->padded_bits);
  79. }
  80. }
  81. }
  82. static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
  83. {
  84. GetBitContext gb;
  85. int x, y, plane;
  86. if (init_get_bits8(&gb, c->video, c->video_size) < 0)
  87. return;
  88. for (y = 0; y < c->avctx->height; y++) {
  89. for (plane = 0; plane < c->bpp; plane++) {
  90. for (x = 0; x < c->avctx->width; x++)
  91. out[linesize * y + x] |= get_bits1(&gb) << plane;
  92. skip_bits(&gb, c->padded_bits);
  93. }
  94. }
  95. }
  96. static void chunky2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
  97. {
  98. GetByteContext gb;
  99. int y;
  100. bytestream2_init(&gb, c->video, c->video_size);
  101. for (y = 0; y < c->avctx->height; y++) {
  102. bytestream2_get_buffer(&gb, out + linesize * y, c->avctx->width * 3);
  103. }
  104. }
  105. static void import_format(CDXLVideoContext *c, int linesize, uint8_t *out)
  106. {
  107. memset(out, 0, linesize * c->avctx->height);
  108. switch (c->format) {
  109. case BIT_PLANAR:
  110. bitplanar2chunky(c, linesize, out);
  111. break;
  112. case BIT_LINE:
  113. bitline2chunky(c, linesize, out);
  114. break;
  115. case CHUNKY:
  116. chunky2chunky(c, linesize, out);
  117. break;
  118. }
  119. }
  120. static void cdxl_decode_rgb(CDXLVideoContext *c, AVFrame *frame)
  121. {
  122. uint32_t *new_palette = (uint32_t *)frame->data[1];
  123. memset(frame->data[1], 0, AVPALETTE_SIZE);
  124. import_palette(c, new_palette);
  125. import_format(c, frame->linesize[0], frame->data[0]);
  126. }
  127. static void cdxl_decode_raw(CDXLVideoContext *c, AVFrame *frame)
  128. {
  129. import_format(c, frame->linesize[0], frame->data[0]);
  130. }
  131. static void cdxl_decode_ham6(CDXLVideoContext *c, AVFrame *frame)
  132. {
  133. AVCodecContext *avctx = c->avctx;
  134. uint32_t new_palette[16], r, g, b;
  135. uint8_t *ptr, *out, index, op;
  136. int x, y;
  137. ptr = c->new_video;
  138. out = frame->data[0];
  139. import_palette(c, new_palette);
  140. import_format(c, avctx->width, c->new_video);
  141. for (y = 0; y < avctx->height; y++) {
  142. r = new_palette[0] & 0xFF0000;
  143. g = new_palette[0] & 0xFF00;
  144. b = new_palette[0] & 0xFF;
  145. for (x = 0; x < avctx->width; x++) {
  146. index = *ptr++;
  147. op = index >> 4;
  148. index &= 15;
  149. switch (op) {
  150. case 0:
  151. r = new_palette[index] & 0xFF0000;
  152. g = new_palette[index] & 0xFF00;
  153. b = new_palette[index] & 0xFF;
  154. break;
  155. case 1:
  156. b = index * 0x11;
  157. break;
  158. case 2:
  159. r = index * 0x11 << 16;
  160. break;
  161. case 3:
  162. g = index * 0x11 << 8;
  163. break;
  164. }
  165. AV_WL24(out + x * 3, r | g | b);
  166. }
  167. out += frame->linesize[0];
  168. }
  169. }
  170. static void cdxl_decode_ham8(CDXLVideoContext *c, AVFrame *frame)
  171. {
  172. AVCodecContext *avctx = c->avctx;
  173. uint32_t new_palette[64], r, g, b;
  174. uint8_t *ptr, *out, index, op;
  175. int x, y;
  176. ptr = c->new_video;
  177. out = frame->data[0];
  178. import_palette(c, new_palette);
  179. import_format(c, avctx->width, c->new_video);
  180. for (y = 0; y < avctx->height; y++) {
  181. r = new_palette[0] & 0xFF0000;
  182. g = new_palette[0] & 0xFF00;
  183. b = new_palette[0] & 0xFF;
  184. for (x = 0; x < avctx->width; x++) {
  185. index = *ptr++;
  186. op = index >> 6;
  187. index &= 63;
  188. switch (op) {
  189. case 0:
  190. r = new_palette[index] & 0xFF0000;
  191. g = new_palette[index] & 0xFF00;
  192. b = new_palette[index] & 0xFF;
  193. break;
  194. case 1:
  195. b = (index << 2) | (b & 3);
  196. break;
  197. case 2:
  198. r = (index << 18) | (r & (3 << 16));
  199. break;
  200. case 3:
  201. g = (index << 10) | (g & (3 << 8));
  202. break;
  203. }
  204. AV_WL24(out + x * 3, r | g | b);
  205. }
  206. out += frame->linesize[0];
  207. }
  208. }
  209. static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
  210. int *got_frame, AVPacket *pkt)
  211. {
  212. CDXLVideoContext *c = avctx->priv_data;
  213. AVFrame * const p = data;
  214. int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
  215. const uint8_t *buf = pkt->data;
  216. if (buf_size < 32)
  217. return AVERROR_INVALIDDATA;
  218. encoding = buf[1] & 7;
  219. c->format = buf[1] & 0xE0;
  220. w = AV_RB16(&buf[14]);
  221. h = AV_RB16(&buf[16]);
  222. c->bpp = buf[19];
  223. c->palette_size = AV_RB16(&buf[20]);
  224. c->palette = buf + 32;
  225. c->video = c->palette + c->palette_size;
  226. c->video_size = buf_size - c->palette_size - 32;
  227. if (c->palette_size > 512)
  228. return AVERROR_INVALIDDATA;
  229. if (buf_size < c->palette_size + 32)
  230. return AVERROR_INVALIDDATA;
  231. if (c->bpp < 1)
  232. return AVERROR_INVALIDDATA;
  233. if (c->format != BIT_PLANAR && c->format != BIT_LINE && c->format != CHUNKY) {
  234. avpriv_request_sample(avctx, "Pixel format 0x%0x", c->format);
  235. return AVERROR_PATCHWELCOME;
  236. }
  237. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  238. return ret;
  239. if (c->format == CHUNKY)
  240. aligned_width = avctx->width;
  241. else
  242. aligned_width = FFALIGN(c->avctx->width, 16);
  243. c->padded_bits = aligned_width - c->avctx->width;
  244. if (c->video_size < aligned_width * avctx->height * (int64_t)c->bpp / 8)
  245. return AVERROR_INVALIDDATA;
  246. if (!encoding && c->palette_size && c->bpp <= 8 && c->format != CHUNKY) {
  247. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  248. } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8) && c->format != CHUNKY) {
  249. if (c->palette_size != (1 << (c->bpp - 1)))
  250. return AVERROR_INVALIDDATA;
  251. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  252. } else if (!encoding && c->bpp == 24 && c->format == CHUNKY &&
  253. !c->palette_size) {
  254. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  255. } else {
  256. avpriv_request_sample(avctx, "Encoding %d, bpp %d and format 0x%x",
  257. encoding, c->bpp, c->format);
  258. return AVERROR_PATCHWELCOME;
  259. }
  260. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  261. return ret;
  262. p->pict_type = AV_PICTURE_TYPE_I;
  263. if (encoding) {
  264. av_fast_padded_malloc(&c->new_video, &c->new_video_size,
  265. h * w + AV_INPUT_BUFFER_PADDING_SIZE);
  266. if (!c->new_video)
  267. return AVERROR(ENOMEM);
  268. if (c->bpp == 8)
  269. cdxl_decode_ham8(c, p);
  270. else
  271. cdxl_decode_ham6(c, p);
  272. } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  273. cdxl_decode_rgb(c, p);
  274. } else {
  275. cdxl_decode_raw(c, p);
  276. }
  277. *got_frame = 1;
  278. return buf_size;
  279. }
  280. static av_cold int cdxl_decode_end(AVCodecContext *avctx)
  281. {
  282. CDXLVideoContext *c = avctx->priv_data;
  283. av_freep(&c->new_video);
  284. return 0;
  285. }
  286. AVCodec ff_cdxl_decoder = {
  287. .name = "cdxl",
  288. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. .id = AV_CODEC_ID_CDXL,
  291. .priv_data_size = sizeof(CDXLVideoContext),
  292. .init = cdxl_decode_init,
  293. .close = cdxl_decode_end,
  294. .decode = cdxl_decode_frame,
  295. .capabilities = AV_CODEC_CAP_DR1,
  296. };