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.

301 lines
8.7KB

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