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.

384 lines
12KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Lossless Fraps 'FPS1' decoder
  25. * @author Roine Gustafsson (roine at users sf net)
  26. * @author Konstantin Shishkov
  27. *
  28. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  29. *
  30. * Version 2 files support by Konstantin Shishkov
  31. */
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "huffman.h"
  35. #include "bytestream.h"
  36. #include "dsputil.h"
  37. #include "internal.h"
  38. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  39. /**
  40. * local variable storage
  41. */
  42. typedef struct FrapsContext {
  43. AVCodecContext *avctx;
  44. AVFrame *frame;
  45. uint8_t *tmpbuf;
  46. int tmpbuf_size;
  47. DSPContext dsp;
  48. } FrapsContext;
  49. /**
  50. * initializes decoder
  51. * @param avctx codec context
  52. * @return 0 on success or negative if fails
  53. */
  54. static av_cold int decode_init(AVCodecContext *avctx)
  55. {
  56. FrapsContext * const s = avctx->priv_data;
  57. avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
  58. s->avctx = avctx;
  59. s->tmpbuf = NULL;
  60. s->frame = av_frame_alloc();
  61. if (!s->frame)
  62. return AVERROR(ENOMEM);
  63. ff_dsputil_init(&s->dsp, avctx);
  64. return 0;
  65. }
  66. /**
  67. * Comparator - our nodes should ascend by count
  68. * but with preserved symbol order
  69. */
  70. static int huff_cmp(const void *va, const void *vb)
  71. {
  72. const Node *a = va, *b = vb;
  73. return (a->count - b->count)*256 + a->sym - b->sym;
  74. }
  75. /**
  76. * decode Fraps v2 packed plane
  77. */
  78. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  79. int h, const uint8_t *src, int size, int Uoff,
  80. const int step)
  81. {
  82. int i, j, ret;
  83. GetBitContext gb;
  84. VLC vlc;
  85. Node nodes[512];
  86. for (i = 0; i < 256; i++)
  87. nodes[i].count = bytestream_get_le32(&src);
  88. size -= 1024;
  89. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
  90. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  91. return ret;
  92. /* we have built Huffman table and are ready to decode plane */
  93. /* convert bits so they may be used by standard bitreader */
  94. s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
  95. init_get_bits(&gb, s->tmpbuf, size * 8);
  96. for (j = 0; j < h; j++) {
  97. for (i = 0; i < w*step; i += step) {
  98. dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
  99. /* lines are stored as deltas between previous lines
  100. * and we need to add 0x80 to the first lines of chroma planes
  101. */
  102. if (j)
  103. dst[i] += dst[i - stride];
  104. else if (Uoff)
  105. dst[i] += 0x80;
  106. if (get_bits_left(&gb) < 0) {
  107. ff_free_vlc(&vlc);
  108. return AVERROR_INVALIDDATA;
  109. }
  110. }
  111. dst += stride;
  112. }
  113. ff_free_vlc(&vlc);
  114. return 0;
  115. }
  116. static int decode_frame(AVCodecContext *avctx,
  117. void *data, int *got_frame,
  118. AVPacket *avpkt)
  119. {
  120. FrapsContext * const s = avctx->priv_data;
  121. const uint8_t *buf = avpkt->data;
  122. int buf_size = avpkt->size;
  123. AVFrame *frame = data;
  124. AVFrame * const f = s->frame;
  125. uint32_t header;
  126. unsigned int version,header_size;
  127. unsigned int x, y;
  128. const uint32_t *buf32;
  129. uint32_t *luma1,*luma2,*cb,*cr;
  130. uint32_t offs[4];
  131. int i, j, ret, is_chroma, planes;
  132. enum AVPixelFormat pix_fmt;
  133. int prev_pic_bit, expected_size;
  134. if (buf_size < 4) {
  135. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  136. return AVERROR_INVALIDDATA;
  137. }
  138. header = AV_RL32(buf);
  139. version = header & 0xff;
  140. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  141. prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
  142. if (version > 5) {
  143. av_log(avctx, AV_LOG_ERROR,
  144. "This file is encoded with Fraps version %d. " \
  145. "This codec can only decode versions <= 5.\n", version);
  146. return AVERROR_PATCHWELCOME;
  147. }
  148. buf += 4;
  149. if (header_size == 8)
  150. buf += 4;
  151. pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  152. if (avctx->pix_fmt != pix_fmt && f->data[0]) {
  153. av_frame_unref(f);
  154. }
  155. avctx->pix_fmt = pix_fmt;
  156. expected_size = header_size;
  157. switch (version) {
  158. case 0:
  159. default:
  160. /* Fraps v0 is a reordered YUV420 */
  161. if (!prev_pic_bit)
  162. expected_size += avctx->width * avctx->height * 3 / 2;
  163. if (buf_size != expected_size) {
  164. av_log(avctx, AV_LOG_ERROR,
  165. "Invalid frame length %d (should be %d)\n",
  166. buf_size, expected_size);
  167. return AVERROR_INVALIDDATA;
  168. }
  169. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  170. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  171. avctx->width, avctx->height);
  172. return AVERROR_INVALIDDATA;
  173. }
  174. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  175. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  176. return ret;
  177. }
  178. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  179. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  180. if (f->pict_type == AV_PICTURE_TYPE_I) {
  181. buf32 = (const uint32_t*)buf;
  182. for (y = 0; y < avctx->height / 2; y++) {
  183. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
  184. luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
  185. cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
  186. cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
  187. for (x = 0; x < avctx->width; x += 8) {
  188. *(luma1++) = *(buf32++);
  189. *(luma1++) = *(buf32++);
  190. *(luma2++) = *(buf32++);
  191. *(luma2++) = *(buf32++);
  192. *(cr++) = *(buf32++);
  193. *(cb++) = *(buf32++);
  194. }
  195. }
  196. }
  197. break;
  198. case 1:
  199. /* Fraps v1 is an upside-down BGR24 */
  200. if (!prev_pic_bit)
  201. expected_size += avctx->width * avctx->height * 3;
  202. if (buf_size != expected_size) {
  203. av_log(avctx, AV_LOG_ERROR,
  204. "Invalid frame length %d (should be %d)\n",
  205. buf_size, expected_size);
  206. return AVERROR_INVALIDDATA;
  207. }
  208. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  209. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  210. return ret;
  211. }
  212. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  213. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  214. if (f->pict_type == AV_PICTURE_TYPE_I) {
  215. for (y = 0; y<avctx->height; y++)
  216. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  217. &buf[y * avctx->width * 3],
  218. 3 * avctx->width);
  219. }
  220. break;
  221. case 2:
  222. case 4:
  223. /**
  224. * Fraps v2 is Huffman-coded YUV420 planes
  225. * Fraps v4 is virtually the same
  226. */
  227. planes = 3;
  228. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  229. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  230. return ret;
  231. }
  232. /* skip frame */
  233. if (buf_size == 8) {
  234. f->pict_type = AV_PICTURE_TYPE_P;
  235. f->key_frame = 0;
  236. break;
  237. }
  238. f->pict_type = AV_PICTURE_TYPE_I;
  239. f->key_frame = 1;
  240. if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
  241. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. for (i = 0; i < planes; i++) {
  245. offs[i] = AV_RL32(buf + 4 + i * 4);
  246. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  247. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  248. return AVERROR_INVALIDDATA;
  249. }
  250. }
  251. offs[planes] = buf_size;
  252. for (i = 0; i < planes; i++) {
  253. is_chroma = !!i;
  254. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  255. offs[i + 1] - offs[i] - 1024);
  256. if (!s->tmpbuf)
  257. return AVERROR(ENOMEM);
  258. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  259. avctx->width >> is_chroma,
  260. avctx->height >> is_chroma,
  261. buf + offs[i], offs[i + 1] - offs[i],
  262. is_chroma, 1)) < 0) {
  263. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  264. return ret;
  265. }
  266. }
  267. break;
  268. case 3:
  269. case 5:
  270. /* Virtually the same as version 4, but is for RGB24 */
  271. planes = 3;
  272. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  273. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  274. return ret;
  275. }
  276. /* skip frame */
  277. if (buf_size == 8) {
  278. f->pict_type = AV_PICTURE_TYPE_P;
  279. f->key_frame = 0;
  280. break;
  281. }
  282. f->pict_type = AV_PICTURE_TYPE_I;
  283. f->key_frame = 1;
  284. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  285. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  286. return AVERROR_INVALIDDATA;
  287. }
  288. for (i = 0; i < planes; i++) {
  289. offs[i] = AV_RL32(buf + 4 + i * 4);
  290. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  291. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  292. return AVERROR_INVALIDDATA;
  293. }
  294. }
  295. offs[planes] = buf_size;
  296. for (i = 0; i < planes; i++) {
  297. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  298. offs[i + 1] - offs[i] - 1024);
  299. if (!s->tmpbuf)
  300. return AVERROR(ENOMEM);
  301. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  302. -f->linesize[0], avctx->width, avctx->height,
  303. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  304. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  305. return ret;
  306. }
  307. }
  308. // convert pseudo-YUV into real RGB
  309. for (j = 0; j < avctx->height; j++) {
  310. for (i = 0; i < avctx->width; i++) {
  311. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  312. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  313. }
  314. }
  315. break;
  316. }
  317. if ((ret = av_frame_ref(frame, f)) < 0)
  318. return ret;
  319. *got_frame = 1;
  320. return buf_size;
  321. }
  322. /**
  323. * closes decoder
  324. * @param avctx codec context
  325. * @return 0 on success or negative if fails
  326. */
  327. static av_cold int decode_end(AVCodecContext *avctx)
  328. {
  329. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  330. av_frame_free(&s->frame);
  331. av_freep(&s->tmpbuf);
  332. return 0;
  333. }
  334. AVCodec ff_fraps_decoder = {
  335. .name = "fraps",
  336. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. .id = AV_CODEC_ID_FRAPS,
  339. .priv_data_size = sizeof(FrapsContext),
  340. .init = decode_init,
  341. .close = decode_end,
  342. .decode = decode_frame,
  343. .capabilities = CODEC_CAP_DR1,
  344. };