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.

278 lines
8.1KB

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