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.

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