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.

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