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.

151 lines
4.6KB

  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 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. init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
  46. skip_bits(&hgb, 32); /* reserved zeros */
  47. if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
  48. {
  49. av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
  50. return 0;
  51. }
  52. field_size = get_bits_long(&hgb, 32); /* field size */
  53. av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
  54. skip_bits(&hgb, 32); /* padded field size */
  55. second_field_offs = get_bits_long(&hgb, 32);
  56. av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
  57. dqt_offs = get_bits_long(&hgb, 32);
  58. av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
  59. if (dqt_offs)
  60. {
  61. init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8);
  62. s->start_code = DQT;
  63. ff_mjpeg_decode_dqt(s);
  64. }
  65. dht_offs = get_bits_long(&hgb, 32);
  66. av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
  67. if (dht_offs)
  68. {
  69. init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8);
  70. s->start_code = DHT;
  71. ff_mjpeg_decode_dht(s);
  72. }
  73. sof_offs = get_bits_long(&hgb, 32);
  74. av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
  75. if (sof_offs)
  76. {
  77. init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8);
  78. s->start_code = SOF0;
  79. if (ff_mjpeg_decode_sof(s) < 0)
  80. return -1;
  81. }
  82. sos_offs = get_bits_long(&hgb, 32);
  83. av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
  84. sod_offs = get_bits_long(&hgb, 32);
  85. av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
  86. if (sos_offs)
  87. {
  88. // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
  89. init_get_bits(&s->gb, buf_ptr+sos_offs, field_size*8);
  90. s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
  91. s->start_code = SOS;
  92. ff_mjpeg_decode_sos(s);
  93. }
  94. if (s->interlaced) {
  95. s->bottom_field ^= 1;
  96. /* if not bottom field, do not output image yet */
  97. if (s->bottom_field != s->interlace_polarity && second_field_offs)
  98. {
  99. buf_ptr = buf + second_field_offs;
  100. second_field_offs = 0;
  101. goto read_header;
  102. }
  103. }
  104. //XXX FIXME factorize, this looks very similar to the EOI code
  105. *picture= s->picture;
  106. *data_size = sizeof(AVFrame);
  107. if(!s->lossless){
  108. picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
  109. picture->qstride= 0;
  110. picture->qscale_table= s->qscale_table;
  111. memset(picture->qscale_table, picture->quality, (s->width+15)/16);
  112. if(avctx->debug & FF_DEBUG_QP)
  113. av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
  114. picture->quality*= FF_QP2LAMBDA;
  115. }
  116. return buf_ptr - buf;
  117. }
  118. AVCodec mjpegb_decoder = {
  119. "mjpegb",
  120. CODEC_TYPE_VIDEO,
  121. CODEC_ID_MJPEGB,
  122. sizeof(MJpegDecodeContext),
  123. ff_mjpeg_decode_init,
  124. NULL,
  125. ff_mjpeg_decode_end,
  126. mjpegb_decode_frame,
  127. CODEC_CAP_DR1,
  128. NULL,
  129. .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"),
  130. };