nuv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "common.h"
  24. #include "avcodec.h"
  25. #include "bswap.h"
  26. #include "dsputil.h"
  27. #include "lzo.h"
  28. #include "rtjpeg.h"
  29. typedef struct {
  30. AVFrame pic;
  31. int width, height;
  32. unsigned int decomp_size;
  33. unsigned char* decomp_buf;
  34. uint32_t lq[64], cq[64];
  35. RTJpegContext rtj;
  36. DSPContext dsp;
  37. } NuvContext;
  38. /**
  39. * \brief copy frame data from buffer to AVFrame, handling stride.
  40. * \param f destination AVFrame
  41. * \param src source buffer, does not use any line-stride
  42. * \param width width of the video frame
  43. * \param height height of the video frame
  44. */
  45. static void copy_frame(AVFrame *f, uint8_t *src,
  46. int width, int height) {
  47. AVPicture pic;
  48. avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
  49. av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
  50. }
  51. /**
  52. * \brief extract quantization tables from codec data into our context
  53. */
  54. static int get_quant(AVCodecContext *avctx, NuvContext *c,
  55. uint8_t *buf, int size) {
  56. int i;
  57. if (size < 2 * 64 * 4) {
  58. av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
  59. return -1;
  60. }
  61. for (i = 0; i < 64; i++, buf += 4)
  62. c->lq[i] = AV_RL32(buf);
  63. for (i = 0; i < 64; i++, buf += 4)
  64. c->cq[i] = AV_RL32(buf);
  65. return 0;
  66. }
  67. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  68. uint8_t *buf, int buf_size) {
  69. NuvContext *c = (NuvContext *)avctx->priv_data;
  70. AVFrame *picture = data;
  71. int orig_size = buf_size;
  72. enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
  73. NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
  74. NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
  75. if (buf_size < 12) {
  76. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  77. return -1;
  78. }
  79. if (c->pic.data[0])
  80. avctx->release_buffer(avctx, &c->pic);
  81. c->pic.reference = 1;
  82. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  83. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  84. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  85. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  86. return -1;
  87. }
  88. // codec data (rtjpeg quant tables)
  89. if (buf[0] == 'D' && buf[1] == 'R') {
  90. int ret;
  91. // skip rest of the frameheader.
  92. buf = &buf[12];
  93. buf_size -= 12;
  94. ret = get_quant(avctx, c, buf, buf_size);
  95. if (ret < 0)
  96. return ret;
  97. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  98. return orig_size;
  99. }
  100. if (buf[0] != 'V' || buf_size < 12) {
  101. av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
  102. return -1;
  103. }
  104. comptype = buf[1];
  105. // skip rest of the frameheader.
  106. buf = &buf[12];
  107. buf_size -= 12;
  108. c->pic.pict_type = FF_I_TYPE;
  109. c->pic.key_frame = 1;
  110. // decompress/copy/whatever data
  111. switch (comptype) {
  112. case NUV_UNCOMPRESSED: {
  113. int height = c->height;
  114. if (buf_size < c->width * height * 3 / 2) {
  115. av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
  116. height = buf_size / c->width / 3 * 2;
  117. }
  118. copy_frame(&c->pic, buf, c->width, height);
  119. break;
  120. }
  121. case NUV_RTJPEG: {
  122. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
  123. break;
  124. }
  125. case NUV_RTJPEG_IN_LZO: {
  126. int outlen = c->decomp_size, inlen = buf_size;
  127. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  128. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  129. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, c->decomp_buf, c->decomp_size);
  130. break;
  131. }
  132. case NUV_LZO: {
  133. int outlen = c->decomp_size, inlen = buf_size;
  134. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  135. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  136. copy_frame(&c->pic, c->decomp_buf, c->width, c->height);
  137. break;
  138. }
  139. case NUV_BLACK: {
  140. memset(c->pic.data[0], 0, c->width * c->height);
  141. memset(c->pic.data[1], 128, c->width * c->height / 4);
  142. memset(c->pic.data[2], 128, c->width * c->height / 4);
  143. break;
  144. }
  145. case NUV_COPY_LAST: {
  146. c->pic.pict_type = FF_P_TYPE;
  147. c->pic.key_frame = 0;
  148. /* nothing more to do here */
  149. break;
  150. }
  151. default:
  152. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  153. return -1;
  154. }
  155. *picture = c->pic;
  156. *data_size = sizeof(AVFrame);
  157. return orig_size;
  158. }
  159. static int decode_init(AVCodecContext *avctx) {
  160. NuvContext *c = (NuvContext *)avctx->priv_data;
  161. avctx->width = (avctx->width + 1) & ~1;
  162. avctx->height = (avctx->height + 1) & ~1;
  163. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  164. return 1;
  165. }
  166. avctx->has_b_frames = 0;
  167. avctx->pix_fmt = PIX_FMT_YUV420P;
  168. c->pic.data[0] = NULL;
  169. c->width = avctx->width;
  170. c->height = avctx->height;
  171. c->decomp_size = c->height * c->width * 3 / 2;
  172. c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
  173. if (!c->decomp_buf) {
  174. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  175. return 1;
  176. }
  177. dsputil_init(&c->dsp, avctx);
  178. if (avctx->extradata_size)
  179. get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
  180. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  181. return 0;
  182. }
  183. static int decode_end(AVCodecContext *avctx) {
  184. NuvContext *c = (NuvContext *)avctx->priv_data;
  185. av_freep(&c->decomp_buf);
  186. if (c->pic.data[0])
  187. avctx->release_buffer(avctx, &c->pic);
  188. return 0;
  189. }
  190. AVCodec nuv_decoder = {
  191. "nuv",
  192. CODEC_TYPE_VIDEO,
  193. CODEC_ID_NUV,
  194. sizeof(NuvContext),
  195. decode_init,
  196. NULL,
  197. decode_end,
  198. decode_frame,
  199. CODEC_CAP_DR1,
  200. };