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