fraps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /**
  24. * @file fraps.c
  25. * Lossless Fraps 'FPS1' decoder
  26. * @author Roine Gustafsson <roine at users sf net>
  27. * @author Konstantin Shishkov
  28. *
  29. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  30. *
  31. * Version 2 files support by Konstantin Shishkov
  32. */
  33. #include "avcodec.h"
  34. #include "bitstream.h"
  35. #include "dsputil.h"
  36. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  37. /* symbol for Huffman tree node */
  38. #define HNODE -1
  39. /**
  40. * Huffman node
  41. * FIXME one day this should belong to one general framework
  42. */
  43. typedef struct Node{
  44. int16_t sym;
  45. int16_t n0;
  46. int count;
  47. }Node;
  48. /**
  49. * local variable storage
  50. */
  51. typedef struct FrapsContext{
  52. AVCodecContext *avctx;
  53. AVFrame frame;
  54. Node nodes[512];
  55. uint8_t *tmpbuf;
  56. DSPContext dsp;
  57. } FrapsContext;
  58. /**
  59. * initializes decoder
  60. * @param avctx codec context
  61. * @return 0 on success or negative if fails
  62. */
  63. static int decode_init(AVCodecContext *avctx)
  64. {
  65. FrapsContext * const s = avctx->priv_data;
  66. avctx->coded_frame = (AVFrame*)&s->frame;
  67. avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
  68. s->avctx = avctx;
  69. s->frame.data[0] = NULL;
  70. s->tmpbuf = NULL;
  71. dsputil_init(&s->dsp, avctx);
  72. return 0;
  73. }
  74. /**
  75. * Comparator - our nodes should ascend by count
  76. * but with preserved symbol order
  77. */
  78. static int huff_cmp(const Node *a, const Node *b){
  79. return (a->count - b->count)*256 + a->sym - b->sym;
  80. }
  81. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat, Node *nodes, int node, uint32_t pfx, int pl, int *pos)
  82. {
  83. int s;
  84. s = nodes[node].sym;
  85. if(s != HNODE || !nodes[node].count){
  86. bits[*pos] = pfx;
  87. lens[*pos] = pl;
  88. xlat[*pos] = s;
  89. (*pos)++;
  90. }else{
  91. pfx <<= 1;
  92. pl++;
  93. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos);
  94. pfx |= 1;
  95. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
  96. }
  97. }
  98. static int build_huff_tree(VLC *vlc, Node *nodes, uint8_t *xlat)
  99. {
  100. uint32_t bits[256];
  101. int16_t lens[256];
  102. int pos = 0;
  103. get_tree_codes(bits, lens, xlat, nodes, 510, 0, 0, &pos);
  104. return init_vlc(vlc, 9, pos, lens, 2, 2, bits, 4, 4, 0);
  105. }
  106. /**
  107. * decode Fraps v2 packed plane
  108. */
  109. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  110. int h, uint8_t *src, int size, int Uoff)
  111. {
  112. int i, j;
  113. int cur_node;
  114. GetBitContext gb;
  115. VLC vlc;
  116. int64_t sum = 0;
  117. uint8_t recode[256];
  118. for(i = 0; i < 256; i++){
  119. s->nodes[i].sym = i;
  120. s->nodes[i].count = AV_RL32(src);
  121. s->nodes[i].n0 = -2;
  122. if(s->nodes[i].count < 0) {
  123. av_log(s->avctx, AV_LOG_ERROR, "Symbol count < 0\n");
  124. return -1;
  125. }
  126. src += 4;
  127. sum += s->nodes[i].count;
  128. }
  129. size -= 1024;
  130. if(sum >> 31) {
  131. av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
  132. return -1;
  133. }
  134. qsort(s->nodes, 256, sizeof(Node), huff_cmp);
  135. cur_node = 256;
  136. for(i = 0; i < 511; i += 2){
  137. s->nodes[cur_node].sym = HNODE;
  138. s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count;
  139. s->nodes[cur_node].n0 = i;
  140. for(j = cur_node; j > 0; j--){
  141. if(s->nodes[j].count >= s->nodes[j - 1].count) break;
  142. FFSWAP(Node, s->nodes[j], s->nodes[j - 1]);
  143. }
  144. cur_node++;
  145. }
  146. if(build_huff_tree(&vlc, s->nodes, recode) < 0){
  147. av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
  148. return -1;
  149. }
  150. /* we have built Huffman table and are ready to decode plane */
  151. /* convert bits so they may be used by standard bitreader */
  152. s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
  153. init_get_bits(&gb, s->tmpbuf, size * 8);
  154. for(j = 0; j < h; j++){
  155. for(i = 0; i < w; i++){
  156. dst[i] = recode[get_vlc2(&gb, vlc.table, 9, 3)];
  157. /* lines are stored as deltas between previous lines
  158. * and we need to add 0x80 to the first lines of chroma planes
  159. */
  160. if(j) dst[i] += dst[i - stride];
  161. else if(Uoff) dst[i] += 0x80;
  162. }
  163. dst += stride;
  164. }
  165. free_vlc(&vlc);
  166. return 0;
  167. }
  168. /**
  169. * decode a frame
  170. * @param avctx codec context
  171. * @param data output AVFrame
  172. * @param data_size size of output data or 0 if no picture is returned
  173. * @param buf input data frame
  174. * @param buf_size size of input data frame
  175. * @return number of consumed bytes on success or negative if decode fails
  176. */
  177. static int decode_frame(AVCodecContext *avctx,
  178. void *data, int *data_size,
  179. uint8_t *buf, int buf_size)
  180. {
  181. FrapsContext * const s = avctx->priv_data;
  182. AVFrame *frame = data;
  183. AVFrame * const f = (AVFrame*)&s->frame;
  184. uint32_t header;
  185. unsigned int version,header_size;
  186. unsigned int x, y;
  187. uint32_t *buf32;
  188. uint32_t *luma1,*luma2,*cb,*cr;
  189. uint32_t offs[4];
  190. int i, is_chroma, planes;
  191. header = AV_RL32(buf);
  192. version = header & 0xff;
  193. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  194. if (version > 2 && version != 4) {
  195. av_log(avctx, AV_LOG_ERROR,
  196. "This file is encoded with Fraps version %d. " \
  197. "This codec can only decode version 0, 1, 2 and 4.\n", version);
  198. return -1;
  199. }
  200. buf+=4;
  201. if (header_size == 8)
  202. buf+=4;
  203. switch(version) {
  204. case 0:
  205. default:
  206. /* Fraps v0 is a reordered YUV420 */
  207. avctx->pix_fmt = PIX_FMT_YUV420P;
  208. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  209. (buf_size != header_size) ) {
  210. av_log(avctx, AV_LOG_ERROR,
  211. "Invalid frame length %d (should be %d)\n",
  212. buf_size, avctx->width*avctx->height*3/2+header_size);
  213. return -1;
  214. }
  215. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  216. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  217. avctx->width, avctx->height);
  218. return -1;
  219. }
  220. f->reference = 1;
  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. /* bit 31 means same as previous pic */
  229. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  230. f->key_frame = f->pict_type == FF_I_TYPE;
  231. if (f->pict_type == FF_I_TYPE) {
  232. buf32=(uint32_t*)buf;
  233. for(y=0; y<avctx->height/2; y++){
  234. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  235. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  236. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  237. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  238. for(x=0; x<avctx->width; x+=8){
  239. *(luma1++) = *(buf32++);
  240. *(luma1++) = *(buf32++);
  241. *(luma2++) = *(buf32++);
  242. *(luma2++) = *(buf32++);
  243. *(cr++) = *(buf32++);
  244. *(cb++) = *(buf32++);
  245. }
  246. }
  247. }
  248. break;
  249. case 1:
  250. /* Fraps v1 is an upside-down BGR24 */
  251. avctx->pix_fmt = PIX_FMT_BGR24;
  252. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  253. (buf_size != header_size) ) {
  254. av_log(avctx, AV_LOG_ERROR,
  255. "Invalid frame length %d (should be %d)\n",
  256. buf_size, avctx->width*avctx->height*3+header_size);
  257. return -1;
  258. }
  259. f->reference = 1;
  260. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  261. FF_BUFFER_HINTS_PRESERVE |
  262. FF_BUFFER_HINTS_REUSABLE;
  263. if (avctx->reget_buffer(avctx, f)) {
  264. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  265. return -1;
  266. }
  267. /* bit 31 means same as previous pic */
  268. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  269. f->key_frame = f->pict_type == FF_I_TYPE;
  270. if (f->pict_type == FF_I_TYPE) {
  271. for(y=0; y<avctx->height; y++)
  272. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  273. &buf[y*avctx->width*3],
  274. f->linesize[0]);
  275. }
  276. break;
  277. case 2:
  278. case 4:
  279. /**
  280. * Fraps v2 is Huffman-coded YUV420 planes
  281. * Fraps v4 is virtually the same
  282. */
  283. avctx->pix_fmt = PIX_FMT_YUV420P;
  284. planes = 3;
  285. f->reference = 1;
  286. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  287. FF_BUFFER_HINTS_PRESERVE |
  288. FF_BUFFER_HINTS_REUSABLE;
  289. if (avctx->reget_buffer(avctx, f)) {
  290. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  291. return -1;
  292. }
  293. /* skip frame */
  294. if(buf_size == 8) {
  295. f->pict_type = FF_P_TYPE;
  296. f->key_frame = 0;
  297. break;
  298. }
  299. f->pict_type = FF_I_TYPE;
  300. f->key_frame = 1;
  301. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  302. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  303. return -1;
  304. }
  305. for(i = 0; i < planes; i++) {
  306. offs[i] = AV_RL32(buf + 4 + i * 4);
  307. if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  308. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  309. return -1;
  310. }
  311. }
  312. offs[planes] = buf_size;
  313. for(i = 0; i < planes; i++){
  314. is_chroma = !!i;
  315. s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
  316. if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
  317. avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) {
  318. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  319. return -1;
  320. }
  321. }
  322. break;
  323. }
  324. *frame = *f;
  325. *data_size = sizeof(AVFrame);
  326. return buf_size;
  327. }
  328. /**
  329. * closes decoder
  330. * @param avctx codec context
  331. * @return 0 on success or negative if fails
  332. */
  333. static int decode_end(AVCodecContext *avctx)
  334. {
  335. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  336. if (s->frame.data[0])
  337. avctx->release_buffer(avctx, &s->frame);
  338. av_freep(&s->tmpbuf);
  339. return 0;
  340. }
  341. AVCodec fraps_decoder = {
  342. "fraps",
  343. CODEC_TYPE_VIDEO,
  344. CODEC_ID_FRAPS,
  345. sizeof(FrapsContext),
  346. decode_init,
  347. NULL,
  348. decode_end,
  349. decode_frame,
  350. CODEC_CAP_DR1,
  351. };