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.

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