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.

386 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. avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
  157. : AVCOL_RANGE_JPEG;
  158. expected_size = header_size;
  159. switch (version) {
  160. case 0:
  161. default:
  162. /* Fraps v0 is a reordered YUV420 */
  163. if (!prev_pic_bit)
  164. expected_size += avctx->width * avctx->height * 3 / 2;
  165. if (buf_size != expected_size) {
  166. av_log(avctx, AV_LOG_ERROR,
  167. "Invalid frame length %d (should be %d)\n",
  168. buf_size, expected_size);
  169. return AVERROR_INVALIDDATA;
  170. }
  171. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  172. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  173. avctx->width, avctx->height);
  174. return AVERROR_INVALIDDATA;
  175. }
  176. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  177. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  178. return ret;
  179. }
  180. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  181. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  182. if (f->pict_type == AV_PICTURE_TYPE_I) {
  183. buf32 = (const uint32_t*)buf;
  184. for (y = 0; y < avctx->height / 2; y++) {
  185. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
  186. luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
  187. cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
  188. cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
  189. for (x = 0; x < avctx->width; x += 8) {
  190. *(luma1++) = *(buf32++);
  191. *(luma1++) = *(buf32++);
  192. *(luma2++) = *(buf32++);
  193. *(luma2++) = *(buf32++);
  194. *(cr++) = *(buf32++);
  195. *(cb++) = *(buf32++);
  196. }
  197. }
  198. }
  199. break;
  200. case 1:
  201. /* Fraps v1 is an upside-down BGR24 */
  202. if (!prev_pic_bit)
  203. expected_size += avctx->width * avctx->height * 3;
  204. if (buf_size != expected_size) {
  205. av_log(avctx, AV_LOG_ERROR,
  206. "Invalid frame length %d (should be %d)\n",
  207. buf_size, expected_size);
  208. return AVERROR_INVALIDDATA;
  209. }
  210. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  211. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  212. return ret;
  213. }
  214. f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  215. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  216. if (f->pict_type == AV_PICTURE_TYPE_I) {
  217. for (y = 0; y<avctx->height; y++)
  218. memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
  219. &buf[y * avctx->width * 3],
  220. 3 * avctx->width);
  221. }
  222. break;
  223. case 2:
  224. case 4:
  225. /**
  226. * Fraps v2 is Huffman-coded YUV420 planes
  227. * Fraps v4 is virtually the same
  228. */
  229. planes = 3;
  230. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  231. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  232. return ret;
  233. }
  234. /* skip frame */
  235. if (buf_size == 8) {
  236. f->pict_type = AV_PICTURE_TYPE_P;
  237. f->key_frame = 0;
  238. break;
  239. }
  240. f->pict_type = AV_PICTURE_TYPE_I;
  241. f->key_frame = 1;
  242. if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
  243. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  244. return AVERROR_INVALIDDATA;
  245. }
  246. for (i = 0; i < planes; i++) {
  247. offs[i] = AV_RL32(buf + 4 + i * 4);
  248. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  249. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  250. return AVERROR_INVALIDDATA;
  251. }
  252. }
  253. offs[planes] = buf_size;
  254. for (i = 0; i < planes; i++) {
  255. is_chroma = !!i;
  256. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  257. offs[i + 1] - offs[i] - 1024);
  258. if (!s->tmpbuf)
  259. return AVERROR(ENOMEM);
  260. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  261. avctx->width >> is_chroma,
  262. avctx->height >> is_chroma,
  263. buf + offs[i], offs[i + 1] - offs[i],
  264. is_chroma, 1)) < 0) {
  265. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  266. return ret;
  267. }
  268. }
  269. break;
  270. case 3:
  271. case 5:
  272. /* Virtually the same as version 4, but is for RGB24 */
  273. planes = 3;
  274. if ((ret = ff_reget_buffer(avctx, f)) < 0) {
  275. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  276. return ret;
  277. }
  278. /* skip frame */
  279. if (buf_size == 8) {
  280. f->pict_type = AV_PICTURE_TYPE_P;
  281. f->key_frame = 0;
  282. break;
  283. }
  284. f->pict_type = AV_PICTURE_TYPE_I;
  285. f->key_frame = 1;
  286. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  287. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. for (i = 0; i < planes; i++) {
  291. offs[i] = AV_RL32(buf + 4 + i * 4);
  292. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  293. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  294. return AVERROR_INVALIDDATA;
  295. }
  296. }
  297. offs[planes] = buf_size;
  298. for (i = 0; i < planes; i++) {
  299. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  300. offs[i + 1] - offs[i] - 1024);
  301. if (!s->tmpbuf)
  302. return AVERROR(ENOMEM);
  303. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  304. -f->linesize[0], avctx->width, avctx->height,
  305. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  306. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  307. return ret;
  308. }
  309. }
  310. // convert pseudo-YUV into real RGB
  311. for (j = 0; j < avctx->height; j++) {
  312. for (i = 0; i < avctx->width; i++) {
  313. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  314. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  315. }
  316. }
  317. break;
  318. }
  319. if ((ret = av_frame_ref(frame, f)) < 0)
  320. return ret;
  321. *got_frame = 1;
  322. return buf_size;
  323. }
  324. /**
  325. * closes decoder
  326. * @param avctx codec context
  327. * @return 0 on success or negative if fails
  328. */
  329. static av_cold int decode_end(AVCodecContext *avctx)
  330. {
  331. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  332. av_frame_free(&s->frame);
  333. av_freep(&s->tmpbuf);
  334. return 0;
  335. }
  336. AVCodec ff_fraps_decoder = {
  337. .name = "fraps",
  338. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .id = AV_CODEC_ID_FRAPS,
  341. .priv_data_size = sizeof(FrapsContext),
  342. .init = decode_init,
  343. .close = decode_end,
  344. .decode = decode_frame,
  345. .capabilities = CODEC_CAP_DR1,
  346. };