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.

314 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. import_palette(c, new_palette);
  106. import_format(c, c->frame.linesize[0], c->frame.data[0]);
  107. }
  108. static void cdxl_decode_ham6(CDXLVideoContext *c)
  109. {
  110. AVCodecContext *avctx = c->avctx;
  111. uint32_t new_palette[16], r, g, b;
  112. uint8_t *ptr, *out, index, op;
  113. int x, y;
  114. ptr = c->new_video;
  115. out = c->frame.data[0];
  116. import_palette(c, new_palette);
  117. import_format(c, avctx->width, c->new_video);
  118. for (y = 0; y < avctx->height; y++) {
  119. r = new_palette[0] & 0xFF0000;
  120. g = new_palette[0] & 0xFF00;
  121. b = new_palette[0] & 0xFF;
  122. for (x = 0; x < avctx->width; x++) {
  123. index = *ptr++;
  124. op = index >> 4;
  125. index &= 15;
  126. switch (op) {
  127. case 0:
  128. r = new_palette[index] & 0xFF0000;
  129. g = new_palette[index] & 0xFF00;
  130. b = new_palette[index] & 0xFF;
  131. break;
  132. case 1:
  133. b = index * 0x11;
  134. break;
  135. case 2:
  136. r = index * 0x11 << 16;
  137. break;
  138. case 3:
  139. g = index * 0x11 << 8;
  140. break;
  141. }
  142. AV_WL24(out + x * 3, r | g | b);
  143. }
  144. out += c->frame.linesize[0];
  145. }
  146. }
  147. static void cdxl_decode_ham8(CDXLVideoContext *c)
  148. {
  149. AVCodecContext *avctx = c->avctx;
  150. uint32_t new_palette[64], r, g, b;
  151. uint8_t *ptr, *out, index, op;
  152. int x, y;
  153. ptr = c->new_video;
  154. out = c->frame.data[0];
  155. import_palette(c, new_palette);
  156. import_format(c, avctx->width, c->new_video);
  157. for (y = 0; y < avctx->height; y++) {
  158. r = new_palette[0] & 0xFF0000;
  159. g = new_palette[0] & 0xFF00;
  160. b = new_palette[0] & 0xFF;
  161. for (x = 0; x < avctx->width; x++) {
  162. index = *ptr++;
  163. op = index >> 6;
  164. index &= 63;
  165. switch (op) {
  166. case 0:
  167. r = new_palette[index] & 0xFF0000;
  168. g = new_palette[index] & 0xFF00;
  169. b = new_palette[index] & 0xFF;
  170. break;
  171. case 1:
  172. b = (index << 2) | (b & 3);
  173. break;
  174. case 2:
  175. r = (index << 18) | (r & (3 << 16));
  176. break;
  177. case 3:
  178. g = (index << 10) | (g & (3 << 8));
  179. break;
  180. }
  181. AV_WL24(out + x * 3, r | g | b);
  182. }
  183. out += c->frame.linesize[0];
  184. }
  185. }
  186. static int cdxl_decode_frame(AVCodecContext *avctx, void *data,
  187. int *got_frame, AVPacket *pkt)
  188. {
  189. CDXLVideoContext *c = avctx->priv_data;
  190. AVFrame * const p = &c->frame;
  191. int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
  192. const uint8_t *buf = pkt->data;
  193. if (buf_size < 32)
  194. return AVERROR_INVALIDDATA;
  195. encoding = buf[1] & 7;
  196. c->format = buf[1] & 0xE0;
  197. w = AV_RB16(&buf[14]);
  198. h = AV_RB16(&buf[16]);
  199. c->bpp = buf[19];
  200. c->palette_size = AV_RB16(&buf[20]);
  201. c->palette = buf + 32;
  202. c->video = c->palette + c->palette_size;
  203. c->video_size = buf_size - c->palette_size - 32;
  204. if (c->palette_size > 512)
  205. return AVERROR_INVALIDDATA;
  206. if (buf_size < c->palette_size + 32)
  207. return AVERROR_INVALIDDATA;
  208. if (c->bpp < 1)
  209. return AVERROR_INVALIDDATA;
  210. if (c->format != BIT_PLANAR && c->format != BIT_LINE) {
  211. av_log_ask_for_sample(avctx, "unsupported pixel format: 0x%0x\n", c->format);
  212. return AVERROR_PATCHWELCOME;
  213. }
  214. if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
  215. return ret;
  216. if (w != avctx->width || h != avctx->height)
  217. avcodec_set_dimensions(avctx, w, h);
  218. aligned_width = FFALIGN(c->avctx->width, 16);
  219. c->padded_bits = aligned_width - c->avctx->width;
  220. if (c->video_size < aligned_width * avctx->height * c->bpp / 8)
  221. return AVERROR_INVALIDDATA;
  222. if (!encoding && c->palette_size && c->bpp <= 8) {
  223. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  224. } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8)) {
  225. if (c->palette_size != (1 << (c->bpp - 1)))
  226. return AVERROR_INVALIDDATA;
  227. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  228. } else {
  229. av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
  230. encoding, c->bpp);
  231. return AVERROR_PATCHWELCOME;
  232. }
  233. if (p->data[0])
  234. avctx->release_buffer(avctx, p);
  235. p->reference = 0;
  236. if ((ret = ff_get_buffer(avctx, p)) < 0) {
  237. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  238. return ret;
  239. }
  240. p->pict_type = AV_PICTURE_TYPE_I;
  241. if (encoding) {
  242. av_fast_padded_malloc(&c->new_video, &c->new_video_size,
  243. h * w + FF_INPUT_BUFFER_PADDING_SIZE);
  244. if (!c->new_video)
  245. return AVERROR(ENOMEM);
  246. if (c->bpp == 8)
  247. cdxl_decode_ham8(c);
  248. else
  249. cdxl_decode_ham6(c);
  250. } else {
  251. cdxl_decode_rgb(c);
  252. }
  253. *got_frame = 1;
  254. *(AVFrame*)data = c->frame;
  255. return buf_size;
  256. }
  257. static av_cold int cdxl_decode_end(AVCodecContext *avctx)
  258. {
  259. CDXLVideoContext *c = avctx->priv_data;
  260. av_free(c->new_video);
  261. if (c->frame.data[0])
  262. avctx->release_buffer(avctx, &c->frame);
  263. return 0;
  264. }
  265. AVCodec ff_cdxl_decoder = {
  266. .name = "cdxl",
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. .id = AV_CODEC_ID_CDXL,
  269. .priv_data_size = sizeof(CDXLVideoContext),
  270. .init = cdxl_decode_init,
  271. .close = cdxl_decode_end,
  272. .decode = cdxl_decode_frame,
  273. .capabilities = CODEC_CAP_DR1,
  274. .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
  275. };