2013-05-22 04:16:51 +03:00
|
|
|
/*
|
|
|
|
* SMV JPEG decoder
|
|
|
|
* Copyright (c) 2013 Ash Hughes
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* SMV JPEG decoder.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// #define DEBUG
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "mjpegdec.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct SMVJpegDecodeContext {
|
|
|
|
MJpegDecodeContext jpg;
|
|
|
|
AVFrame *picture[2]; /* pictures array */
|
|
|
|
AVCodecContext* avctx;
|
|
|
|
int frames_per_jpeg;
|
2013-06-07 15:20:59 +03:00
|
|
|
int mjpeg_data_size;
|
2013-05-22 04:16:51 +03:00
|
|
|
} SMVJpegDecodeContext;
|
|
|
|
|
|
|
|
static inline void smv_img_pnt_plane(uint8_t **dst, uint8_t *src,
|
|
|
|
int src_linesize, int height, int nlines)
|
|
|
|
{
|
|
|
|
if (!dst || !src)
|
|
|
|
return;
|
|
|
|
src += (nlines) * src_linesize * height;
|
|
|
|
*dst = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void smv_img_pnt(uint8_t *dst_data[4], uint8_t *src_data[4],
|
|
|
|
const int src_linesizes[4],
|
2015-03-18 02:18:40 +02:00
|
|
|
enum AVPixelFormat pix_fmt, int width, int height,
|
2013-05-22 04:16:51 +03:00
|
|
|
int nlines)
|
|
|
|
{
|
2013-05-22 19:23:57 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
|
2013-05-22 04:16:51 +03:00
|
|
|
int i, planes_nb = 0;
|
|
|
|
|
2013-07-06 08:37:31 +03:00
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
|
2013-05-22 04:16:51 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < desc->nb_components; i++)
|
|
|
|
planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
|
|
|
|
|
|
|
|
for (i = 0; i < planes_nb; i++) {
|
|
|
|
int h = height;
|
|
|
|
if (i == 1 || i == 2) {
|
2016-01-27 17:19:38 +02:00
|
|
|
h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
|
2013-05-22 04:16:51 +03:00
|
|
|
}
|
|
|
|
smv_img_pnt_plane(&dst_data[i], src_data[i],
|
|
|
|
src_linesizes[i], h, nlines);
|
|
|
|
}
|
2013-06-08 02:19:27 +03:00
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
|
avutil/pixdesc: deprecate AV_PIX_FMT_FLAG_PSEUDOPAL
PSEUDOPAL pixel formats are not paletted, but carried a palette with the
intention of allowing code to treat unpaletted formats as paletted. The
palette simply mapped the byte values to the resulting RGB values,
making it some sort of LUT for RGB conversion.
It was used for 1 byte formats only: RGB4_BYTE, BGR4_BYTE, RGB8, BGR8,
GRAY8. The first 4 are awfully obscure, used only by some ancient bitmap
formats. The last one, GRAY8, is more common, but its treatment is
grossly incorrect. It considers full range GRAY8 only, so GRAY8 coming
from typical Y video planes was not mapped to the correct RGB values.
This cannot be fixed, because AVFrame.color_range can be freely changed
at runtime, and there is nothing to ensure the pseudo palette is
updated.
Also, nothing actually used the PSEUDOPAL palette data, except xwdenc
(trivially changed in the previous commit). All other code had to treat
it as a special case, just to ignore or to propagate palette data.
In conclusion, this was just a very strange old mechnaism that has no
real justification to exist anymore (although it may have been nice and
useful in the past). Now it's an artifact that makes the API harder to
use: API users who allocate their own pixel data have to be aware that
they need to allocate the palette, or FFmpeg will crash on them in
_some_ situations. On top of this, there was no API to allocate the
pseuo palette outside of av_frame_get_buffer().
This patch not only deprecates AV_PIX_FMT_FLAG_PSEUDOPAL, but also makes
the pseudo palette optional. Nothing accesses it anymore, though if it's
set, it's propagated. It's still allocated and initialized for
compatibility with API users that rely on this feature. But new API
users do not need to allocate it. This was an explicit goal of this
patch.
Most changes replace AV_PIX_FMT_FLAG_PSEUDOPAL with FF_PSEUDOPAL. I
first tried #ifdefing all code, but it was a mess. The FF_PSEUDOPAL
macro reduces the mess, and still allows defining FF_API_PSEUDOPAL to 0.
Passes FATE with FF_API_PSEUDOPAL enabled and disabled. In addition,
FATE passes with FF_API_PSEUDOPAL set to 1, but with allocation
functions manually changed to not allocating a palette.
2018-03-29 15:18:28 +02:00
|
|
|
desc->flags & FF_PSEUDOPAL)
|
2013-06-08 02:19:27 +03:00
|
|
|
dst_data[1] = src_data[1];
|
2013-05-22 04:16:51 +03:00
|
|
|
}
|
|
|
|
|
2014-11-23 00:25:12 +02:00
|
|
|
static av_cold int smvjpeg_decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
SMVJpegDecodeContext *s = avctx->priv_data;
|
|
|
|
MJpegDecodeContext *jpg = &s->jpg;
|
|
|
|
|
|
|
|
jpg->picture_ptr = NULL;
|
|
|
|
av_frame_free(&s->picture[0]);
|
|
|
|
av_frame_free(&s->picture[1]);
|
2020-05-27 09:30:46 +02:00
|
|
|
avcodec_free_context(&s->avctx);
|
2020-06-13 01:50:22 +02:00
|
|
|
return 0;
|
2014-11-23 00:25:12 +02:00
|
|
|
}
|
|
|
|
|
2013-05-22 04:16:51 +03:00
|
|
|
static av_cold int smvjpeg_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
SMVJpegDecodeContext *s = avctx->priv_data;
|
|
|
|
AVCodec *codec;
|
|
|
|
AVDictionary *thread_opt = NULL;
|
2015-04-01 18:05:16 +02:00
|
|
|
int ret = 0, r;
|
2013-05-22 04:16:51 +03:00
|
|
|
|
|
|
|
s->frames_per_jpeg = 0;
|
|
|
|
|
|
|
|
s->picture[0] = av_frame_alloc();
|
|
|
|
if (!s->picture[0])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->picture[1] = av_frame_alloc();
|
2014-11-23 00:25:12 +02:00
|
|
|
if (!s->picture[1]) {
|
|
|
|
av_frame_free(&s->picture[0]);
|
2013-05-22 04:16:51 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2014-11-23 00:25:12 +02:00
|
|
|
}
|
2013-05-22 04:16:51 +03:00
|
|
|
|
|
|
|
s->jpg.picture_ptr = s->picture[0];
|
|
|
|
|
|
|
|
if (avctx->extradata_size >= 4)
|
|
|
|
s->frames_per_jpeg = AV_RL32(avctx->extradata);
|
|
|
|
|
|
|
|
if (s->frames_per_jpeg <= 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
|
2015-04-01 18:05:16 +02:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
2013-05-22 04:16:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
|
|
|
|
if (!codec) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n");
|
2015-04-01 18:05:16 +02:00
|
|
|
smvjpeg_decode_end(avctx);
|
|
|
|
return AVERROR_DECODER_NOT_FOUND;
|
2013-05-22 04:16:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s->avctx = avcodec_alloc_context3(codec);
|
|
|
|
|
|
|
|
av_dict_set(&thread_opt, "threads", "1", 0);
|
2013-06-21 22:56:18 +03:00
|
|
|
s->avctx->refcounted_frames = 1;
|
2013-05-30 04:30:01 +03:00
|
|
|
s->avctx->flags = avctx->flags;
|
|
|
|
s->avctx->idct_algo = avctx->idct_algo;
|
2015-04-01 18:05:16 +02:00
|
|
|
if ((r = ff_codec_open2_recursive(s->avctx, codec, &thread_opt)) < 0) {
|
2013-05-22 04:16:51 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n");
|
2015-04-01 18:05:16 +02:00
|
|
|
ret = r;
|
2013-05-22 04:16:51 +03:00
|
|
|
}
|
|
|
|
av_dict_free(&thread_opt);
|
|
|
|
|
2014-11-23 00:25:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
smvjpeg_decode_end(avctx);
|
2013-05-22 04:16:51 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
|
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
2013-06-07 21:36:48 +03:00
|
|
|
const AVPixFmtDescriptor *desc;
|
2013-05-22 04:16:51 +03:00
|
|
|
SMVJpegDecodeContext *s = avctx->priv_data;
|
|
|
|
AVFrame* mjpeg_data = s->picture[0];
|
|
|
|
int i, cur_frame = 0, ret = 0;
|
|
|
|
|
|
|
|
cur_frame = avpkt->pts % s->frames_per_jpeg;
|
|
|
|
|
2016-11-10 23:09:03 +02:00
|
|
|
/* cur_frame is later used to calculate the buffer offset, so it mustn't be negative */
|
|
|
|
if (cur_frame < 0)
|
|
|
|
cur_frame += s->frames_per_jpeg;
|
|
|
|
|
2013-05-22 04:16:51 +03:00
|
|
|
/* Are we at the start of a block? */
|
2013-06-21 22:56:18 +03:00
|
|
|
if (!cur_frame) {
|
|
|
|
av_frame_unref(mjpeg_data);
|
2013-06-07 15:20:59 +03:00
|
|
|
ret = avcodec_decode_video2(s->avctx, mjpeg_data, &s->mjpeg_data_size, avpkt);
|
2015-05-15 17:21:10 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
s->mjpeg_data_size = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2013-06-21 22:56:18 +03:00
|
|
|
} else if (!s->mjpeg_data_size)
|
2013-06-08 21:00:16 +03:00
|
|
|
return AVERROR(EINVAL);
|
2013-06-07 15:20:59 +03:00
|
|
|
|
2013-06-07 21:36:48 +03:00
|
|
|
desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
|
2015-06-13 00:27:21 +02:00
|
|
|
av_assert0(desc);
|
|
|
|
|
|
|
|
if (mjpeg_data->height % (s->frames_per_jpeg << desc->log2_chroma_h)) {
|
2013-06-07 21:36:48 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid height\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2013-06-07 15:20:59 +03:00
|
|
|
/*use the last lot... */
|
|
|
|
*data_size = s->mjpeg_data_size;
|
2013-05-22 04:16:51 +03:00
|
|
|
|
|
|
|
avctx->pix_fmt = s->avctx->pix_fmt;
|
|
|
|
|
|
|
|
/* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected
|
|
|
|
in init */
|
2014-05-09 20:41:05 +03:00
|
|
|
ret = ff_set_dimensions(avctx, mjpeg_data->width, mjpeg_data->height / s->frames_per_jpeg);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Failed to set dimensions\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2013-05-22 04:16:51 +03:00
|
|
|
|
2013-06-07 15:20:59 +03:00
|
|
|
if (*data_size) {
|
|
|
|
s->picture[1]->extended_data = NULL;
|
|
|
|
s->picture[1]->width = avctx->width;
|
|
|
|
s->picture[1]->height = avctx->height;
|
|
|
|
s->picture[1]->format = avctx->pix_fmt;
|
|
|
|
smv_img_pnt(s->picture[1]->data, mjpeg_data->data, mjpeg_data->linesize,
|
|
|
|
avctx->pix_fmt, avctx->width, avctx->height, cur_frame);
|
|
|
|
for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
|
|
|
|
s->picture[1]->linesize[i] = mjpeg_data->linesize[i];
|
|
|
|
|
|
|
|
ret = av_frame_ref(data, s->picture[1]);
|
2016-12-15 21:09:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-06-07 15:20:59 +03:00
|
|
|
}
|
2013-05-22 04:16:51 +03:00
|
|
|
|
2016-12-15 21:09:48 +02:00
|
|
|
return avpkt->size;
|
2013-05-22 04:16:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass smvjpegdec_class = {
|
|
|
|
.class_name = "SMVJPEG decoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec ff_smvjpeg_decoder = {
|
|
|
|
.name = "smvjpeg",
|
2013-10-04 16:33:20 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"),
|
2013-05-22 04:16:51 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_SMVJPEG,
|
|
|
|
.priv_data_size = sizeof(SMVJpegDecodeContext),
|
|
|
|
.init = smvjpeg_decode_init,
|
|
|
|
.close = smvjpeg_decode_end,
|
|
|
|
.decode = smvjpeg_decode_frame,
|
|
|
|
.priv_class = &smvjpegdec_class,
|
|
|
|
};
|