3 * Copyright (c) 2013 Ash Hughes
5 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/opt.h"
30 #include "libavutil/imgutils.h"
34 typedef struct SMVJpegDecodeContext {
35 MJpegDecodeContext jpg;
36 AVFrame *picture[2]; /* pictures array */
37 AVCodecContext* avctx;
40 } SMVJpegDecodeContext;
42 static inline void smv_img_pnt_plane(uint8_t **dst, uint8_t *src,
43 int src_linesize, int height, int nlines)
47 src += (nlines) * src_linesize * height;
51 static inline void smv_img_pnt(uint8_t *dst_data[4], uint8_t *src_data[4],
52 const int src_linesizes[4],
53 enum AVPixelFormat pix_fmt, int width, int height,
56 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
59 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
62 for (i = 0; i < desc->nb_components; i++)
63 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
65 for (i = 0; i < planes_nb; i++) {
67 if (i == 1 || i == 2) {
68 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
70 smv_img_pnt_plane(&dst_data[i], src_data[i],
71 src_linesizes[i], h, nlines);
73 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
74 desc->flags & FF_PSEUDOPAL)
75 dst_data[1] = src_data[1];
78 static av_cold int smvjpeg_decode_end(AVCodecContext *avctx)
80 SMVJpegDecodeContext *s = avctx->priv_data;
81 MJpegDecodeContext *jpg = &s->jpg;
84 jpg->picture_ptr = NULL;
85 av_frame_free(&s->picture[0]);
86 av_frame_free(&s->picture[1]);
87 avcodec_free_context(&s->avctx);
91 static av_cold int smvjpeg_decode_init(AVCodecContext *avctx)
93 SMVJpegDecodeContext *s = avctx->priv_data;
95 AVDictionary *thread_opt = NULL;
98 s->frames_per_jpeg = 0;
100 s->picture[0] = av_frame_alloc();
102 return AVERROR(ENOMEM);
104 s->picture[1] = av_frame_alloc();
105 if (!s->picture[1]) {
106 av_frame_free(&s->picture[0]);
107 return AVERROR(ENOMEM);
110 s->jpg.picture_ptr = s->picture[0];
112 if (avctx->extradata_size >= 4)
113 s->frames_per_jpeg = AV_RL32(avctx->extradata);
115 if (s->frames_per_jpeg <= 0) {
116 av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
117 ret = AVERROR_INVALIDDATA;
120 codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
122 av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n");
123 smvjpeg_decode_end(avctx);
124 return AVERROR_DECODER_NOT_FOUND;
127 s->avctx = avcodec_alloc_context3(codec);
129 av_dict_set(&thread_opt, "threads", "1", 0);
130 s->avctx->refcounted_frames = 1;
131 s->avctx->flags = avctx->flags;
132 s->avctx->idct_algo = avctx->idct_algo;
133 if ((r = ff_codec_open2_recursive(s->avctx, codec, &thread_opt)) < 0) {
134 av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n");
137 av_dict_free(&thread_opt);
140 smvjpeg_decode_end(avctx);
144 static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
147 const AVPixFmtDescriptor *desc;
148 SMVJpegDecodeContext *s = avctx->priv_data;
149 AVFrame* mjpeg_data = s->picture[0];
150 int i, cur_frame = 0, ret = 0;
152 cur_frame = avpkt->pts % s->frames_per_jpeg;
154 /* cur_frame is later used to calculate the buffer offset, so it mustn't be negative */
156 cur_frame += s->frames_per_jpeg;
158 /* Are we at the start of a block? */
160 av_frame_unref(mjpeg_data);
161 ret = avcodec_decode_video2(s->avctx, mjpeg_data, &s->mjpeg_data_size, avpkt);
163 s->mjpeg_data_size = 0;
166 } else if (!s->mjpeg_data_size)
167 return AVERROR(EINVAL);
169 desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
172 if (mjpeg_data->height % (s->frames_per_jpeg << desc->log2_chroma_h)) {
173 av_log(avctx, AV_LOG_ERROR, "Invalid height\n");
174 return AVERROR_INVALIDDATA;
177 /*use the last lot... */
178 *data_size = s->mjpeg_data_size;
180 avctx->pix_fmt = s->avctx->pix_fmt;
182 /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected
184 ret = ff_set_dimensions(avctx, mjpeg_data->width, mjpeg_data->height / s->frames_per_jpeg);
186 av_log(s, AV_LOG_ERROR, "Failed to set dimensions\n");
191 s->picture[1]->extended_data = NULL;
192 s->picture[1]->width = avctx->width;
193 s->picture[1]->height = avctx->height;
194 s->picture[1]->format = avctx->pix_fmt;
195 smv_img_pnt(s->picture[1]->data, mjpeg_data->data, mjpeg_data->linesize,
196 avctx->pix_fmt, avctx->width, avctx->height, cur_frame);
197 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
198 s->picture[1]->linesize[i] = mjpeg_data->linesize[i];
200 ret = av_frame_ref(data, s->picture[1]);
208 static const AVClass smvjpegdec_class = {
209 .class_name = "SMVJPEG decoder",
210 .item_name = av_default_item_name,
211 .version = LIBAVUTIL_VERSION_INT,
214 AVCodec ff_smvjpeg_decoder = {
216 .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"),
217 .type = AVMEDIA_TYPE_VIDEO,
218 .id = AV_CODEC_ID_SMVJPEG,
219 .priv_data_size = sizeof(SMVJpegDecodeContext),
220 .init = smvjpeg_decode_init,
221 .close = smvjpeg_decode_end,
222 .decode = smvjpeg_decode_frame,
223 .priv_class = &smvjpegdec_class,