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.

382 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. avcodec_get_frame_defaults(&s->frame);
  61. ff_dsputil_init(&s->dsp, avctx);
  62. return 0;
  63. }
  64. /**
  65. * Comparator - our nodes should ascend by count
  66. * but with preserved symbol order
  67. */
  68. static int huff_cmp(const void *va, const void *vb)
  69. {
  70. const Node *a = va, *b = vb;
  71. return (a->count - b->count)*256 + a->sym - b->sym;
  72. }
  73. /**
  74. * decode Fraps v2 packed plane
  75. */
  76. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  77. int h, const uint8_t *src, int size, int Uoff,
  78. const int step)
  79. {
  80. int i, j, ret;
  81. GetBitContext gb;
  82. VLC vlc;
  83. Node nodes[512];
  84. for (i = 0; i < 256; i++)
  85. nodes[i].count = bytestream_get_le32(&src);
  86. size -= 1024;
  87. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
  88. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  89. return ret;
  90. /* we have built Huffman table and are ready to decode plane */
  91. /* convert bits so they may be used by standard bitreader */
  92. s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
  93. init_get_bits(&gb, s->tmpbuf, size * 8);
  94. for (j = 0; j < h; j++) {
  95. for (i = 0; i < w*step; i += step) {
  96. dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
  97. /* lines are stored as deltas between previous lines
  98. * and we need to add 0x80 to the first lines of chroma planes
  99. */
  100. if (j)
  101. dst[i] += dst[i - stride];
  102. else if (Uoff)
  103. dst[i] += 0x80;
  104. if (get_bits_left(&gb) < 0) {
  105. ff_free_vlc(&vlc);
  106. return AVERROR_INVALIDDATA;
  107. }
  108. }
  109. dst += stride;
  110. }
  111. ff_free_vlc(&vlc);
  112. return 0;
  113. }
  114. static int decode_frame(AVCodecContext *avctx,
  115. void *data, int *got_frame,
  116. AVPacket *avpkt)
  117. {
  118. FrapsContext * const s = avctx->priv_data;
  119. const uint8_t *buf = avpkt->data;
  120. int buf_size = avpkt->size;
  121. AVFrame *frame = data;
  122. AVFrame * const f = &s->frame;
  123. uint32_t header;
  124. unsigned int version,header_size;
  125. unsigned int x, y;
  126. const uint32_t *buf32;
  127. uint32_t *luma1,*luma2,*cb,*cr;
  128. uint32_t offs[4];
  129. int i, j, ret, is_chroma, planes;
  130. enum AVPixelFormat pix_fmt;
  131. int prev_pic_bit, expected_size;
  132. if (buf_size < 4) {
  133. av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. header = AV_RL32(buf);
  137. version = header & 0xff;
  138. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  139. prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
  140. if (version > 5) {
  141. av_log(avctx, AV_LOG_ERROR,
  142. "This file is encoded with Fraps version %d. " \
  143. "This codec can only decode versions <= 5.\n", version);
  144. return AVERROR_PATCHWELCOME;
  145. }
  146. buf += 4;
  147. if (header_size == 8)
  148. buf += 4;
  149. pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  150. if (avctx->pix_fmt != pix_fmt && f->data[0]) {
  151. av_frame_unref(f);
  152. }
  153. avctx->pix_fmt = pix_fmt;
  154. expected_size = header_size;
  155. switch (version) {
  156. case 0:
  157. default:
  158. /* Fraps v0 is a reordered YUV420 */
  159. if (!prev_pic_bit)
  160. expected_size += avctx->width * avctx->height * 3 / 2;
  161. if (buf_size != expected_size) {
  162. av_log(avctx, AV_LOG_ERROR,
  163. "Invalid frame length %d (should be %d)\n",
  164. buf_size, expected_size);
  165. return AVERROR_INVALIDDATA;
  166. }
  167. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  168. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  169. avctx->width, avctx->height);
  170. return AVERROR_INVALIDDATA;
  171. }
  172. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  173. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  174. return ret;
  175. }
  176. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  177. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  178. if (f->pict_type == AV_PICTURE_TYPE_I) {
  179. buf32 = (const uint32_t*)buf;
  180. for (y = 0; y < avctx->height / 2; y++) {
  181. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
  182. luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
  183. cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
  184. cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
  185. for (x = 0; x < avctx->width; x += 8) {
  186. *(luma1++) = *(buf32++);
  187. *(luma1++) = *(buf32++);
  188. *(luma2++) = *(buf32++);
  189. *(luma2++) = *(buf32++);
  190. *(cr++) = *(buf32++);
  191. *(cb++) = *(buf32++);
  192. }
  193. }
  194. }
  195. break;
  196. case 1:
  197. /* Fraps v1 is an upside-down BGR24 */
  198. if (!prev_pic_bit)
  199. expected_size += avctx->width * avctx->height * 3;
  200. if (buf_size != expected_size) {
  201. av_log(avctx, AV_LOG_ERROR,
  202. "Invalid frame length %d (should be %d)\n",
  203. buf_size, expected_size);
  204. return AVERROR_INVALIDDATA;
  205. }
  206. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  208. return ret;
  209. }
  210. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  211. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  212. if (f->pict_type == AV_PICTURE_TYPE_I) {
  213. for (y = 0; y<avctx->height; y++)
  214. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  215. &buf[y * avctx->width * 3],
  216. 3 * avctx->width);
  217. }
  218. break;
  219. case 2:
  220. case 4:
  221. /**
  222. * Fraps v2 is Huffman-coded YUV420 planes
  223. * Fraps v4 is virtually the same
  224. */
  225. planes = 3;
  226. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  227. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  228. return ret;
  229. }
  230. /* skip frame */
  231. if (buf_size == 8) {
  232. f->pict_type = AV_PICTURE_TYPE_P;
  233. f->key_frame = 0;
  234. break;
  235. }
  236. f->pict_type = AV_PICTURE_TYPE_I;
  237. f->key_frame = 1;
  238. if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
  239. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  240. return AVERROR_INVALIDDATA;
  241. }
  242. for (i = 0; i < planes; i++) {
  243. offs[i] = AV_RL32(buf + 4 + i * 4);
  244. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  245. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  246. return AVERROR_INVALIDDATA;
  247. }
  248. }
  249. offs[planes] = buf_size;
  250. for (i = 0; i < planes; i++) {
  251. is_chroma = !!i;
  252. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  253. offs[i + 1] - offs[i] - 1024);
  254. if (!s->tmpbuf)
  255. return AVERROR(ENOMEM);
  256. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  257. avctx->width >> is_chroma,
  258. avctx->height >> is_chroma,
  259. buf + offs[i], offs[i + 1] - offs[i],
  260. is_chroma, 1)) < 0) {
  261. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  262. return ret;
  263. }
  264. }
  265. break;
  266. case 3:
  267. case 5:
  268. /* Virtually the same as version 4, but is for RGB24 */
  269. planes = 3;
  270. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  271. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  272. return ret;
  273. }
  274. /* skip frame */
  275. if (buf_size == 8) {
  276. f->pict_type = AV_PICTURE_TYPE_P;
  277. f->key_frame = 0;
  278. break;
  279. }
  280. f->pict_type = AV_PICTURE_TYPE_I;
  281. f->key_frame = 1;
  282. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  283. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  284. return AVERROR_INVALIDDATA;
  285. }
  286. for (i = 0; i < planes; i++) {
  287. offs[i] = AV_RL32(buf + 4 + i * 4);
  288. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  289. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  290. return AVERROR_INVALIDDATA;
  291. }
  292. }
  293. offs[planes] = buf_size;
  294. for (i = 0; i < planes; i++) {
  295. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  296. offs[i + 1] - offs[i] - 1024);
  297. if (!s->tmpbuf)
  298. return AVERROR(ENOMEM);
  299. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  300. -f->linesize[0], avctx->width, avctx->height,
  301. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  302. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  303. return ret;
  304. }
  305. }
  306. // convert pseudo-YUV into real RGB
  307. for (j = 0; j < avctx->height; j++) {
  308. for (i = 0; i < avctx->width; i++) {
  309. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  310. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  311. }
  312. }
  313. break;
  314. }
  315. if ((ret = av_frame_ref(frame, f)) < 0)
  316. return ret;
  317. *got_frame = 1;
  318. return buf_size;
  319. }
  320. /**
  321. * closes decoder
  322. * @param avctx codec context
  323. * @return 0 on success or negative if fails
  324. */
  325. static av_cold int decode_end(AVCodecContext *avctx)
  326. {
  327. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  328. av_frame_unref(&s->frame);
  329. av_freep(&s->tmpbuf);
  330. return 0;
  331. }
  332. AVCodec ff_fraps_decoder = {
  333. .name = "fraps",
  334. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .id = AV_CODEC_ID_FRAPS,
  337. .priv_data_size = sizeof(FrapsContext),
  338. .init = decode_init,
  339. .close = decode_end,
  340. .decode = decode_frame,
  341. .capabilities = CODEC_CAP_DR1,
  342. };