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.

329 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 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/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 * 4) {
  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. if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
  76. buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
  77. buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
  78. buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
  79. }else{
  80. buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
  81. buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
  82. buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
  83. }
  84. for (i = 0; i < 3; i++) {
  85. int pred, diff;
  86. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  87. topleft[i] = top[i];
  88. top[i] = buffer[x+1][i];
  89. left[i] = buffer[x][i];
  90. diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
  91. if (i == 0)
  92. ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  93. else
  94. ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  95. }
  96. }
  97. }
  98. return 0;
  99. }
  100. static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
  101. const AVFrame *frame, int predictor,
  102. int mb_x, int mb_y)
  103. {
  104. int i;
  105. if (mb_x == 0 || mb_y == 0) {
  106. for (i = 0; i < 3; i++) {
  107. uint8_t *ptr;
  108. int x, y, h, v, linesize;
  109. h = s->hsample[i];
  110. v = s->vsample[i];
  111. linesize = frame->linesize[i];
  112. for (y = 0; y < v; y++) {
  113. for (x = 0; x < h; x++) {
  114. int pred;
  115. ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  116. if (y == 0 && mb_y == 0) {
  117. if (x == 0 && mb_x == 0)
  118. pred = 128;
  119. else
  120. pred = ptr[-1];
  121. } else {
  122. if (x == 0 && mb_x == 0) {
  123. pred = ptr[-linesize];
  124. } else {
  125. PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
  126. ptr[-1], predictor);
  127. }
  128. }
  129. if (i == 0)
  130. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  131. else
  132. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  133. }
  134. }
  135. }
  136. } else {
  137. for (i = 0; i < 3; i++) {
  138. uint8_t *ptr;
  139. int x, y, h, v, linesize;
  140. h = s->hsample[i];
  141. v = s->vsample[i];
  142. linesize = frame->linesize[i];
  143. for (y = 0; y < v; y++) {
  144. for (x = 0; x < h; x++) {
  145. int pred;
  146. ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  147. PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
  148. if (i == 0)
  149. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
  150. else
  151. ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
  158. const AVFrame *frame)
  159. {
  160. const int predictor = avctx->prediction_method + 1;
  161. LJpegEncContext *s = avctx->priv_data;
  162. const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
  163. const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
  164. int mb_x, mb_y;
  165. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  166. if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
  167. mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
  168. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  169. return -1;
  170. }
  171. for (mb_x = 0; mb_x < mb_width; mb_x++)
  172. ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
  173. }
  174. return 0;
  175. }
  176. static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  177. const AVFrame *pict, int *got_packet)
  178. {
  179. LJpegEncContext *s = avctx->priv_data;
  180. PutBitContext pb;
  181. const int width = avctx->width;
  182. const int height = avctx->height;
  183. const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
  184. const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
  185. int max_pkt_size = FF_MIN_BUFFER_SIZE;
  186. int ret, header_bits;
  187. if( avctx->pix_fmt == AV_PIX_FMT_BGR0
  188. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  189. || avctx->pix_fmt == AV_PIX_FMT_BGR24)
  190. max_pkt_size += width * height * 3 * 4;
  191. else {
  192. max_pkt_size += mb_width * mb_height * 3 * 4
  193. * s->hsample[0] * s->vsample[0];
  194. }
  195. if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
  196. return ret;
  197. init_put_bits(&pb, pkt->data, pkt->size);
  198. ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
  199. s->matrix, s->matrix);
  200. header_bits = put_bits_count(&pb);
  201. if( avctx->pix_fmt == AV_PIX_FMT_BGR0
  202. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  203. || avctx->pix_fmt == AV_PIX_FMT_BGR24)
  204. ret = ljpeg_encode_bgr(avctx, &pb, pict);
  205. else
  206. ret = ljpeg_encode_yuv(avctx, &pb, pict);
  207. if (ret < 0)
  208. return ret;
  209. emms_c();
  210. ff_mjpeg_escape_FF(&pb, header_bits >> 3);
  211. ff_mjpeg_encode_picture_trailer(&pb, header_bits);
  212. flush_put_bits(&pb);
  213. pkt->size = put_bits_ptr(&pb) - pb.buf;
  214. pkt->flags |= AV_PKT_FLAG_KEY;
  215. *got_packet = 1;
  216. return 0;
  217. }
  218. static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
  219. {
  220. LJpegEncContext *s = avctx->priv_data;
  221. av_frame_free(&avctx->coded_frame);
  222. av_freep(&s->scratch);
  223. return 0;
  224. }
  225. static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
  226. {
  227. LJpegEncContext *s = avctx->priv_data;
  228. if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  229. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  230. avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
  231. avctx->color_range == AVCOL_RANGE_MPEG) &&
  232. avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  233. av_log(avctx, AV_LOG_ERROR,
  234. "Limited range YUV is non-standard, set strict_std_compliance to "
  235. "at least unofficial to use it.\n");
  236. return AVERROR(EINVAL);
  237. }
  238. avctx->coded_frame = av_frame_alloc();
  239. if (!avctx->coded_frame)
  240. return AVERROR(ENOMEM);
  241. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  242. avctx->coded_frame->key_frame = 1;
  243. s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
  244. ff_dsputil_init(&s->dsp, avctx);
  245. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  246. ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
  247. ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
  248. s->huff_code_dc_luminance,
  249. avpriv_mjpeg_bits_dc_luminance,
  250. avpriv_mjpeg_val_dc);
  251. ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
  252. s->huff_code_dc_chrominance,
  253. avpriv_mjpeg_bits_dc_chrominance,
  254. avpriv_mjpeg_val_dc);
  255. return 0;
  256. }
  257. AVCodec ff_ljpeg_encoder = {
  258. .name = "ljpeg",
  259. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  260. .type = AVMEDIA_TYPE_VIDEO,
  261. .id = AV_CODEC_ID_LJPEG,
  262. .priv_data_size = sizeof(LJpegEncContext),
  263. .init = ljpeg_encode_init,
  264. .encode2 = ljpeg_encode_frame,
  265. .close = ljpeg_encode_close,
  266. .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
  267. .pix_fmts = (const enum AVPixelFormat[]){
  268. AV_PIX_FMT_BGR24 , AV_PIX_FMT_BGRA , AV_PIX_FMT_BGR0,
  269. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  270. AV_PIX_FMT_YUV420P , AV_PIX_FMT_YUV444P , AV_PIX_FMT_YUV422P,
  271. AV_PIX_FMT_NONE},
  272. };