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.

187 lines
5.9KB

  1. /*
  2. * MJPEG 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. * MJPEG encoder.
  30. */
  31. #include "libavutil/pixdesc.h"
  32. #include "avcodec.h"
  33. #include "jpegtables.h"
  34. #include "mjpegenc_common.h"
  35. #include "mpegvideo.h"
  36. #include "mjpeg.h"
  37. #include "mjpegenc.h"
  38. av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
  39. {
  40. MJpegContext *m;
  41. m = av_malloc(sizeof(MJpegContext));
  42. if (!m)
  43. return AVERROR(ENOMEM);
  44. s->min_qcoeff=-1023;
  45. s->max_qcoeff= 1023;
  46. /* build all the huffman tables */
  47. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  48. m->huff_code_dc_luminance,
  49. avpriv_mjpeg_bits_dc_luminance,
  50. avpriv_mjpeg_val_dc);
  51. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  52. m->huff_code_dc_chrominance,
  53. avpriv_mjpeg_bits_dc_chrominance,
  54. avpriv_mjpeg_val_dc);
  55. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  56. m->huff_code_ac_luminance,
  57. avpriv_mjpeg_bits_ac_luminance,
  58. avpriv_mjpeg_val_ac_luminance);
  59. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  60. m->huff_code_ac_chrominance,
  61. avpriv_mjpeg_bits_ac_chrominance,
  62. avpriv_mjpeg_val_ac_chrominance);
  63. s->mjpeg_ctx = m;
  64. return 0;
  65. }
  66. void ff_mjpeg_encode_close(MpegEncContext *s)
  67. {
  68. av_free(s->mjpeg_ctx);
  69. }
  70. static void encode_block(MpegEncContext *s, int16_t *block, int n)
  71. {
  72. int mant, nbits, code, i, j;
  73. int component, dc, run, last_index, val;
  74. MJpegContext *m = s->mjpeg_ctx;
  75. uint8_t *huff_size_ac;
  76. uint16_t *huff_code_ac;
  77. /* DC coef */
  78. component = (n <= 3 ? 0 : (n&1) + 1);
  79. dc = block[0]; /* overflow is impossible */
  80. val = dc - s->last_dc[component];
  81. if (n < 4) {
  82. ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  83. huff_size_ac = m->huff_size_ac_luminance;
  84. huff_code_ac = m->huff_code_ac_luminance;
  85. } else {
  86. ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  87. huff_size_ac = m->huff_size_ac_chrominance;
  88. huff_code_ac = m->huff_code_ac_chrominance;
  89. }
  90. s->last_dc[component] = dc;
  91. /* AC coefs */
  92. run = 0;
  93. last_index = s->block_last_index[n];
  94. for(i=1;i<=last_index;i++) {
  95. j = s->intra_scantable.permutated[i];
  96. val = block[j];
  97. if (val == 0) {
  98. run++;
  99. } else {
  100. while (run >= 16) {
  101. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  102. run -= 16;
  103. }
  104. mant = val;
  105. if (val < 0) {
  106. val = -val;
  107. mant--;
  108. }
  109. nbits= av_log2(val) + 1;
  110. code = (run << 4) | nbits;
  111. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  112. put_sbits(&s->pb, nbits, mant);
  113. run = 0;
  114. }
  115. }
  116. /* output EOB only if not already 64 values */
  117. if (last_index < 63 || run != 0)
  118. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  119. }
  120. void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[8][64])
  121. {
  122. int i;
  123. for(i=0;i<5;i++) {
  124. encode_block(s, block[i], i);
  125. }
  126. if (s->chroma_format == CHROMA_420) {
  127. encode_block(s, block[5], 5);
  128. } else {
  129. encode_block(s, block[6], 6);
  130. encode_block(s, block[5], 5);
  131. encode_block(s, block[7], 7);
  132. }
  133. s->i_tex_bits += get_bits_diff(s);
  134. }
  135. #define OFFSET(x) offsetof(MpegEncContext, x)
  136. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  137. static const AVOption options[] = {
  138. { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
  139. { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
  140. { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
  141. { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
  142. { NULL},
  143. };
  144. static const AVClass mjpeg_class = {
  145. .class_name = "mjpeg",
  146. .item_name = av_default_item_name,
  147. .option = options,
  148. .version = LIBAVUTIL_VERSION_INT,
  149. };
  150. AVCodec ff_mjpeg_encoder = {
  151. .name = "mjpeg",
  152. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  153. .type = AVMEDIA_TYPE_VIDEO,
  154. .id = AV_CODEC_ID_MJPEG,
  155. .priv_data_size = sizeof(MpegEncContext),
  156. .priv_class = &mjpeg_class,
  157. .init = ff_mpv_encode_init,
  158. .encode2 = ff_mpv_encode_picture,
  159. .close = ff_mpv_encode_end,
  160. .pix_fmts = (const enum AVPixelFormat[]){
  161. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
  162. },
  163. };