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.

215 lines
6.7KB

  1. /*
  2. * NuppelVideo decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "avcodec.h"
  24. #include "bswap.h"
  25. #include "dsputil.h"
  26. #include "lzo.h"
  27. #include "rtjpeg.h"
  28. typedef struct {
  29. AVFrame pic;
  30. int width, height;
  31. unsigned int decomp_size;
  32. unsigned char* decomp_buf;
  33. uint32_t lq[64], cq[64];
  34. RTJpegContext rtj;
  35. DSPContext dsp;
  36. } NuvContext;
  37. /**
  38. * \brief copy frame data from buffer to AVFrame, handling stride.
  39. * \param f destination AVFrame
  40. * \param src source buffer, does not use any line-stride
  41. * \param width width of the video frame
  42. * \param height height of the video frame
  43. */
  44. static void copy_frame(AVFrame *f, uint8_t *src,
  45. int width, int height) {
  46. AVPicture pic;
  47. avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
  48. av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
  49. }
  50. /**
  51. * \brief extract quantization tables from codec data into our context
  52. */
  53. static int get_quant(AVCodecContext *avctx, NuvContext *c,
  54. uint8_t *buf, int size) {
  55. int i;
  56. if (size < 2 * 64 * 4) {
  57. av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
  58. return -1;
  59. }
  60. for (i = 0; i < 64; i++, buf += 4)
  61. c->lq[i] = AV_RL32(buf);
  62. for (i = 0; i < 64; i++, buf += 4)
  63. c->cq[i] = AV_RL32(buf);
  64. return 0;
  65. }
  66. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  67. uint8_t *buf, int buf_size) {
  68. NuvContext *c = avctx->priv_data;
  69. AVFrame *picture = data;
  70. int orig_size = buf_size;
  71. enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
  72. NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
  73. NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
  74. if (buf_size < 12) {
  75. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  76. return -1;
  77. }
  78. if (c->pic.data[0])
  79. avctx->release_buffer(avctx, &c->pic);
  80. c->pic.reference = 1;
  81. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  82. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  83. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  84. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  85. return -1;
  86. }
  87. // codec data (rtjpeg quant tables)
  88. if (buf[0] == 'D' && buf[1] == 'R') {
  89. int ret;
  90. // skip rest of the frameheader.
  91. buf = &buf[12];
  92. buf_size -= 12;
  93. ret = get_quant(avctx, c, buf, buf_size);
  94. if (ret < 0)
  95. return ret;
  96. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  97. return orig_size;
  98. }
  99. if (buf[0] != 'V' || buf_size < 12) {
  100. av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
  101. return -1;
  102. }
  103. comptype = buf[1];
  104. // skip rest of the frameheader.
  105. buf = &buf[12];
  106. buf_size -= 12;
  107. c->pic.pict_type = FF_I_TYPE;
  108. c->pic.key_frame = 1;
  109. // decompress/copy/whatever data
  110. switch (comptype) {
  111. case NUV_UNCOMPRESSED: {
  112. int height = c->height;
  113. if (buf_size < c->width * height * 3 / 2) {
  114. av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
  115. height = buf_size / c->width / 3 * 2;
  116. }
  117. copy_frame(&c->pic, buf, c->width, height);
  118. break;
  119. }
  120. case NUV_RTJPEG: {
  121. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
  122. break;
  123. }
  124. case NUV_RTJPEG_IN_LZO: {
  125. int outlen = c->decomp_size, inlen = buf_size;
  126. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  127. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  128. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, c->decomp_buf, c->decomp_size);
  129. break;
  130. }
  131. case NUV_LZO: {
  132. int outlen = c->decomp_size, inlen = buf_size;
  133. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  134. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  135. copy_frame(&c->pic, c->decomp_buf, c->width, c->height);
  136. break;
  137. }
  138. case NUV_BLACK: {
  139. memset(c->pic.data[0], 0, c->width * c->height);
  140. memset(c->pic.data[1], 128, c->width * c->height / 4);
  141. memset(c->pic.data[2], 128, c->width * c->height / 4);
  142. break;
  143. }
  144. case NUV_COPY_LAST: {
  145. c->pic.pict_type = FF_P_TYPE;
  146. c->pic.key_frame = 0;
  147. /* nothing more to do here */
  148. break;
  149. }
  150. default:
  151. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  152. return -1;
  153. }
  154. *picture = c->pic;
  155. *data_size = sizeof(AVFrame);
  156. return orig_size;
  157. }
  158. static int decode_init(AVCodecContext *avctx) {
  159. NuvContext *c = avctx->priv_data;
  160. avctx->width = (avctx->width + 1) & ~1;
  161. avctx->height = (avctx->height + 1) & ~1;
  162. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  163. return 1;
  164. }
  165. avctx->pix_fmt = PIX_FMT_YUV420P;
  166. c->pic.data[0] = NULL;
  167. c->width = avctx->width;
  168. c->height = avctx->height;
  169. c->decomp_size = c->height * c->width * 3 / 2;
  170. c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
  171. if (!c->decomp_buf) {
  172. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  173. return 1;
  174. }
  175. dsputil_init(&c->dsp, avctx);
  176. if (avctx->extradata_size)
  177. get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
  178. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  179. return 0;
  180. }
  181. static int decode_end(AVCodecContext *avctx) {
  182. NuvContext *c = avctx->priv_data;
  183. av_freep(&c->decomp_buf);
  184. if (c->pic.data[0])
  185. avctx->release_buffer(avctx, &c->pic);
  186. return 0;
  187. }
  188. AVCodec nuv_decoder = {
  189. "nuv",
  190. CODEC_TYPE_VIDEO,
  191. CODEC_ID_NUV,
  192. sizeof(NuvContext),
  193. decode_init,
  194. NULL,
  195. decode_end,
  196. decode_frame,
  197. CODEC_CAP_DR1,
  198. };