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.

370 lines
12KB

  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 "idctdsp.h"
  36. #include "internal.h"
  37. #include "jpegtables.h"
  38. #include "mjpegenc_common.h"
  39. #include "mjpeg.h"
  40. #include "mjpegenc.h"
  41. typedef struct LJpegEncContext {
  42. AVClass *class;
  43. IDCTDSPContext idsp;
  44. ScanTable scantable;
  45. uint16_t matrix[64];
  46. int vsample[3];
  47. int hsample[3];
  48. uint16_t huff_code_dc_luminance[12];
  49. uint16_t huff_code_dc_chrominance[12];
  50. uint8_t huff_size_dc_luminance[12];
  51. uint8_t huff_size_dc_chrominance[12];
  52. uint16_t (*scratch)[4];
  53. int pred;
  54. } LJpegEncContext;
  55. static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
  56. const AVFrame *frame)
  57. {
  58. LJpegEncContext *s = avctx->priv_data;
  59. const int width = frame->width;
  60. const int height = frame->height;
  61. const int linesize = frame->linesize[0];
  62. uint16_t (*buffer)[4] = s->scratch;
  63. int left[3], top[3], topleft[3];
  64. int x, y, i;
  65. #if FF_API_PRIVATE_OPT
  66. FF_DISABLE_DEPRECATION_WARNINGS
  67. if (avctx->prediction_method)
  68. s->pred = avctx->prediction_method + 1;
  69. FF_ENABLE_DEPRECATION_WARNINGS
  70. #endif
  71. for (i = 0; i < 3; i++)
  72. buffer[0][i] = 1 << (9 - 1);
  73. for (y = 0; y < height; y++) {
  74. const int modified_predictor = y ? s->pred : 1;
  75. uint8_t *ptr = frame->data[0] + (linesize * y);
  76. if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
  77. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  78. return -1;
  79. }
  80. for (i = 0; i < 3; i++)
  81. top[i]= left[i]= topleft[i]= buffer[0][i];
  82. for (x = 0; x < width; x++) {
  83. buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
  84. buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
  85. buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
  86. for (i = 0; i < 3; i++) {
  87. int pred, diff;
  88. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  89. topleft[i] = top[i];
  90. top[i] = buffer[x+1][i];
  91. left[i] = buffer[x][i];
  92. diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
  93. if (i == 0)
  94. ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  95. else
  96. ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
  103. const AVFrame *frame, int predictor,
  104. int mb_x, int mb_y)
  105. {
  106. int i;
  107. if (mb_x == 0 || mb_y == 0) {
  108. for (i = 0; i < 3; i++) {
  109. uint8_t *ptr;
  110. int x, y, h, v, linesize;
  111. h = s->hsample[i];
  112. v = s->vsample[i];
  113. linesize = frame->linesize[i];
  114. for (y = 0; y < v; y++) {
  115. for (x = 0; x < h; x++) {
  116. int pred;
  117. ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  118. if (y == 0 && mb_y == 0) {
  119. if (x == 0 && mb_x == 0)
  120. pred = 128;
  121. else
  122. pred = ptr[-1];
  123. } else {
  124. if (x == 0 && mb_x == 0) {
  125. pred = ptr[-linesize];
  126. } else {
  127. PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
  128. ptr[-1], predictor);
  129. }
  130. }
  131. if (i == 0)
  132. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  133. else
  134. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  135. }
  136. }
  137. }
  138. } else {
  139. for (i = 0; i < 3; i++) {
  140. uint8_t *ptr;
  141. int x, y, h, v, linesize;
  142. h = s->hsample[i];
  143. v = s->vsample[i];
  144. linesize = frame->linesize[i];
  145. for (y = 0; y < v; y++) {
  146. for (x = 0; x < h; x++) {
  147. int pred;
  148. ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  149. PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
  150. if (i == 0)
  151. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  152. else
  153. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
  160. const AVFrame *frame)
  161. {
  162. LJpegEncContext *s = avctx->priv_data;
  163. const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
  164. const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
  165. int mb_x, mb_y;
  166. #if FF_API_PRIVATE_OPT
  167. FF_DISABLE_DEPRECATION_WARNINGS
  168. if (avctx->prediction_method)
  169. s->pred = avctx->prediction_method + 1;
  170. FF_ENABLE_DEPRECATION_WARNINGS
  171. #endif
  172. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  173. if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
  174. mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
  175. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  176. return -1;
  177. }
  178. for (mb_x = 0; mb_x < mb_width; mb_x++)
  179. ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
  180. }
  181. return 0;
  182. }
  183. static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  184. const AVFrame *pict, int *got_packet)
  185. {
  186. LJpegEncContext *s = avctx->priv_data;
  187. PutBitContext pb;
  188. const int width = avctx->width;
  189. const int height = avctx->height;
  190. const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
  191. const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
  192. int max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
  193. int ret, header_bits;
  194. if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
  195. max_pkt_size += width * height * 3 * 3;
  196. else {
  197. max_pkt_size += mb_width * mb_height * 3 * 4
  198. * s->hsample[0] * s->vsample[0];
  199. }
  200. if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
  201. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
  202. return ret;
  203. }
  204. init_put_bits(&pb, pkt->data, pkt->size);
  205. ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
  206. s->pred, s->matrix);
  207. header_bits = put_bits_count(&pb);
  208. if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
  209. ret = ljpeg_encode_bgr(avctx, &pb, pict);
  210. else
  211. ret = ljpeg_encode_yuv(avctx, &pb, pict);
  212. if (ret < 0)
  213. return ret;
  214. emms_c();
  215. ff_mjpeg_encode_picture_trailer(&pb, header_bits);
  216. flush_put_bits(&pb);
  217. pkt->size = put_bits_ptr(&pb) - pb.buf;
  218. pkt->flags |= AV_PKT_FLAG_KEY;
  219. *got_packet = 1;
  220. return 0;
  221. }
  222. static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
  223. {
  224. LJpegEncContext *s = avctx->priv_data;
  225. av_freep(&s->scratch);
  226. return 0;
  227. }
  228. static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
  229. {
  230. LJpegEncContext *s = avctx->priv_data;
  231. int chroma_v_shift, chroma_h_shift;
  232. if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  233. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  234. avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
  235. avctx->color_range == AVCOL_RANGE_MPEG) &&
  236. avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  237. av_log(avctx, AV_LOG_ERROR,
  238. "Limited range YUV is non-standard, set strict_std_compliance to "
  239. "at least unofficial to use it.\n");
  240. return AVERROR(EINVAL);
  241. }
  242. #if FF_API_CODED_FRAME
  243. FF_DISABLE_DEPRECATION_WARNINGS
  244. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  245. avctx->coded_frame->key_frame = 1;
  246. FF_ENABLE_DEPRECATION_WARNINGS
  247. #endif
  248. s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
  249. ff_idctdsp_init(&s->idsp, avctx);
  250. ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
  251. ff_zigzag_direct);
  252. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  253. &chroma_v_shift);
  254. if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  255. s->vsample[0] = s->hsample[0] =
  256. s->vsample[1] = s->hsample[1] =
  257. s->vsample[2] = s->hsample[2] = 1;
  258. } else {
  259. s->vsample[0] = 2;
  260. s->vsample[1] = 2 >> chroma_v_shift;
  261. s->vsample[2] = 2 >> chroma_v_shift;
  262. s->hsample[0] = 2;
  263. s->hsample[1] = 2 >> chroma_h_shift;
  264. s->hsample[2] = 2 >> chroma_h_shift;
  265. }
  266. ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
  267. s->huff_code_dc_luminance,
  268. avpriv_mjpeg_bits_dc_luminance,
  269. avpriv_mjpeg_val_dc);
  270. ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
  271. s->huff_code_dc_chrominance,
  272. avpriv_mjpeg_bits_dc_chrominance,
  273. avpriv_mjpeg_val_dc);
  274. return 0;
  275. }
  276. #define OFFSET(x) offsetof(LJpegEncContext, x)
  277. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  278. static const AVOption options[] = {
  279. { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
  280. { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
  281. { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
  282. { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
  283. { NULL},
  284. };
  285. static const AVClass ljpeg_class = {
  286. .class_name = "ljpeg",
  287. .item_name = av_default_item_name,
  288. .option = options,
  289. .version = LIBAVUTIL_VERSION_INT,
  290. };
  291. AVCodec ff_ljpeg_encoder = {
  292. .name = "ljpeg",
  293. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .id = AV_CODEC_ID_LJPEG,
  296. .priv_data_size = sizeof(LJpegEncContext),
  297. .priv_class = &ljpeg_class,
  298. .init = ljpeg_encode_init,
  299. .encode2 = ljpeg_encode_frame,
  300. .close = ljpeg_encode_close,
  301. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,
  302. AV_PIX_FMT_YUVJ422P,
  303. AV_PIX_FMT_YUVJ444P,
  304. AV_PIX_FMT_BGR24,
  305. AV_PIX_FMT_YUV420P,
  306. AV_PIX_FMT_YUV422P,
  307. AV_PIX_FMT_YUV444P,
  308. AV_PIX_FMT_NONE },
  309. };