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.

310 lines
8.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 "get_bits.h"
  31. #include "internal.h"
  32. #define BIT_PLANAR 0x00
  33. #define CHUNKY 0x20
  34. #define BYTE_PLANAR 0x40
  35. #define BIT_LINE 0x80
  36. #define BYTE_LINE 0xC0
  37. typedef struct CDXLVideoContext {
  38. AVCodecContext *avctx;
  39. int bpp;
  40. int format;
  41. int padded_bits;
  42. const uint8_t *palette;
  43. int palette_size;
  44. const uint8_t *video;
  45. int video_size;
  46. uint8_t *new_video;
  47. int new_video_size;
  48. } CDXLVideoContext;
  49. static av_cold int cdxl_decode_init(AVCodecContext *avctx)
  50. {
  51. CDXLVideoContext *c = avctx->priv_data;
  52. c->new_video_size = 0;
  53. c->avctx = avctx;
  54. return 0;
  55. }
  56. static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
  57. {
  58. int i;
  59. for (i = 0; i < c->palette_size / 2; i++) {
  60. unsigned rgb = AV_RB16(&c->palette[i * 2]);
  61. unsigned r = ((rgb >> 8) & 0xF) * 0x11;
  62. unsigned g = ((rgb >> 4) & 0xF) * 0x11;
  63. unsigned b = (rgb & 0xF) * 0x11;
  64. AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
  65. }
  66. }
  67. static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
  68. {
  69. GetBitContext gb;
  70. int x, y, plane;
  71. if (init_get_bits8(&gb, c->video, c->video_size) < 0)
  72. return;
  73. for (plane = 0; plane < c->bpp; plane++) {
  74. for (y = 0; y < c->avctx->height; y++) {
  75. for (x = 0; x < c->avctx->width; x++)
  76. out[linesize * y + x] |= get_bits1(&gb) << plane;
  77. skip_bits(&gb, c->padded_bits);
  78. }
  79. }
  80. }
  81. static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
  82. {
  83. GetBitContext gb;
  84. int x, y, plane;
  85. if (init_get_bits8(&gb, c->video, c->video_size) < 0)
  86. return;
  87. for (y = 0; y < c->avctx->height; y++) {
  88. for (plane = 0; plane < c->bpp; plane++) {
  89. for (x = 0; x < c->avctx->width; x++)
  90. out[linesize * y + x] |= get_bits1(&gb) << plane;
  91. skip_bits(&gb, c->padded_bits);
  92. }
  93. }
  94. }
  95. static void import_format(CDXLVideoContext *c, int linesize, uint8_t *out)
  96. {
  97. memset(out, 0, linesize * c->avctx->height);
  98. switch (c->format) {
  99. case BIT_PLANAR:
  100. bitplanar2chunky(c, linesize, out);
  101. break;
  102. case BIT_LINE:
  103. bitline2chunky(c, linesize, out);
  104. break;
  105. }
  106. }
  107. static void cdxl_decode_rgb(CDXLVideoContext *c, AVFrame *frame)
  108. {
  109. uint32_t *new_palette = (uint32_t *)frame->data[1];
  110. memset(frame->data[1], 0, AVPALETTE_SIZE);
  111. import_palette(c, new_palette);
  112. import_format(c, frame->linesize[0], frame->data[0]);
  113. }
  114. static void cdxl_decode_ham6(CDXLVideoContext *c, AVFrame *frame)
  115. {
  116. AVCodecContext *avctx = c->avctx;
  117. uint32_t new_palette[16], r, g, b;
  118. uint8_t *ptr, *out, index, op;
  119. int x, y;
  120. ptr = c->new_video;
  121. out = frame->data[0];
  122. import_palette(c, new_palette);
  123. import_format(c, avctx->width, c->new_video);
  124. for (y = 0; y < avctx->height; y++) {
  125. r = new_palette[0] & 0xFF0000;
  126. g = new_palette[0] & 0xFF00;
  127. b = new_palette[0] & 0xFF;
  128. for (x = 0; x < avctx->width; x++) {
  129. index = *ptr++;
  130. op = index >> 4;
  131. index &= 15;
  132. switch (op) {
  133. case 0:
  134. r = new_palette[index] & 0xFF0000;
  135. g = new_palette[index] & 0xFF00;
  136. b = new_palette[index] & 0xFF;
  137. break;
  138. case 1:
  139. b = index * 0x11;
  140. break;
  141. case 2:
  142. r = index * 0x11 << 16;
  143. break;
  144. case 3:
  145. g = index * 0x11 << 8;
  146. break;
  147. }
  148. AV_WL24(out + x * 3, r | g | b);
  149. }
  150. out += frame->linesize[0];
  151. }
  152. }
  153. static void cdxl_decode_ham8(CDXLVideoContext *c, AVFrame *frame)
  154. {
  155. AVCodecContext *avctx = c->avctx;
  156. uint32_t new_palette[64], r, g, b;
  157. uint8_t *ptr, *out, index, op;
  158. int x, y;
  159. ptr = c->new_video;
  160. out = frame->data[0];
  161. import_palette(c, new_palette);
  162. import_format(c, avctx->width, c->new_video);
  163. for (y = 0; y < avctx->height; y++) {
  164. r = new_palette[0] & 0xFF0000;
  165. g = new_palette[0] & 0xFF00;
  166. b = new_palette[0] & 0xFF;
  167. for (x = 0; x < avctx->width; x++) {
  168. index = *ptr++;
  169. op = index >> 6;
  170. index &= 63;
  171. switch (op) {
  172. case 0:
  173. r = new_palette[index] & 0xFF0000;
  174. g = new_palette[index] & 0xFF00;
  175. b = new_palette[index] & 0xFF;
  176. break;
  177. case 1:
  178. b = (index << 2) | (b & 3);
  179. break;
  180. case 2:
  181. r = (index << 18) | (r & (3 << 16));
  182. break;
  183. case 3:
  184. g = (index << 10) | (g & (3 << 8));
  185. break;
  186. }
  187. AV_WL24(out + x * 3, r | g | b);
  188. }
  189. out += frame->linesize[0];
  190. }
  191. }
  192. static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
  193. int *got_frame, AVPacket *pkt)
  194. {
  195. CDXLVideoContext *c = avctx->priv_data;
  196. AVFrame * const p = data;
  197. int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
  198. const uint8_t *buf = pkt->data;
  199. if (buf_size < 32)
  200. return AVERROR_INVALIDDATA;
  201. encoding = buf[1] & 7;
  202. c->format = buf[1] & 0xE0;
  203. w = AV_RB16(&buf[14]);
  204. h = AV_RB16(&buf[16]);
  205. c->bpp = buf[19];
  206. c->palette_size = AV_RB16(&buf[20]);
  207. c->palette = buf + 32;
  208. c->video = c->palette + c->palette_size;
  209. c->video_size = buf_size - c->palette_size - 32;
  210. if (c->palette_size > 512)
  211. return AVERROR_INVALIDDATA;
  212. if (buf_size < c->palette_size + 32)
  213. return AVERROR_INVALIDDATA;
  214. if (c->bpp < 1)
  215. return AVERROR_INVALIDDATA;
  216. if (c->format != BIT_PLANAR && c->format != BIT_LINE) {
  217. avpriv_request_sample(avctx, "Pixel format 0x%0x", c->format);
  218. return AVERROR_PATCHWELCOME;
  219. }
  220. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  221. return ret;
  222. aligned_width = FFALIGN(c->avctx->width, 16);
  223. c->padded_bits = aligned_width - c->avctx->width;
  224. if (c->video_size < aligned_width * avctx->height * c->bpp / 8)
  225. return AVERROR_INVALIDDATA;
  226. if (!encoding && c->palette_size && c->bpp <= 8) {
  227. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  228. } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
  229. if (c->palette_size != (1 << (c->bpp - 1)))
  230. return AVERROR_INVALIDDATA;
  231. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  232. } else {
  233. avpriv_request_sample(avctx, "Encoding %d and bpp %d",
  234. encoding, c->bpp);
  235. return AVERROR_PATCHWELCOME;
  236. }
  237. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  238. return ret;
  239. p->pict_type = AV_PICTURE_TYPE_I;
  240. if (encoding) {
  241. av_fast_padded_malloc(&c->new_video, &c->new_video_size,
  242. h * w + AV_INPUT_BUFFER_PADDING_SIZE);
  243. if (!c->new_video)
  244. return AVERROR(ENOMEM);
  245. if (c->bpp == 8)
  246. cdxl_decode_ham8(c, p);
  247. else
  248. cdxl_decode_ham6(c, p);
  249. } else {
  250. cdxl_decode_rgb(c, p);
  251. }
  252. *got_frame = 1;
  253. return buf_size;
  254. }
  255. static av_cold int cdxl_decode_end(AVCodecContext *avctx)
  256. {
  257. CDXLVideoContext *c = avctx->priv_data;
  258. av_freep(&c->new_video);
  259. return 0;
  260. }
  261. AVCodec ff_cdxl_decoder = {
  262. .name = "cdxl",
  263. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .id = AV_CODEC_ID_CDXL,
  266. .priv_data_size = sizeof(CDXLVideoContext),
  267. .init = cdxl_decode_init,
  268. .close = cdxl_decode_end,
  269. .decode = cdxl_decode_frame,
  270. .capabilities = AV_CODEC_CAP_DR1,
  271. };