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.

336 lines
11KB

  1. /*
  2. * lossless JPEG encoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * Support for external huffman table, various fixes (AVID workaround),
  8. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  9. * by Alex Beregszaszi
  10. *
  11. * This file is part of Libav.
  12. *
  13. * Libav is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * Libav is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with Libav; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file
  29. * lossless JPEG encoder.
  30. */
  31. #include "libavutil/frame.h"
  32. #include "libavutil/mem.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "internal.h"
  37. #include "mpegvideo.h"
  38. #include "mjpeg.h"
  39. #include "mjpegenc.h"
  40. typedef struct LJpegEncContext {
  41. DSPContext dsp;
  42. ScanTable scantable;
  43. uint16_t matrix[64];
  44. int vsample[3];
  45. int hsample[3];
  46. uint16_t huff_code_dc_luminance[12];
  47. uint16_t huff_code_dc_chrominance[12];
  48. uint8_t huff_size_dc_luminance[12];
  49. uint8_t huff_size_dc_chrominance[12];
  50. uint16_t (*scratch)[4];
  51. } LJpegEncContext;
  52. static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
  53. const AVFrame *frame)
  54. {
  55. LJpegEncContext *s = avctx->priv_data;
  56. const int width = frame->width;
  57. const int height = frame->height;
  58. const int linesize = frame->linesize[0];
  59. uint16_t (*buffer)[4] = s->scratch;
  60. const int predictor = avctx->prediction_method+1;
  61. int left[3], top[3], topleft[3];
  62. int x, y, i;
  63. for (i = 0; i < 3; i++)
  64. buffer[0][i] = 1 << (9 - 1);
  65. for (y = 0; y < height; y++) {
  66. const int modified_predictor = y ? predictor : 1;
  67. uint8_t *ptr = frame->data[0] + (linesize * y);
  68. if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
  69. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  70. return -1;
  71. }
  72. for (i = 0; i < 3; i++)
  73. top[i]= left[i]= topleft[i]= buffer[0][i];
  74. for (x = 0; x < width; x++) {
  75. buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
  76. buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
  77. buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
  78. for (i = 0; i < 3; i++) {
  79. int pred, diff;
  80. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  81. topleft[i] = top[i];
  82. top[i] = buffer[x+1][i];
  83. left[i] = buffer[x][i];
  84. diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
  85. if (i == 0)
  86. ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  87. else
  88. ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  89. }
  90. }
  91. }
  92. return 0;
  93. }
  94. static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
  95. const AVFrame *frame, int predictor,
  96. int mb_x, int mb_y)
  97. {
  98. int i;
  99. if (mb_x == 0 || mb_y == 0) {
  100. for (i = 0; i < 3; i++) {
  101. uint8_t *ptr;
  102. int x, y, h, v, linesize;
  103. h = s->hsample[i];
  104. v = s->vsample[i];
  105. linesize = frame->linesize[i];
  106. for (y = 0; y < v; y++) {
  107. for (x = 0; x < h; x++) {
  108. int pred;
  109. ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  110. if (y == 0 && mb_y == 0) {
  111. if (x == 0 && mb_x == 0)
  112. pred = 128;
  113. else
  114. pred = ptr[-1];
  115. } else {
  116. if (x == 0 && mb_x == 0) {
  117. pred = ptr[-linesize];
  118. } else {
  119. PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
  120. ptr[-1], predictor);
  121. }
  122. }
  123. if (i == 0)
  124. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  125. else
  126. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  127. }
  128. }
  129. }
  130. } else {
  131. for (i = 0; i < 3; i++) {
  132. uint8_t *ptr;
  133. int x, y, h, v, linesize;
  134. h = s->hsample[i];
  135. v = s->vsample[i];
  136. linesize = frame->linesize[i];
  137. for (y = 0; y < v; y++) {
  138. for (x = 0; x < h; x++) {
  139. int pred;
  140. ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  141. PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
  142. if (i == 0)
  143. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  144. else
  145. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
  152. const AVFrame *frame)
  153. {
  154. const int predictor = avctx->prediction_method + 1;
  155. LJpegEncContext *s = avctx->priv_data;
  156. const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
  157. const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
  158. int mb_x, mb_y;
  159. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  160. if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
  161. mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
  162. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  163. return -1;
  164. }
  165. for (mb_x = 0; mb_x < mb_width; mb_x++)
  166. ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
  167. }
  168. return 0;
  169. }
  170. static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  171. const AVFrame *pict, int *got_packet)
  172. {
  173. LJpegEncContext *s = avctx->priv_data;
  174. PutBitContext pb;
  175. const int width = avctx->width;
  176. const int height = avctx->height;
  177. const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
  178. const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
  179. int max_pkt_size = FF_MIN_BUFFER_SIZE;
  180. int ret, header_bits;
  181. if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
  182. max_pkt_size += width * height * 3 * 3;
  183. else {
  184. max_pkt_size += mb_width * mb_height * 3 * 4
  185. * s->hsample[0] * s->vsample[0];
  186. }
  187. if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
  188. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
  189. return ret;
  190. }
  191. init_put_bits(&pb, pkt->data, pkt->size);
  192. ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
  193. s->matrix);
  194. header_bits = put_bits_count(&pb);
  195. if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
  196. ret = ljpeg_encode_bgr(avctx, &pb, pict);
  197. else
  198. ret = ljpeg_encode_yuv(avctx, &pb, pict);
  199. if (ret < 0)
  200. return ret;
  201. emms_c();
  202. ff_mjpeg_encode_picture_trailer(&pb, header_bits);
  203. flush_put_bits(&pb);
  204. pkt->size = put_bits_ptr(&pb) - pb.buf;
  205. pkt->flags |= AV_PKT_FLAG_KEY;
  206. *got_packet = 1;
  207. return 0;
  208. }
  209. static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
  210. {
  211. LJpegEncContext *s = avctx->priv_data;
  212. av_frame_free(&avctx->coded_frame);
  213. av_freep(&s->scratch);
  214. return 0;
  215. }
  216. static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
  217. {
  218. LJpegEncContext *s = avctx->priv_data;
  219. int chroma_v_shift, chroma_h_shift;
  220. if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  221. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  222. avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
  223. avctx->color_range == AVCOL_RANGE_MPEG) &&
  224. avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  225. av_log(avctx, AV_LOG_ERROR,
  226. "Limited range YUV is non-standard, set strict_std_compliance to "
  227. "at least unofficial to use it.\n");
  228. return AVERROR(EINVAL);
  229. }
  230. avctx->coded_frame = av_frame_alloc();
  231. if (!avctx->coded_frame)
  232. return AVERROR(ENOMEM);
  233. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  234. avctx->coded_frame->key_frame = 1;
  235. s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
  236. ff_dsputil_init(&s->dsp, avctx);
  237. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  238. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  239. &chroma_v_shift);
  240. if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  241. s->vsample[0] = s->hsample[0] =
  242. s->vsample[1] = s->hsample[1] =
  243. s->vsample[2] = s->hsample[2] = 1;
  244. } else {
  245. s->vsample[0] = 2;
  246. s->vsample[1] = 2 >> chroma_v_shift;
  247. s->vsample[2] = 2 >> chroma_v_shift;
  248. s->hsample[0] = 2;
  249. s->hsample[1] = 2 >> chroma_h_shift;
  250. s->hsample[2] = 2 >> chroma_h_shift;
  251. }
  252. ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
  253. s->huff_code_dc_luminance,
  254. avpriv_mjpeg_bits_dc_luminance,
  255. avpriv_mjpeg_val_dc);
  256. ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
  257. s->huff_code_dc_chrominance,
  258. avpriv_mjpeg_bits_dc_chrominance,
  259. avpriv_mjpeg_val_dc);
  260. return 0;
  261. }
  262. AVCodec ff_ljpeg_encoder = {
  263. .name = "ljpeg",
  264. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .id = AV_CODEC_ID_LJPEG,
  267. .priv_data_size = sizeof(LJpegEncContext),
  268. .init = ljpeg_encode_init,
  269. .encode2 = ljpeg_encode_frame,
  270. .close = ljpeg_encode_close,
  271. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,
  272. AV_PIX_FMT_YUVJ422P,
  273. AV_PIX_FMT_YUVJ444P,
  274. AV_PIX_FMT_BGR24,
  275. AV_PIX_FMT_YUV420P,
  276. AV_PIX_FMT_YUV422P,
  277. AV_PIX_FMT_YUV444P,
  278. AV_PIX_FMT_NONE },
  279. };