mjpegbdec.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Apple MJPEG-B decoder
  3. * Copyright (c) 2002 Alex Beregszaszi
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/mjpegbdec.c
  23. * Apple MJPEG-B decoder.
  24. */
  25. #include "avcodec.h"
  26. #include "mjpeg.h"
  27. #include "mjpegdec.h"
  28. static int mjpegb_decode_frame(AVCodecContext *avctx,
  29. void *data, int *data_size,
  30. const uint8_t *buf, int buf_size)
  31. {
  32. MJpegDecodeContext *s = avctx->priv_data;
  33. const uint8_t *buf_end, *buf_ptr;
  34. AVFrame *picture = data;
  35. GetBitContext hgb; /* for the header */
  36. uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
  37. uint32_t field_size, sod_offs;
  38. buf_ptr = buf;
  39. buf_end = buf + buf_size;
  40. read_header:
  41. /* reset on every SOI */
  42. s->restart_interval = 0;
  43. s->restart_count = 0;
  44. s->mjpb_skiptosod = 0;
  45. if (buf_end - buf_ptr >= 1 << 28)
  46. return AVERROR_INVALIDDATA;
  47. init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
  48. skip_bits(&hgb, 32); /* reserved zeros */
  49. if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
  50. {
  51. av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
  52. return 0;
  53. }
  54. field_size = get_bits_long(&hgb, 32); /* field size */
  55. av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
  56. skip_bits(&hgb, 32); /* padded field size */
  57. second_field_offs = get_bits_long(&hgb, 32);
  58. av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
  59. dqt_offs = get_bits_long(&hgb, 32);
  60. av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
  61. if (dqt_offs)
  62. {
  63. init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
  64. s->start_code = DQT;
  65. ff_mjpeg_decode_dqt(s);
  66. }
  67. dht_offs = get_bits_long(&hgb, 32);
  68. av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
  69. if (dht_offs)
  70. {
  71. init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
  72. s->start_code = DHT;
  73. ff_mjpeg_decode_dht(s);
  74. }
  75. sof_offs = get_bits_long(&hgb, 32);
  76. av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
  77. if (sof_offs)
  78. {
  79. init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
  80. s->start_code = SOF0;
  81. if (ff_mjpeg_decode_sof(s) < 0)
  82. return -1;
  83. }
  84. sos_offs = get_bits_long(&hgb, 32);
  85. av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
  86. sod_offs = get_bits_long(&hgb, 32);
  87. av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
  88. if (sos_offs)
  89. {
  90. init_get_bits(&s->gb, buf_ptr + sos_offs,
  91. 8 * FFMIN(field_size, buf_end - buf_ptr - sos_offs));
  92. s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
  93. s->start_code = SOS;
  94. ff_mjpeg_decode_sos(s);
  95. }
  96. if (s->interlaced) {
  97. s->bottom_field ^= 1;
  98. /* if not bottom field, do not output image yet */
  99. if (s->bottom_field != s->interlace_polarity && second_field_offs)
  100. {
  101. buf_ptr = buf + second_field_offs;
  102. second_field_offs = 0;
  103. goto read_header;
  104. }
  105. }
  106. //XXX FIXME factorize, this looks very similar to the EOI code
  107. *picture= s->picture;
  108. *data_size = sizeof(AVFrame);
  109. if(!s->lossless){
  110. picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
  111. picture->qstride= 0;
  112. picture->qscale_table= s->qscale_table;
  113. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  114. if(avctx->debug & FF_DEBUG_QP)
  115. av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  116. picture->quality*= FF_QP2LAMBDA;
  117. }
  118. return buf_ptr - buf;
  119. }
  120. AVCodec mjpegb_decoder = {
  121. "mjpegb",
  122. CODEC_TYPE_VIDEO,
  123. CODEC_ID_MJPEGB,
  124. sizeof(MJpegDecodeContext),
  125. ff_mjpeg_decode_init,
  126. NULL,
  127. ff_mjpeg_decode_end,
  128. mjpegb_decode_frame,
  129. CODEC_CAP_DR1,
  130. NULL,
  131. .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"),
  132. };