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.

170 lines
5.2KB

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