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.

393 lines
11KB

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