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.

482 lines
16KB

  1. /*
  2. * Cinepak Video Decoder
  3. * Copyright (C) 2003 The FFmpeg project
  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. /**
  22. * @file
  23. * Cinepak video decoder
  24. * @author Ewald Snel <ewald@rambo.its.tudelft.nl>
  25. *
  26. * @see For more information on the Cinepak algorithm, visit:
  27. * http://www.csse.monash.edu.au/~timf/
  28. * @see For more information on the quirky data inside Sega FILM/CPK files, visit:
  29. * http://wiki.multimedia.cx/index.php?title=Sega_FILM
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "libavutil/common.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "avcodec.h"
  37. #include "internal.h"
  38. typedef struct cvid_codebook {
  39. uint8_t y0, y1, y2, y3;
  40. uint8_t u, v;
  41. } cvid_codebook;
  42. #define MAX_STRIPS 32
  43. typedef struct cvid_strip {
  44. uint16_t id;
  45. uint16_t x1, y1;
  46. uint16_t x2, y2;
  47. cvid_codebook v4_codebook[256];
  48. cvid_codebook v1_codebook[256];
  49. } cvid_strip;
  50. typedef struct CinepakContext {
  51. AVCodecContext *avctx;
  52. AVFrame *frame;
  53. const unsigned char *data;
  54. int size;
  55. int width, height;
  56. int palette_video;
  57. cvid_strip strips[MAX_STRIPS];
  58. int sega_film_skip_bytes;
  59. uint32_t pal[256];
  60. } CinepakContext;
  61. static void cinepak_decode_codebook (cvid_codebook *codebook,
  62. int chunk_id, int size, const uint8_t *data)
  63. {
  64. const uint8_t *eod = (data + size);
  65. uint32_t flag, mask;
  66. int i, n;
  67. /* check if this chunk contains 4- or 6-element vectors */
  68. n = (chunk_id & 0x04) ? 4 : 6;
  69. flag = 0;
  70. mask = 0;
  71. for (i=0; i < 256; i++) {
  72. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  73. if ((data + 4) > eod)
  74. break;
  75. flag = AV_RB32 (data);
  76. data += 4;
  77. mask = 0x80000000;
  78. }
  79. if (!(chunk_id & 0x01) || (flag & mask)) {
  80. if ((data + n) > eod)
  81. break;
  82. if (n == 6) {
  83. codebook[i].y0 = *data++;
  84. codebook[i].y1 = *data++;
  85. codebook[i].y2 = *data++;
  86. codebook[i].y3 = *data++;
  87. codebook[i].u = 128 + *data++;
  88. codebook[i].v = 128 + *data++;
  89. } else {
  90. /* this codebook type indicates either greyscale or
  91. * palettized video; if palettized, U & V components will
  92. * not be used so it is safe to set them to 128 for the
  93. * benefit of greyscale rendering in YUV420P */
  94. codebook[i].y0 = *data++;
  95. codebook[i].y1 = *data++;
  96. codebook[i].y2 = *data++;
  97. codebook[i].y3 = *data++;
  98. codebook[i].u = 128;
  99. codebook[i].v = 128;
  100. }
  101. }
  102. }
  103. }
  104. static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
  105. int chunk_id, int size, const uint8_t *data)
  106. {
  107. const uint8_t *eod = (data + size);
  108. uint32_t flag, mask;
  109. cvid_codebook *codebook;
  110. unsigned int x, y;
  111. uint32_t iy[4];
  112. uint32_t iu[2];
  113. uint32_t iv[2];
  114. flag = 0;
  115. mask = 0;
  116. for (y=strip->y1; y < strip->y2; y+=4) {
  117. iy[0] = strip->x1 + (y * s->frame->linesize[0]);
  118. iy[1] = iy[0] + s->frame->linesize[0];
  119. iy[2] = iy[1] + s->frame->linesize[0];
  120. iy[3] = iy[2] + s->frame->linesize[0];
  121. iu[0] = (strip->x1/2) + ((y/2) * s->frame->linesize[1]);
  122. iu[1] = iu[0] + s->frame->linesize[1];
  123. iv[0] = (strip->x1/2) + ((y/2) * s->frame->linesize[2]);
  124. iv[1] = iv[0] + s->frame->linesize[2];
  125. for (x=strip->x1; x < strip->x2; x+=4) {
  126. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  127. if ((data + 4) > eod)
  128. return AVERROR_INVALIDDATA;
  129. flag = AV_RB32 (data);
  130. data += 4;
  131. mask = 0x80000000;
  132. }
  133. if (!(chunk_id & 0x01) || (flag & mask)) {
  134. if (!(chunk_id & 0x02) && !(mask >>= 1)) {
  135. if ((data + 4) > eod)
  136. return AVERROR_INVALIDDATA;
  137. flag = AV_RB32 (data);
  138. data += 4;
  139. mask = 0x80000000;
  140. }
  141. if ((chunk_id & 0x02) || (~flag & mask)) {
  142. if (data >= eod)
  143. return AVERROR_INVALIDDATA;
  144. codebook = &strip->v1_codebook[*data++];
  145. s->frame->data[0][iy[0] + 0] = codebook->y0;
  146. s->frame->data[0][iy[0] + 1] = codebook->y0;
  147. s->frame->data[0][iy[1] + 0] = codebook->y0;
  148. s->frame->data[0][iy[1] + 1] = codebook->y0;
  149. if (!s->palette_video) {
  150. s->frame->data[1][iu[0]] = codebook->u;
  151. s->frame->data[2][iv[0]] = codebook->v;
  152. }
  153. s->frame->data[0][iy[0] + 2] = codebook->y1;
  154. s->frame->data[0][iy[0] + 3] = codebook->y1;
  155. s->frame->data[0][iy[1] + 2] = codebook->y1;
  156. s->frame->data[0][iy[1] + 3] = codebook->y1;
  157. if (!s->palette_video) {
  158. s->frame->data[1][iu[0] + 1] = codebook->u;
  159. s->frame->data[2][iv[0] + 1] = codebook->v;
  160. }
  161. s->frame->data[0][iy[2] + 0] = codebook->y2;
  162. s->frame->data[0][iy[2] + 1] = codebook->y2;
  163. s->frame->data[0][iy[3] + 0] = codebook->y2;
  164. s->frame->data[0][iy[3] + 1] = codebook->y2;
  165. if (!s->palette_video) {
  166. s->frame->data[1][iu[1]] = codebook->u;
  167. s->frame->data[2][iv[1]] = codebook->v;
  168. }
  169. s->frame->data[0][iy[2] + 2] = codebook->y3;
  170. s->frame->data[0][iy[2] + 3] = codebook->y3;
  171. s->frame->data[0][iy[3] + 2] = codebook->y3;
  172. s->frame->data[0][iy[3] + 3] = codebook->y3;
  173. if (!s->palette_video) {
  174. s->frame->data[1][iu[1] + 1] = codebook->u;
  175. s->frame->data[2][iv[1] + 1] = codebook->v;
  176. }
  177. } else if (flag & mask) {
  178. if ((data + 4) > eod)
  179. return AVERROR_INVALIDDATA;
  180. codebook = &strip->v4_codebook[*data++];
  181. s->frame->data[0][iy[0] + 0] = codebook->y0;
  182. s->frame->data[0][iy[0] + 1] = codebook->y1;
  183. s->frame->data[0][iy[1] + 0] = codebook->y2;
  184. s->frame->data[0][iy[1] + 1] = codebook->y3;
  185. if (!s->palette_video) {
  186. s->frame->data[1][iu[0]] = codebook->u;
  187. s->frame->data[2][iv[0]] = codebook->v;
  188. }
  189. codebook = &strip->v4_codebook[*data++];
  190. s->frame->data[0][iy[0] + 2] = codebook->y0;
  191. s->frame->data[0][iy[0] + 3] = codebook->y1;
  192. s->frame->data[0][iy[1] + 2] = codebook->y2;
  193. s->frame->data[0][iy[1] + 3] = codebook->y3;
  194. if (!s->palette_video) {
  195. s->frame->data[1][iu[0] + 1] = codebook->u;
  196. s->frame->data[2][iv[0] + 1] = codebook->v;
  197. }
  198. codebook = &strip->v4_codebook[*data++];
  199. s->frame->data[0][iy[2] + 0] = codebook->y0;
  200. s->frame->data[0][iy[2] + 1] = codebook->y1;
  201. s->frame->data[0][iy[3] + 0] = codebook->y2;
  202. s->frame->data[0][iy[3] + 1] = codebook->y3;
  203. if (!s->palette_video) {
  204. s->frame->data[1][iu[1]] = codebook->u;
  205. s->frame->data[2][iv[1]] = codebook->v;
  206. }
  207. codebook = &strip->v4_codebook[*data++];
  208. s->frame->data[0][iy[2] + 2] = codebook->y0;
  209. s->frame->data[0][iy[2] + 3] = codebook->y1;
  210. s->frame->data[0][iy[3] + 2] = codebook->y2;
  211. s->frame->data[0][iy[3] + 3] = codebook->y3;
  212. if (!s->palette_video) {
  213. s->frame->data[1][iu[1] + 1] = codebook->u;
  214. s->frame->data[2][iv[1] + 1] = codebook->v;
  215. }
  216. }
  217. }
  218. iy[0] += 4; iy[1] += 4;
  219. iy[2] += 4; iy[3] += 4;
  220. iu[0] += 2; iu[1] += 2;
  221. iv[0] += 2; iv[1] += 2;
  222. }
  223. }
  224. return 0;
  225. }
  226. static int cinepak_decode_strip (CinepakContext *s,
  227. cvid_strip *strip, const uint8_t *data, int size)
  228. {
  229. const uint8_t *eod = (data + size);
  230. int chunk_id, chunk_size;
  231. /* coordinate sanity checks */
  232. if (strip->x2 > s->width ||
  233. strip->y2 > s->height ||
  234. strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
  235. return AVERROR_INVALIDDATA;
  236. while ((data + 4) <= eod) {
  237. chunk_id = data[0];
  238. chunk_size = AV_RB24 (&data[1]) - 4;
  239. if(chunk_size < 0)
  240. return AVERROR_INVALIDDATA;
  241. data += 4;
  242. chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
  243. switch (chunk_id) {
  244. case 0x20:
  245. case 0x21:
  246. case 0x24:
  247. case 0x25:
  248. cinepak_decode_codebook (strip->v4_codebook, chunk_id,
  249. chunk_size, data);
  250. break;
  251. case 0x22:
  252. case 0x23:
  253. case 0x26:
  254. case 0x27:
  255. cinepak_decode_codebook (strip->v1_codebook, chunk_id,
  256. chunk_size, data);
  257. break;
  258. case 0x30:
  259. case 0x31:
  260. case 0x32:
  261. return cinepak_decode_vectors (s, strip, chunk_id,
  262. chunk_size, data);
  263. }
  264. data += chunk_size;
  265. }
  266. return AVERROR_INVALIDDATA;
  267. }
  268. static int cinepak_decode (CinepakContext *s)
  269. {
  270. const uint8_t *eod = (s->data + s->size);
  271. int i, result, strip_size, frame_flags, num_strips;
  272. int y0 = 0;
  273. int encoded_buf_size;
  274. if (s->size < 10)
  275. return AVERROR_INVALIDDATA;
  276. frame_flags = s->data[0];
  277. num_strips = AV_RB16 (&s->data[8]);
  278. encoded_buf_size = AV_RB24(&s->data[1]);
  279. /* if this is the first frame, check for deviant Sega FILM data */
  280. if (s->sega_film_skip_bytes == -1) {
  281. if (!encoded_buf_size) {
  282. avpriv_request_sample(s->avctx, "encoded_buf_size 0");
  283. return AVERROR_PATCHWELCOME;
  284. }
  285. if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
  286. /* If the encoded frame size differs from the frame size as indicated
  287. * by the container file, this data likely comes from a Sega FILM/CPK file.
  288. * If the frame header is followed by the bytes FE 00 00 06 00 00 then
  289. * this is probably one of the two known files that have 6 extra bytes
  290. * after the frame header. Else, assume 2 extra bytes. The container
  291. * size also cannot be a multiple of the encoded size. */
  292. if (s->size >= 16 &&
  293. (s->data[10] == 0xFE) &&
  294. (s->data[11] == 0x00) &&
  295. (s->data[12] == 0x00) &&
  296. (s->data[13] == 0x06) &&
  297. (s->data[14] == 0x00) &&
  298. (s->data[15] == 0x00))
  299. s->sega_film_skip_bytes = 6;
  300. else
  301. s->sega_film_skip_bytes = 2;
  302. } else
  303. s->sega_film_skip_bytes = 0;
  304. }
  305. s->data += 10 + s->sega_film_skip_bytes;
  306. num_strips = FFMIN(num_strips, MAX_STRIPS);
  307. for (i=0; i < num_strips; i++) {
  308. if ((s->data + 12) > eod)
  309. return AVERROR_INVALIDDATA;
  310. s->strips[i].id = s->data[0];
  311. s->strips[i].y1 = y0;
  312. s->strips[i].x1 = 0;
  313. s->strips[i].y2 = y0 + AV_RB16 (&s->data[8]);
  314. s->strips[i].x2 = s->avctx->width;
  315. strip_size = AV_RB24 (&s->data[1]) - 12;
  316. if (strip_size < 0)
  317. return AVERROR_INVALIDDATA;
  318. s->data += 12;
  319. strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
  320. if ((i > 0) && !(frame_flags & 0x01)) {
  321. memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
  322. sizeof(s->strips[i].v4_codebook));
  323. memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
  324. sizeof(s->strips[i].v1_codebook));
  325. }
  326. result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
  327. if (result != 0)
  328. return result;
  329. s->data += strip_size;
  330. y0 = s->strips[i].y2;
  331. }
  332. return 0;
  333. }
  334. static av_cold int cinepak_decode_init(AVCodecContext *avctx)
  335. {
  336. CinepakContext *s = avctx->priv_data;
  337. s->avctx = avctx;
  338. s->width = (avctx->width + 3) & ~3;
  339. s->height = (avctx->height + 3) & ~3;
  340. s->sega_film_skip_bytes = -1; /* uninitialized state */
  341. // check for paletted data
  342. if (avctx->bits_per_coded_sample != 8) {
  343. s->palette_video = 0;
  344. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  345. } else {
  346. s->palette_video = 1;
  347. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  348. }
  349. s->frame = av_frame_alloc();
  350. if (!s->frame)
  351. return AVERROR(ENOMEM);
  352. return 0;
  353. }
  354. static int cinepak_decode_frame(AVCodecContext *avctx,
  355. void *data, int *got_frame,
  356. AVPacket *avpkt)
  357. {
  358. const uint8_t *buf = avpkt->data;
  359. int ret = 0, buf_size = avpkt->size;
  360. CinepakContext *s = avctx->priv_data;
  361. s->data = buf;
  362. s->size = buf_size;
  363. if ((ret = ff_reget_buffer(avctx, s->frame))) {
  364. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  365. return ret;
  366. }
  367. if (s->palette_video) {
  368. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  369. if (pal) {
  370. s->frame->palette_has_changed = 1;
  371. memcpy(s->pal, pal, AVPALETTE_SIZE);
  372. }
  373. }
  374. cinepak_decode(s);
  375. if (s->palette_video)
  376. memcpy (s->frame->data[1], s->pal, AVPALETTE_SIZE);
  377. if ((ret = av_frame_ref(data, s->frame)) < 0)
  378. return ret;
  379. *got_frame = 1;
  380. /* report that the buffer was completely consumed */
  381. return buf_size;
  382. }
  383. static av_cold int cinepak_decode_end(AVCodecContext *avctx)
  384. {
  385. CinepakContext *s = avctx->priv_data;
  386. av_frame_free(&s->frame);
  387. return 0;
  388. }
  389. AVCodec ff_cinepak_decoder = {
  390. .name = "cinepak",
  391. .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
  392. .type = AVMEDIA_TYPE_VIDEO,
  393. .id = AV_CODEC_ID_CINEPAK,
  394. .priv_data_size = sizeof(CinepakContext),
  395. .init = cinepak_decode_init,
  396. .close = cinepak_decode_end,
  397. .decode = cinepak_decode_frame,
  398. .capabilities = AV_CODEC_CAP_DR1,
  399. };