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.

249 lines
7.4KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file fraps.c
  22. * Lossless Fraps 'FPS1' decoder
  23. * @author Roine Gustafsson <roine at users sf net>
  24. *
  25. * Only decodes version 0 and 1 files.
  26. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  27. *
  28. * Version 2 files, which are the most commonly found Fraps files, cannot be
  29. * decoded yet.
  30. */
  31. #include "avcodec.h"
  32. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  33. /**
  34. * local variable storage
  35. */
  36. typedef struct FrapsContext{
  37. AVCodecContext *avctx;
  38. AVFrame frame;
  39. } FrapsContext;
  40. /**
  41. * initializes decoder
  42. * @param avctx codec context
  43. * @return 0 on success or negative if fails
  44. */
  45. static int decode_init(AVCodecContext *avctx)
  46. {
  47. FrapsContext * const s = avctx->priv_data;
  48. avctx->coded_frame = (AVFrame*)&s->frame;
  49. avctx->has_b_frames = 0;
  50. avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
  51. s->avctx = avctx;
  52. s->frame.data[0] = NULL;
  53. return 0;
  54. }
  55. /**
  56. * decode a frame
  57. * @param avctx codec context
  58. * @param data output AVFrame
  59. * @param data_size size of output data or 0 if no picture is returned
  60. * @param buf input data frame
  61. * @param buf_size size of input data frame
  62. * @return number of consumed bytes on success or negative if decode fails
  63. */
  64. static int decode_frame(AVCodecContext *avctx,
  65. void *data, int *data_size,
  66. uint8_t *buf, int buf_size)
  67. {
  68. FrapsContext * const s = avctx->priv_data;
  69. AVFrame *frame = data;
  70. AVFrame * const f = (AVFrame*)&s->frame;
  71. uint32_t header;
  72. unsigned int version,header_size;
  73. unsigned int x, y;
  74. uint32_t *buf32;
  75. uint32_t *luma1,*luma2,*cb,*cr;
  76. header = LE_32(buf);
  77. version = header & 0xff;
  78. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  79. if (version > 1) {
  80. av_log(avctx, AV_LOG_ERROR,
  81. "This file is encoded with Fraps version %d. " \
  82. "This codec can only decode version 0 and 1.\n", version);
  83. return -1;
  84. }
  85. buf+=4;
  86. if (header_size == 8)
  87. buf+=4;
  88. switch(version) {
  89. case 0:
  90. default:
  91. /* Fraps v0 is a reordered YUV420 */
  92. avctx->pix_fmt = PIX_FMT_YUV420P;
  93. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  94. (buf_size != header_size) ) {
  95. av_log(avctx, AV_LOG_ERROR,
  96. "Invalid frame length %d (should be %d)\n",
  97. buf_size, avctx->width*avctx->height*3/2+header_size);
  98. return -1;
  99. }
  100. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  101. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  102. avctx->width, avctx->height);
  103. return -1;
  104. }
  105. f->reference = 1;
  106. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  107. FF_BUFFER_HINTS_PRESERVE |
  108. FF_BUFFER_HINTS_REUSABLE;
  109. if (avctx->reget_buffer(avctx, f)) {
  110. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  111. return -1;
  112. }
  113. /* bit 31 means same as previous pic */
  114. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  115. f->key_frame = f->pict_type == FF_I_TYPE;
  116. if (f->pict_type == FF_I_TYPE) {
  117. buf32=(uint32_t*)buf;
  118. for(y=0; y<avctx->height/2; y++){
  119. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  120. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  121. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  122. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  123. for(x=0; x<avctx->width; x+=8){
  124. *(luma1++) = *(buf32++);
  125. *(luma1++) = *(buf32++);
  126. *(luma2++) = *(buf32++);
  127. *(luma2++) = *(buf32++);
  128. *(cr++) = *(buf32++);
  129. *(cb++) = *(buf32++);
  130. }
  131. }
  132. }
  133. break;
  134. case 1:
  135. /* Fraps v1 is an upside-down BGR24 */
  136. avctx->pix_fmt = PIX_FMT_BGR24;
  137. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  138. (buf_size != header_size) ) {
  139. av_log(avctx, AV_LOG_ERROR,
  140. "Invalid frame length %d (should be %d)\n",
  141. buf_size, avctx->width*avctx->height*3+header_size);
  142. return -1;
  143. }
  144. f->reference = 1;
  145. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  146. FF_BUFFER_HINTS_PRESERVE |
  147. FF_BUFFER_HINTS_REUSABLE;
  148. if (avctx->reget_buffer(avctx, f)) {
  149. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  150. return -1;
  151. }
  152. /* bit 31 means same as previous pic */
  153. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  154. f->key_frame = f->pict_type == FF_I_TYPE;
  155. if (f->pict_type == FF_I_TYPE) {
  156. for(y=0; y<avctx->height; y++)
  157. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  158. &buf[y*avctx->width*3],
  159. f->linesize[0]);
  160. }
  161. break;
  162. case 2:
  163. /**
  164. * Fraps v2 sub-header description. All numbers are little-endian:
  165. * (this is all guesswork)
  166. *
  167. * 0: DWORD 'FPSx'
  168. * 4: DWORD 0x00000010 unknown, perhaps flags
  169. * 8: DWORD off_2 offset to plane 2
  170. * 12: DWORD off_3 offset to plane 3
  171. * 16: 256xDWORD freqtbl_1 frequency table for plane 1
  172. * 1040: plane_1
  173. * ...
  174. * off_2: 256xDWORD freqtbl_2 frequency table for plane 2
  175. * plane_2
  176. * ...
  177. * off_3: 256xDWORD freqtbl_3 frequency table for plane 3
  178. * plane_3
  179. */
  180. if ((BE_32(buf) != FPS_TAG)||(buf_size < (3*1024 + 8))) {
  181. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  182. return -1;
  183. }
  184. /* NOT FINISHED */
  185. break;
  186. }
  187. *frame = *f;
  188. *data_size = sizeof(AVFrame);
  189. return buf_size;
  190. }
  191. /**
  192. * closes decoder
  193. * @param avctx codec context
  194. * @return 0 on success or negative if fails
  195. */
  196. static int decode_end(AVCodecContext *avctx)
  197. {
  198. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  199. if (s->frame.data[0])
  200. avctx->release_buffer(avctx, &s->frame);
  201. return 0;
  202. }
  203. AVCodec fraps_decoder = {
  204. "fraps",
  205. CODEC_TYPE_VIDEO,
  206. CODEC_ID_FRAPS,
  207. sizeof(FrapsContext),
  208. decode_init,
  209. NULL,
  210. decode_end,
  211. decode_frame,
  212. CODEC_CAP_DR1,
  213. };