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.

340 lines
10KB

  1. /*
  2. * NuppelVideo decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavutil/bswap.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/lzo.h"
  27. #include "libavutil/imgutils.h"
  28. #include "avcodec.h"
  29. #include "idctdsp.h"
  30. #include "internal.h"
  31. #include "rtjpeg.h"
  32. typedef struct NuvContext {
  33. AVFrame *pic;
  34. int codec_frameheader;
  35. int quality;
  36. int width, height;
  37. unsigned int decomp_size;
  38. unsigned char *decomp_buf;
  39. uint32_t lq[64], cq[64];
  40. RTJpegContext rtj;
  41. } NuvContext;
  42. static const uint8_t fallback_lquant[] = {
  43. 16, 11, 10, 16, 24, 40, 51, 61,
  44. 12, 12, 14, 19, 26, 58, 60, 55,
  45. 14, 13, 16, 24, 40, 57, 69, 56,
  46. 14, 17, 22, 29, 51, 87, 80, 62,
  47. 18, 22, 37, 56, 68, 109, 103, 77,
  48. 24, 35, 55, 64, 81, 104, 113, 92,
  49. 49, 64, 78, 87, 103, 121, 120, 101,
  50. 72, 92, 95, 98, 112, 100, 103, 99
  51. };
  52. static const uint8_t fallback_cquant[] = {
  53. 17, 18, 24, 47, 99, 99, 99, 99,
  54. 18, 21, 26, 66, 99, 99, 99, 99,
  55. 24, 26, 56, 99, 99, 99, 99, 99,
  56. 47, 66, 99, 99, 99, 99, 99, 99,
  57. 99, 99, 99, 99, 99, 99, 99, 99,
  58. 99, 99, 99, 99, 99, 99, 99, 99,
  59. 99, 99, 99, 99, 99, 99, 99, 99,
  60. 99, 99, 99, 99, 99, 99, 99, 99
  61. };
  62. /**
  63. * @brief copy frame data from buffer to AVFrame, handling stride.
  64. * @param f destination AVFrame
  65. * @param src source buffer, does not use any line-stride
  66. * @param width width of the video frame
  67. * @param height height of the video frame
  68. */
  69. static void copy_frame(AVFrame *f, const uint8_t *src, int width, int height)
  70. {
  71. uint8_t *src_data[4];
  72. int src_linesize[4];
  73. av_image_fill_arrays(src_data, src_linesize, src,
  74. f->format, width, height, 1);
  75. av_image_copy(f->data, f->linesize, src_data, src_linesize,
  76. f->format, width, height);
  77. }
  78. /**
  79. * @brief extract quantization tables from codec data into our context
  80. */
  81. static int get_quant(AVCodecContext *avctx, NuvContext *c, const uint8_t *buf,
  82. int size)
  83. {
  84. int i;
  85. if (size < 2 * 64 * 4) {
  86. av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
  87. return AVERROR_INVALIDDATA;
  88. }
  89. for (i = 0; i < 64; i++, buf += 4)
  90. c->lq[i] = AV_RL32(buf);
  91. for (i = 0; i < 64; i++, buf += 4)
  92. c->cq[i] = AV_RL32(buf);
  93. return 0;
  94. }
  95. /**
  96. * @brief set quantization tables from a quality value
  97. */
  98. static void get_quant_quality(NuvContext *c, int quality)
  99. {
  100. int i;
  101. quality = FFMAX(quality, 1);
  102. for (i = 0; i < 64; i++) {
  103. c->lq[i] = (fallback_lquant[i] << 7) / quality;
  104. c->cq[i] = (fallback_cquant[i] << 7) / quality;
  105. }
  106. }
  107. static int codec_reinit(AVCodecContext *avctx, int width, int height,
  108. int quality)
  109. {
  110. NuvContext *c = avctx->priv_data;
  111. int ret;
  112. width = FFALIGN(width, 2);
  113. height = FFALIGN(height, 2);
  114. if (quality >= 0)
  115. get_quant_quality(c, quality);
  116. if (width != c->width || height != c->height) {
  117. void *ptr;
  118. if ((ret = av_image_check_size(height, width, 0, avctx)) < 0)
  119. return ret;
  120. avctx->width = c->width = width;
  121. avctx->height = c->height = height;
  122. ptr = av_fast_realloc(c->decomp_buf, &c->decomp_size,
  123. c->height * c->width * 3 / 2 +
  124. AV_INPUT_BUFFER_PADDING_SIZE +
  125. RTJPEG_HEADER_SIZE);
  126. if (!ptr) {
  127. av_log(avctx, AV_LOG_ERROR,
  128. "Can't allocate decompression buffer.\n");
  129. return AVERROR(ENOMEM);
  130. } else
  131. c->decomp_buf = ptr;
  132. ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq);
  133. av_frame_unref(c->pic);
  134. } else if (quality != c->quality)
  135. ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq);
  136. return 0;
  137. }
  138. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  139. AVPacket *avpkt)
  140. {
  141. const uint8_t *buf = avpkt->data;
  142. int buf_size = avpkt->size;
  143. NuvContext *c = avctx->priv_data;
  144. AVFrame *picture = data;
  145. int orig_size = buf_size;
  146. int keyframe, ret;
  147. int result, init_frame = !avctx->frame_number;
  148. enum {
  149. NUV_UNCOMPRESSED = '0',
  150. NUV_RTJPEG = '1',
  151. NUV_RTJPEG_IN_LZO = '2',
  152. NUV_LZO = '3',
  153. NUV_BLACK = 'N',
  154. NUV_COPY_LAST = 'L'
  155. } comptype;
  156. if (buf_size < 12) {
  157. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  158. return AVERROR_INVALIDDATA;
  159. }
  160. // codec data (rtjpeg quant tables)
  161. if (buf[0] == 'D' && buf[1] == 'R') {
  162. int ret;
  163. // Skip the rest of the frame header.
  164. buf = &buf[12];
  165. buf_size -= 12;
  166. ret = get_quant(avctx, c, buf, buf_size);
  167. if (ret < 0)
  168. return ret;
  169. ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq);
  170. return orig_size;
  171. }
  172. if (buf[0] != 'V' || buf_size < 12) {
  173. av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. comptype = buf[1];
  177. switch (comptype) {
  178. case NUV_RTJPEG_IN_LZO:
  179. case NUV_RTJPEG:
  180. keyframe = !buf[2];
  181. break;
  182. case NUV_COPY_LAST:
  183. keyframe = 0;
  184. break;
  185. default:
  186. keyframe = 1;
  187. break;
  188. }
  189. // Skip the rest of the frame header.
  190. buf = &buf[12];
  191. buf_size -= 12;
  192. if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) {
  193. int outlen = c->decomp_size - AV_INPUT_BUFFER_PADDING_SIZE;
  194. int inlen = buf_size;
  195. if (av_lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) {
  196. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. buf = c->decomp_buf;
  200. buf_size = outlen;
  201. }
  202. if (c->codec_frameheader) {
  203. int w, h, q;
  204. if (buf_size < RTJPEG_HEADER_SIZE || buf[4] != RTJPEG_HEADER_SIZE ||
  205. buf[5] != RTJPEG_FILE_VERSION) {
  206. av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. w = AV_RL16(&buf[6]);
  210. h = AV_RL16(&buf[8]);
  211. q = buf[10];
  212. if ((result = codec_reinit(avctx, w, h, q)) < 0)
  213. return result;
  214. if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO)
  215. buf = c->decomp_buf;
  216. buf = &buf[RTJPEG_HEADER_SIZE];
  217. buf_size -= RTJPEG_HEADER_SIZE;
  218. }
  219. if (keyframe) {
  220. av_frame_unref(c->pic);
  221. init_frame = 1;
  222. }
  223. result = ff_reget_buffer(avctx, c->pic);
  224. if (result < 0) {
  225. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  226. return result;
  227. }
  228. if (init_frame) {
  229. memset(c->pic->data[0], 0, avctx->height * c->pic->linesize[0]);
  230. memset(c->pic->data[1], 0x80, avctx->height * c->pic->linesize[1] / 2);
  231. memset(c->pic->data[2], 0x80, avctx->height * c->pic->linesize[2] / 2);
  232. }
  233. c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  234. c->pic->key_frame = keyframe;
  235. // decompress/copy/whatever data
  236. switch (comptype) {
  237. case NUV_LZO:
  238. case NUV_UNCOMPRESSED: {
  239. int height = c->height;
  240. if (buf_size < c->width * height * 3 / 2) {
  241. av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
  242. height = buf_size / c->width / 3 * 2;
  243. }
  244. copy_frame(c->pic, buf, c->width, height);
  245. break;
  246. }
  247. case NUV_RTJPEG_IN_LZO:
  248. case NUV_RTJPEG:
  249. ret = ff_rtjpeg_decode_frame_yuv420(&c->rtj, c->pic, buf, buf_size);
  250. if (ret < 0)
  251. return ret;
  252. break;
  253. case NUV_BLACK:
  254. memset(c->pic->data[0], 0, c->width * c->height);
  255. memset(c->pic->data[1], 128, c->width * c->height / 4);
  256. memset(c->pic->data[2], 128, c->width * c->height / 4);
  257. break;
  258. case NUV_COPY_LAST:
  259. /* nothing more to do here */
  260. break;
  261. default:
  262. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  263. return AVERROR_INVALIDDATA;
  264. }
  265. if ((result = av_frame_ref(picture, c->pic)) < 0)
  266. return result;
  267. *got_frame = 1;
  268. return orig_size;
  269. }
  270. static av_cold int decode_init(AVCodecContext *avctx)
  271. {
  272. NuvContext *c = avctx->priv_data;
  273. int ret;
  274. c->pic = av_frame_alloc();
  275. if (!c->pic)
  276. return AVERROR(ENOMEM);
  277. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  278. c->decomp_buf = NULL;
  279. c->quality = -1;
  280. c->width = 0;
  281. c->height = 0;
  282. c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G');
  283. if (avctx->extradata_size)
  284. get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
  285. ff_rtjpeg_init(&c->rtj, avctx);
  286. if ((ret = codec_reinit(avctx, avctx->width, avctx->height, -1)) < 0)
  287. return ret;
  288. return 0;
  289. }
  290. static av_cold int decode_end(AVCodecContext *avctx)
  291. {
  292. NuvContext *c = avctx->priv_data;
  293. av_freep(&c->decomp_buf);
  294. av_frame_free(&c->pic);
  295. return 0;
  296. }
  297. AVCodec ff_nuv_decoder = {
  298. .name = "nuv",
  299. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo/RTJPEG"),
  300. .type = AVMEDIA_TYPE_VIDEO,
  301. .id = AV_CODEC_ID_NUV,
  302. .priv_data_size = sizeof(NuvContext),
  303. .init = decode_init,
  304. .close = decode_end,
  305. .decode = decode_frame,
  306. .capabilities = AV_CODEC_CAP_DR1,
  307. };