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.

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