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.

470 lines
15KB

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