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.

351 lines
10KB

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