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.

279 lines
8.3KB

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