fraps.c 12 KB

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