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.

378 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. if(get_bits_left(&gb) < 0){
  101. free_vlc(&vlc);
  102. return -1;
  103. }
  104. }
  105. dst += stride;
  106. }
  107. free_vlc(&vlc);
  108. return 0;
  109. }
  110. static int decode_frame(AVCodecContext *avctx,
  111. void *data, int *data_size,
  112. AVPacket *avpkt)
  113. {
  114. const uint8_t *buf = avpkt->data;
  115. int buf_size = avpkt->size;
  116. FrapsContext * const s = avctx->priv_data;
  117. AVFrame *frame = data;
  118. AVFrame * const f = (AVFrame*)&s->frame;
  119. uint32_t header;
  120. unsigned int version,header_size;
  121. unsigned int x, y;
  122. const uint32_t *buf32;
  123. uint32_t *luma1,*luma2,*cb,*cr;
  124. uint32_t offs[4];
  125. int i, j, is_chroma, planes;
  126. header = AV_RL32(buf);
  127. version = header & 0xff;
  128. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  129. if (version > 5) {
  130. av_log(avctx, AV_LOG_ERROR,
  131. "This file is encoded with Fraps version %d. " \
  132. "This codec can only decode versions <= 5.\n", version);
  133. return -1;
  134. }
  135. buf+=4;
  136. if (header_size == 8)
  137. buf+=4;
  138. switch(version) {
  139. case 0:
  140. default:
  141. /* Fraps v0 is a reordered YUV420 */
  142. avctx->pix_fmt = PIX_FMT_YUVJ420P;
  143. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  144. (buf_size != header_size) ) {
  145. av_log(avctx, AV_LOG_ERROR,
  146. "Invalid frame length %d (should be %d)\n",
  147. buf_size, avctx->width*avctx->height*3/2+header_size);
  148. return -1;
  149. }
  150. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  151. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  152. avctx->width, avctx->height);
  153. return -1;
  154. }
  155. f->reference = 3;
  156. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  157. FF_BUFFER_HINTS_PRESERVE |
  158. FF_BUFFER_HINTS_REUSABLE;
  159. if (avctx->reget_buffer(avctx, f)) {
  160. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  161. return -1;
  162. }
  163. /* bit 31 means same as previous pic */
  164. f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  165. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  166. if (f->pict_type == AV_PICTURE_TYPE_I) {
  167. buf32=(const uint32_t*)buf;
  168. for(y=0; y<avctx->height/2; y++){
  169. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  170. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  171. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  172. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  173. for(x=0; x<avctx->width; x+=8){
  174. *(luma1++) = *(buf32++);
  175. *(luma1++) = *(buf32++);
  176. *(luma2++) = *(buf32++);
  177. *(luma2++) = *(buf32++);
  178. *(cr++) = *(buf32++);
  179. *(cb++) = *(buf32++);
  180. }
  181. }
  182. }
  183. break;
  184. case 1:
  185. /* Fraps v1 is an upside-down BGR24 */
  186. avctx->pix_fmt = PIX_FMT_BGR24;
  187. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  188. (buf_size != header_size) ) {
  189. av_log(avctx, AV_LOG_ERROR,
  190. "Invalid frame length %d (should be %d)\n",
  191. buf_size, avctx->width*avctx->height*3+header_size);
  192. return -1;
  193. }
  194. f->reference = 3;
  195. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  196. FF_BUFFER_HINTS_PRESERVE |
  197. FF_BUFFER_HINTS_REUSABLE;
  198. if (avctx->reget_buffer(avctx, f)) {
  199. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  200. return -1;
  201. }
  202. /* bit 31 means same as previous pic */
  203. f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  204. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  205. if (f->pict_type == AV_PICTURE_TYPE_I) {
  206. for(y=0; y<avctx->height; y++)
  207. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  208. &buf[y*avctx->width*3],
  209. 3*avctx->width);
  210. }
  211. break;
  212. case 2:
  213. case 4:
  214. /**
  215. * Fraps v2 is Huffman-coded YUV420 planes
  216. * Fraps v4 is virtually the same
  217. */
  218. avctx->pix_fmt = PIX_FMT_YUVJ420P;
  219. planes = 3;
  220. f->reference = 3;
  221. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  222. FF_BUFFER_HINTS_PRESERVE |
  223. FF_BUFFER_HINTS_REUSABLE;
  224. if (avctx->reget_buffer(avctx, f)) {
  225. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  226. return -1;
  227. }
  228. /* skip frame */
  229. if(buf_size == 8) {
  230. f->pict_type = AV_PICTURE_TYPE_P;
  231. f->key_frame = 0;
  232. break;
  233. }
  234. f->pict_type = AV_PICTURE_TYPE_I;
  235. f->key_frame = 1;
  236. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  237. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  238. return -1;
  239. }
  240. for(i = 0; i < planes; i++) {
  241. offs[i] = AV_RL32(buf + 4 + i * 4);
  242. if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  243. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  244. return -1;
  245. }
  246. }
  247. offs[planes] = buf_size;
  248. for(i = 0; i < planes; i++){
  249. is_chroma = !!i;
  250. av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
  251. if (!s->tmpbuf)
  252. return AVERROR(ENOMEM);
  253. if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
  254. avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
  255. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  256. return -1;
  257. }
  258. }
  259. break;
  260. case 3:
  261. case 5:
  262. /* Virtually the same as version 4, but is for RGB24 */
  263. avctx->pix_fmt = PIX_FMT_BGR24;
  264. planes = 3;
  265. f->reference = 3;
  266. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  267. FF_BUFFER_HINTS_PRESERVE |
  268. FF_BUFFER_HINTS_REUSABLE;
  269. if (avctx->reget_buffer(avctx, f)) {
  270. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  271. return -1;
  272. }
  273. /* skip frame */
  274. if(buf_size == 8) {
  275. f->pict_type = AV_PICTURE_TYPE_P;
  276. f->key_frame = 0;
  277. break;
  278. }
  279. f->pict_type = AV_PICTURE_TYPE_I;
  280. f->key_frame = 1;
  281. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  282. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  283. return -1;
  284. }
  285. for(i = 0; i < planes; i++) {
  286. offs[i] = AV_RL32(buf + 4 + i * 4);
  287. if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  288. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  289. return -1;
  290. }
  291. }
  292. offs[planes] = buf_size;
  293. for(i = 0; i < planes; i++){
  294. av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
  295. if (!s->tmpbuf)
  296. return AVERROR(ENOMEM);
  297. if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
  298. avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
  299. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  300. return -1;
  301. }
  302. }
  303. // convert pseudo-YUV into real RGB
  304. for(j = 0; j < avctx->height; j++){
  305. for(i = 0; i < avctx->width; i++){
  306. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  307. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  308. }
  309. }
  310. break;
  311. }
  312. *frame = *f;
  313. *data_size = sizeof(AVFrame);
  314. return buf_size;
  315. }
  316. /**
  317. * closes decoder
  318. * @param avctx codec context
  319. * @return 0 on success or negative if fails
  320. */
  321. static av_cold int decode_end(AVCodecContext *avctx)
  322. {
  323. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  324. if (s->frame.data[0])
  325. avctx->release_buffer(avctx, &s->frame);
  326. av_freep(&s->tmpbuf);
  327. return 0;
  328. }
  329. AVCodec ff_fraps_decoder = {
  330. .name = "fraps",
  331. .type = AVMEDIA_TYPE_VIDEO,
  332. .id = CODEC_ID_FRAPS,
  333. .priv_data_size = sizeof(FrapsContext),
  334. .init = decode_init,
  335. .close = decode_end,
  336. .decode = decode_frame,
  337. .capabilities = CODEC_CAP_DR1,
  338. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  339. };