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.

313 lines
9.1KB

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