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.

311 lines
9.0KB

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