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.

491 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
  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. * Cinepak colorspace support (c) 2013 Rl, Aetey Global Technologies AB
  32. * @author Cinepak colorspace, Rl, Aetey Global Technologies AB
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "libavutil/common.h"
  38. #include "libavutil/intreadwrite.h"
  39. #include "avcodec.h"
  40. typedef uint8_t cvid_codebook[12];
  41. #define MAX_STRIPS 32
  42. typedef struct {
  43. uint16_t id;
  44. uint16_t x1, y1;
  45. uint16_t x2, y2;
  46. cvid_codebook v4_codebook[256];
  47. cvid_codebook v1_codebook[256];
  48. } cvid_strip;
  49. typedef struct CinepakContext {
  50. AVCodecContext *avctx;
  51. AVFrame frame;
  52. const unsigned char *data;
  53. int size;
  54. int width, height;
  55. int palette_video;
  56. cvid_strip strips[MAX_STRIPS];
  57. int sega_film_skip_bytes;
  58. uint32_t pal[256];
  59. } CinepakContext;
  60. static void cinepak_decode_codebook (cvid_codebook *codebook,
  61. int chunk_id, int size, const uint8_t *data)
  62. {
  63. const uint8_t *eod = (data + size);
  64. uint32_t flag, mask;
  65. int i, n;
  66. uint8_t *p;
  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. p = codebook[0];
  72. for (i=0; i < 256; i++) {
  73. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  74. if ((data + 4) > eod)
  75. break;
  76. flag = AV_RB32 (data);
  77. data += 4;
  78. mask = 0x80000000;
  79. }
  80. if (!(chunk_id & 0x01) || (flag & mask)) {
  81. int k, kk;
  82. if ((data + n) > eod)
  83. break;
  84. for (k = 0; k < 4; ++k) {
  85. int r = *data++;
  86. for (kk = 0; kk < 3; ++kk)
  87. *p++ = r;
  88. }
  89. if (n == 6) {
  90. int r, g, b, u, v;
  91. u = *(int8_t *)data++;
  92. v = *(int8_t *)data++;
  93. p -= 12;
  94. for(k=0; k<4; ++k) {
  95. r = *p++ + v*2;
  96. g = *p++ - (u/2) - v;
  97. b = *p + u*2;
  98. p -= 2;
  99. *p++ = av_clip_uint8(r);
  100. *p++ = av_clip_uint8(g);
  101. *p++ = av_clip_uint8(b);
  102. }
  103. }
  104. } else {
  105. p += 12;
  106. }
  107. }
  108. }
  109. static int cinepak_decode_vectors (CinepakContext *s, cvid_strip *strip,
  110. int chunk_id, int size, const uint8_t *data)
  111. {
  112. const uint8_t *eod = (data + size);
  113. uint32_t flag, mask;
  114. uint8_t *cb0, *cb1, *cb2, *cb3;
  115. unsigned int x, y;
  116. char *ip0, *ip1, *ip2, *ip3;
  117. flag = 0;
  118. mask = 0;
  119. for (y=strip->y1; y < strip->y2; y+=4) {
  120. /* take care of y dimension not being multiple of 4, such streams exist */
  121. ip0 = ip1 = ip2 = ip3 = s->frame.data[0] +
  122. (s->palette_video?strip->x1:strip->x1*3) + (y * s->frame.linesize[0]);
  123. if(s->avctx->height - y > 1) {
  124. ip1 = ip0 + s->frame.linesize[0];
  125. if(s->avctx->height - y > 2) {
  126. ip2 = ip1 + s->frame.linesize[0];
  127. if(s->avctx->height - y > 3) {
  128. ip3 = ip2 + s->frame.linesize[0];
  129. }
  130. }
  131. }
  132. /* to get the correct picture for not-multiple-of-4 cases let us fill
  133. * each block from the bottom up, thus possibly overwriting the top line
  134. * more than once but ending with the correct data in place
  135. * (instead of in-loop checking) */
  136. for (x=strip->x1; x < strip->x2; x+=4) {
  137. if ((chunk_id & 0x01) && !(mask >>= 1)) {
  138. if ((data + 4) > eod)
  139. return AVERROR_INVALIDDATA;
  140. flag = AV_RB32 (data);
  141. data += 4;
  142. mask = 0x80000000;
  143. }
  144. if (!(chunk_id & 0x01) || (flag & mask)) {
  145. if (!(chunk_id & 0x02) && !(mask >>= 1)) {
  146. if ((data + 4) > eod)
  147. return AVERROR_INVALIDDATA;
  148. flag = AV_RB32 (data);
  149. data += 4;
  150. mask = 0x80000000;
  151. }
  152. if ((chunk_id & 0x02) || (~flag & mask)) {
  153. uint8_t *p;
  154. if (data >= eod)
  155. return AVERROR_INVALIDDATA;
  156. p = strip->v1_codebook[*data++];
  157. if (s->palette_video) {
  158. ip3[0] = ip3[1] = ip2[0] = ip2[1] = p[6];
  159. ip3[2] = ip3[3] = ip2[2] = ip2[3] = p[9];
  160. ip1[0] = ip1[1] = ip0[0] = ip0[1] = p[0];
  161. ip1[2] = ip1[3] = ip0[2] = ip0[3] = p[3];
  162. } else {
  163. p += 6;
  164. memcpy(ip3 + 0, p, 3); memcpy(ip3 + 3, p, 3);
  165. memcpy(ip2 + 0, p, 3); memcpy(ip2 + 3, p, 3);
  166. p += 3; /* ... + 9 */
  167. memcpy(ip3 + 6, p, 3); memcpy(ip3 + 9, p, 3);
  168. memcpy(ip2 + 6, p, 3); memcpy(ip2 + 9, p, 3);
  169. p -= 9; /* ... + 0 */
  170. memcpy(ip1 + 0, p, 3); memcpy(ip1 + 3, p, 3);
  171. memcpy(ip0 + 0, p, 3); memcpy(ip0 + 3, p, 3);
  172. p += 3; /* ... + 3 */
  173. memcpy(ip1 + 6, p, 3); memcpy(ip1 + 9, p, 3);
  174. memcpy(ip0 + 6, p, 3); memcpy(ip0 + 9, p, 3);
  175. }
  176. } else if (flag & mask) {
  177. if ((data + 4) > eod)
  178. return AVERROR_INVALIDDATA;
  179. cb0 = strip->v4_codebook[*data++];
  180. cb1 = strip->v4_codebook[*data++];
  181. cb2 = strip->v4_codebook[*data++];
  182. cb3 = strip->v4_codebook[*data++];
  183. if (s->palette_video) {
  184. uint8_t *p;
  185. p = ip3;
  186. *p++ = cb2[6];
  187. *p++ = cb2[9];
  188. *p++ = cb3[6];
  189. *p = cb3[9];
  190. p = ip2;
  191. *p++ = cb2[0];
  192. *p++ = cb2[3];
  193. *p++ = cb3[0];
  194. *p = cb3[3];
  195. p = ip1;
  196. *p++ = cb0[6];
  197. *p++ = cb0[9];
  198. *p++ = cb1[6];
  199. *p = cb1[9];
  200. p = ip0;
  201. *p++ = cb0[0];
  202. *p++ = cb0[3];
  203. *p++ = cb1[0];
  204. *p = cb1[3];
  205. } else {
  206. memcpy(ip3 + 0, cb2 + 6, 6);
  207. memcpy(ip3 + 6, cb3 + 6, 6);
  208. memcpy(ip2 + 0, cb2 + 0, 6);
  209. memcpy(ip2 + 6, cb3 + 0, 6);
  210. memcpy(ip1 + 0, cb0 + 6, 6);
  211. memcpy(ip1 + 6, cb1 + 6, 6);
  212. memcpy(ip0 + 0, cb0 + 0, 6);
  213. memcpy(ip0 + 6, cb1 + 0, 6);
  214. }
  215. }
  216. }
  217. if (s->palette_video) {
  218. ip0 += 4; ip1 += 4;
  219. ip2 += 4; ip3 += 4;
  220. } else {
  221. ip0 += 12; ip1 += 12;
  222. ip2 += 12; ip3 += 12;
  223. }
  224. }
  225. }
  226. return 0;
  227. }
  228. static int cinepak_decode_strip (CinepakContext *s,
  229. cvid_strip *strip, const uint8_t *data, int size)
  230. {
  231. const uint8_t *eod = (data + size);
  232. int chunk_id, chunk_size;
  233. /* coordinate sanity checks */
  234. if (strip->x2 > s->width ||
  235. strip->y2 > s->height ||
  236. strip->x1 >= strip->x2 || strip->y1 >= strip->y2)
  237. return AVERROR_INVALIDDATA;
  238. while ((data + 4) <= eod) {
  239. chunk_id = data[0];
  240. chunk_size = AV_RB24 (&data[1]) - 4;
  241. if(chunk_size < 0)
  242. return AVERROR_INVALIDDATA;
  243. data += 4;
  244. chunk_size = ((data + chunk_size) > eod) ? (eod - data) : chunk_size;
  245. switch (chunk_id) {
  246. case 0x20:
  247. case 0x21:
  248. case 0x24:
  249. case 0x25:
  250. cinepak_decode_codebook (strip->v4_codebook, chunk_id,
  251. chunk_size, data);
  252. break;
  253. case 0x22:
  254. case 0x23:
  255. case 0x26:
  256. case 0x27:
  257. cinepak_decode_codebook (strip->v1_codebook, chunk_id,
  258. chunk_size, data);
  259. break;
  260. case 0x30:
  261. case 0x31:
  262. case 0x32:
  263. return cinepak_decode_vectors (s, strip, chunk_id,
  264. chunk_size, data);
  265. }
  266. data += chunk_size;
  267. }
  268. return AVERROR_INVALIDDATA;
  269. }
  270. static int cinepak_decode (CinepakContext *s)
  271. {
  272. const uint8_t *eod = (s->data + s->size);
  273. int i, result, strip_size, frame_flags, num_strips;
  274. int y0 = 0;
  275. int encoded_buf_size;
  276. if (s->size < 10)
  277. return AVERROR_INVALIDDATA;
  278. frame_flags = s->data[0];
  279. num_strips = AV_RB16 (&s->data[8]);
  280. encoded_buf_size = AV_RB24(&s->data[1]);
  281. /* if this is the first frame, check for deviant Sega FILM data */
  282. if (s->sega_film_skip_bytes == -1) {
  283. if (!encoded_buf_size) {
  284. av_log_ask_for_sample(s->avctx, "encoded_buf_size is 0");
  285. return AVERROR_PATCHWELCOME;
  286. }
  287. if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
  288. /* If the encoded frame size differs from the frame size as indicated
  289. * by the container file, this data likely comes from a Sega FILM/CPK file.
  290. * If the frame header is followed by the bytes FE 00 00 06 00 00 then
  291. * this is probably one of the two known files that have 6 extra bytes
  292. * after the frame header. Else, assume 2 extra bytes. The container
  293. * size also cannot be a multiple of the encoded size. */
  294. if (s->size >= 16 &&
  295. (s->data[10] == 0xFE) &&
  296. (s->data[11] == 0x00) &&
  297. (s->data[12] == 0x00) &&
  298. (s->data[13] == 0x06) &&
  299. (s->data[14] == 0x00) &&
  300. (s->data[15] == 0x00))
  301. s->sega_film_skip_bytes = 6;
  302. else
  303. s->sega_film_skip_bytes = 2;
  304. } else
  305. s->sega_film_skip_bytes = 0;
  306. }
  307. s->data += 10 + s->sega_film_skip_bytes;
  308. num_strips = FFMIN(num_strips, MAX_STRIPS);
  309. s->frame.key_frame = 0;
  310. for (i=0; i < num_strips; i++) {
  311. if ((s->data + 12) > eod)
  312. return AVERROR_INVALIDDATA;
  313. s->strips[i].id = s->data[0];
  314. /* zero y1 means "relative to the previous stripe" */
  315. if (!(s->strips[i].y1 = AV_RB16 (&s->data[4])))
  316. s->strips[i].y2 = (s->strips[i].y1 = y0) + AV_RB16 (&s->data[8]);
  317. else
  318. s->strips[i].y2 = AV_RB16 (&s->data[8]);
  319. s->strips[i].x1 = AV_RB16 (&s->data[6]);
  320. s->strips[i].x2 = AV_RB16 (&s->data[10]);
  321. if (s->strips[i].id == 0x10)
  322. s->frame.key_frame = 1;
  323. strip_size = AV_RB24 (&s->data[1]) - 12;
  324. if (strip_size < 0)
  325. return AVERROR_INVALIDDATA;
  326. s->data += 12;
  327. strip_size = ((s->data + strip_size) > eod) ? (eod - s->data) : strip_size;
  328. if ((i > 0) && !(frame_flags & 0x01)) {
  329. memcpy (s->strips[i].v4_codebook, s->strips[i-1].v4_codebook,
  330. sizeof(s->strips[i].v4_codebook));
  331. memcpy (s->strips[i].v1_codebook, s->strips[i-1].v1_codebook,
  332. sizeof(s->strips[i].v1_codebook));
  333. }
  334. result = cinepak_decode_strip (s, &s->strips[i], s->data, strip_size);
  335. if (result != 0)
  336. return result;
  337. s->data += strip_size;
  338. y0 = s->strips[i].y2;
  339. }
  340. return 0;
  341. }
  342. static av_cold int cinepak_decode_init(AVCodecContext *avctx)
  343. {
  344. CinepakContext *s = avctx->priv_data;
  345. s->avctx = avctx;
  346. s->width = (avctx->width + 3) & ~3;
  347. s->height = (avctx->height + 3) & ~3;
  348. s->sega_film_skip_bytes = -1; /* uninitialized state */
  349. // check for paletted data
  350. if (avctx->bits_per_coded_sample != 8) {
  351. s->palette_video = 0;
  352. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  353. } else {
  354. s->palette_video = 1;
  355. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  356. }
  357. avcodec_get_frame_defaults(&s->frame);
  358. s->frame.data[0] = NULL;
  359. return 0;
  360. }
  361. static int cinepak_decode_frame(AVCodecContext *avctx,
  362. void *data, int *got_frame,
  363. AVPacket *avpkt)
  364. {
  365. const uint8_t *buf = avpkt->data;
  366. int ret = 0, buf_size = avpkt->size;
  367. CinepakContext *s = avctx->priv_data;
  368. s->data = buf;
  369. s->size = buf_size;
  370. s->frame.reference = 3;
  371. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  372. FF_BUFFER_HINTS_REUSABLE;
  373. if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
  374. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  375. return ret;
  376. }
  377. if (s->palette_video) {
  378. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  379. if (pal) {
  380. s->frame.palette_has_changed = 1;
  381. memcpy(s->pal, pal, AVPALETTE_SIZE);
  382. }
  383. }
  384. if ((ret = cinepak_decode(s)) < 0) {
  385. av_log(avctx, AV_LOG_ERROR, "cinepak_decode failed\n");
  386. }
  387. if (s->palette_video)
  388. memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
  389. *got_frame = 1;
  390. *(AVFrame*)data = s->frame;
  391. /* report that the buffer was completely consumed */
  392. return buf_size;
  393. }
  394. static av_cold int cinepak_decode_end(AVCodecContext *avctx)
  395. {
  396. CinepakContext *s = avctx->priv_data;
  397. if (s->frame.data[0])
  398. avctx->release_buffer(avctx, &s->frame);
  399. return 0;
  400. }
  401. AVCodec ff_cinepak_decoder = {
  402. .name = "cinepak",
  403. .type = AVMEDIA_TYPE_VIDEO,
  404. .id = AV_CODEC_ID_CINEPAK,
  405. .priv_data_size = sizeof(CinepakContext),
  406. .init = cinepak_decode_init,
  407. .close = cinepak_decode_end,
  408. .decode = cinepak_decode_frame,
  409. .capabilities = CODEC_CAP_DR1,
  410. .long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
  411. };