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.

225 lines
6.8KB

  1. /*
  2. * SMV JPEG decoder
  3. * Copyright (c) 2013 Ash Hughes
  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
  23. * SMV JPEG decoder.
  24. */
  25. // #define DEBUG
  26. #include "avcodec.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/imgutils.h"
  29. #include "mjpegdec.h"
  30. #include "internal.h"
  31. typedef struct SMVJpegDecodeContext {
  32. MJpegDecodeContext jpg;
  33. AVFrame *picture[2]; /* pictures array */
  34. AVCodecContext* avctx;
  35. int frames_per_jpeg;
  36. int mjpeg_data_size;
  37. } SMVJpegDecodeContext;
  38. static inline void smv_img_pnt_plane(uint8_t **dst, uint8_t *src,
  39. int src_linesize, int height, int nlines)
  40. {
  41. if (!dst || !src)
  42. return;
  43. src += (nlines) * src_linesize * height;
  44. *dst = src;
  45. }
  46. static inline void smv_img_pnt(uint8_t *dst_data[4], uint8_t *src_data[4],
  47. const int src_linesizes[4],
  48. enum AVPixelFormat pix_fmt, int width, int height,
  49. int nlines)
  50. {
  51. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  52. int i, planes_nb = 0;
  53. if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  54. return;
  55. for (i = 0; i < desc->nb_components; i++)
  56. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  57. for (i = 0; i < planes_nb; i++) {
  58. int h = height;
  59. if (i == 1 || i == 2) {
  60. h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
  61. }
  62. smv_img_pnt_plane(&dst_data[i], src_data[i],
  63. src_linesizes[i], h, nlines);
  64. }
  65. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  66. desc->flags & FF_PSEUDOPAL)
  67. dst_data[1] = src_data[1];
  68. }
  69. static av_cold int smvjpeg_decode_end(AVCodecContext *avctx)
  70. {
  71. SMVJpegDecodeContext *s = avctx->priv_data;
  72. MJpegDecodeContext *jpg = &s->jpg;
  73. jpg->picture_ptr = NULL;
  74. av_frame_free(&s->picture[0]);
  75. av_frame_free(&s->picture[1]);
  76. avcodec_free_context(&s->avctx);
  77. return 0;
  78. }
  79. static av_cold int smvjpeg_decode_init(AVCodecContext *avctx)
  80. {
  81. SMVJpegDecodeContext *s = avctx->priv_data;
  82. AVCodec *codec;
  83. AVDictionary *thread_opt = NULL;
  84. int ret = 0, r;
  85. s->frames_per_jpeg = 0;
  86. s->picture[0] = av_frame_alloc();
  87. if (!s->picture[0])
  88. return AVERROR(ENOMEM);
  89. s->picture[1] = av_frame_alloc();
  90. if (!s->picture[1]) {
  91. av_frame_free(&s->picture[0]);
  92. return AVERROR(ENOMEM);
  93. }
  94. s->jpg.picture_ptr = s->picture[0];
  95. if (avctx->extradata_size >= 4)
  96. s->frames_per_jpeg = AV_RL32(avctx->extradata);
  97. if (s->frames_per_jpeg <= 0) {
  98. av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
  99. ret = AVERROR_INVALIDDATA;
  100. }
  101. codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  102. if (!codec) {
  103. av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n");
  104. smvjpeg_decode_end(avctx);
  105. return AVERROR_DECODER_NOT_FOUND;
  106. }
  107. s->avctx = avcodec_alloc_context3(codec);
  108. av_dict_set(&thread_opt, "threads", "1", 0);
  109. s->avctx->refcounted_frames = 1;
  110. s->avctx->flags = avctx->flags;
  111. s->avctx->idct_algo = avctx->idct_algo;
  112. if ((r = avcodec_open2(s->avctx, codec, &thread_opt)) < 0) {
  113. av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n");
  114. ret = r;
  115. }
  116. av_dict_free(&thread_opt);
  117. if (ret < 0)
  118. smvjpeg_decode_end(avctx);
  119. return ret;
  120. }
  121. static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  122. AVPacket *avpkt)
  123. {
  124. const AVPixFmtDescriptor *desc;
  125. SMVJpegDecodeContext *s = avctx->priv_data;
  126. AVFrame* mjpeg_data = s->picture[0];
  127. int i, cur_frame = 0, ret = 0;
  128. cur_frame = avpkt->pts % s->frames_per_jpeg;
  129. /* cur_frame is later used to calculate the buffer offset, so it mustn't be negative */
  130. if (cur_frame < 0)
  131. cur_frame += s->frames_per_jpeg;
  132. /* Are we at the start of a block? */
  133. if (!cur_frame) {
  134. av_frame_unref(mjpeg_data);
  135. ret = avcodec_decode_video2(s->avctx, mjpeg_data, &s->mjpeg_data_size, avpkt);
  136. if (ret < 0) {
  137. s->mjpeg_data_size = 0;
  138. return ret;
  139. }
  140. } else if (!s->mjpeg_data_size)
  141. return AVERROR(EINVAL);
  142. desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  143. av_assert0(desc);
  144. if (mjpeg_data->height % (s->frames_per_jpeg << desc->log2_chroma_h)) {
  145. av_log(avctx, AV_LOG_ERROR, "Invalid height\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. /*use the last lot... */
  149. *data_size = s->mjpeg_data_size;
  150. avctx->pix_fmt = s->avctx->pix_fmt;
  151. /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected
  152. in init */
  153. ret = ff_set_dimensions(avctx, mjpeg_data->width, mjpeg_data->height / s->frames_per_jpeg);
  154. if (ret < 0) {
  155. av_log(s, AV_LOG_ERROR, "Failed to set dimensions\n");
  156. return ret;
  157. }
  158. if (*data_size) {
  159. s->picture[1]->extended_data = NULL;
  160. s->picture[1]->width = avctx->width;
  161. s->picture[1]->height = avctx->height;
  162. s->picture[1]->format = avctx->pix_fmt;
  163. smv_img_pnt(s->picture[1]->data, mjpeg_data->data, mjpeg_data->linesize,
  164. avctx->pix_fmt, avctx->width, avctx->height, cur_frame);
  165. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  166. s->picture[1]->linesize[i] = mjpeg_data->linesize[i];
  167. ret = av_frame_ref(data, s->picture[1]);
  168. if (ret < 0)
  169. return ret;
  170. }
  171. return avpkt->size;
  172. }
  173. static const AVClass smvjpegdec_class = {
  174. .class_name = "SMVJPEG decoder",
  175. .item_name = av_default_item_name,
  176. .version = LIBAVUTIL_VERSION_INT,
  177. };
  178. AVCodec ff_smvjpeg_decoder = {
  179. .name = "smvjpeg",
  180. .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"),
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .id = AV_CODEC_ID_SMVJPEG,
  183. .priv_data_size = sizeof(SMVJpegDecodeContext),
  184. .init = smvjpeg_decode_init,
  185. .close = smvjpeg_decode_end,
  186. .decode = smvjpeg_decode_frame,
  187. .priv_class = &smvjpegdec_class,
  188. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  189. };