[PATCH] Support playing SMV files.
This patch updates SMV support to display video properly, addressing the issue in the archive (http://thread.gmane.org/gmane.comp.video.ffmpeg.devel/135864/focus=135893) of more than one frame in a single jpeg. This also helps towards ticket 412 (http://ffmpeg.org/trac/ffmpeg/ticket/412). The patch modifies two functions in libavutil/imgutils.c to reuse code and ensure maintainability. The changed functions now call the modified ones with a default extra argument of 0. Not sure if this is the best way to do this, but it works! Signed-off-by: Ash Hughes <ashley.hughes <at> blueyonder.co.uk> --- diff -uNr ffmpeg-vanilla//libavcodec/allcodecs.c ffmpeg//libavcodec/allcodecs.c --- ffmpeg-vanilla//libavcodec/allcodecs.c 2012-10-01 19:34:06.944974579 +0100 +++ ffmpeg//libavcodec/allcodecs.c 2012-10-01 19:36:15.533611881 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla//libavcodec/avcodec.h ffmpeg//libavcodec/avcodec.h --- ffmpeg-vanilla//libavcodec/avcodec.h 2012-10-01 19:34:06.956974647 +0100 +++ ffmpeg//libavcodec/avcodec.h 2012-10-01 19:36:15.537611963 +0100 @@ -282,6 +282,7 @@ AV_CODEC_ID_PAF_VIDEO = MKBETAG('P','A','F','V'), AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), + AV_CODEC_ID_SMVJPEG, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla//libavcodec/Makefile ffmpeg//libavcodec/Makefile --- ffmpeg-vanilla//libavcodec/Makefile 2012-10-01 19:34:06.928974504 +0100 +++ ffmpeg//libavcodec/Makefile 2012-10-01 19:36:15.537611963 +0100 @@ -388,6 +388,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla//libavformat/wav.c ffmpeg//libavformat/wav.c --- ffmpeg-vanilla//libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg//libavformat/wav.c 2012-10-01 19:41:31.131177230 +0100 @@ -23,6 +23,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavcodec/smvjpegdec.h" #include "libavutil/avassert.h" #include "libavutil/dict.h" #include "libavutil/log.h" @@ -53,6 +54,8 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; + int force_decode; } WAVContext; #if CONFIG_WAV_MUXER @@ -506,9 +509,11 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = sizeof(SMVCodecExtra); + vst->codec->extradata = av_malloc(sizeof(SMVCodecExtra) + FF_INPUT_BUFFER_PADDING_SIZE); size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +523,9 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + ((SMVCodecExtra*)vst->codec->extradata)->frames_per_block = wav->smv_frames_per_jpeg; + wav->smv_cur_pt = 0; + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,9 +625,14 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++; + pkt->stream_index = 1; + pkt->priv = &wav->force_decode; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); if (ret == AVERROR_EOF) { @@ -678,6 +691,8 @@ else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + wav->force_decode = 1; } st = s->streams[0]; diff -uNr ffmpeg-vanilla//libavutil/imgutils.c ffmpeg//libavutil/imgutils.c --- ffmpeg-vanilla//libavutil/imgutils.c 2012-10-01 19:34:06.888974305 +0100 +++ ffmpeg//libavutil/imgutils.c 2012-10-01 19:36:15.541612053 +0100 @@ -235,8 +235,17 @@ const uint8_t *src, int src_linesize, int bytewidth, int height) { + av_image_copy_plane_skip(dst, dst_linesize, src, src_linesize, + bytewidth, height, 0); +} + +void av_image_copy_plane_skip(uint8_t *dst, int dst_linesize, + const uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines) +{ if (!dst || !src) return; + src += (nlines) * src_linesize * height; for (;height > 0; height--) { memcpy(dst, src, bytewidth); dst += dst_linesize; @@ -248,6 +257,14 @@ const uint8_t *src_data[4], const int src_linesizes[4], enum PixelFormat pix_fmt, int width, int height) { + av_image_copy_skip(dst_data, dst_linesizes, src_data, src_linesizes, + pix_fmt, width, height, 0); +} + +void av_image_copy_skip(uint8_t *dst_data[4], int dst_linesizes[4], + const uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines) +{ const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; if (desc->flags & PIX_FMT_HWACCEL) @@ -255,9 +272,9 @@ if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL) { - av_image_copy_plane(dst_data[0], dst_linesizes[0], + av_image_copy_plane_skip(dst_data[0], dst_linesizes[0], src_data[0], src_linesizes[0], - width, height); + width, height, nlines); /* copy the palette */ memcpy(dst_data[1], src_data[1], 4*256); } else { @@ -272,9 +289,9 @@ if (i == 1 || i == 2) { h= -((-height)>>desc->log2_chroma_h); } - av_image_copy_plane(dst_data[i], dst_linesizes[i], + av_image_copy_plane_skip(dst_data[i], dst_linesizes[i], src_data[i], src_linesizes[i], - bwidth, h); + bwidth, h, nlines); } } } diff -uNr ffmpeg-vanilla//libavutil/imgutils.h ffmpeg//libavutil/imgutils.h --- ffmpeg-vanilla//libavutil/imgutils.h 2012-10-01 19:34:06.884974280 +0100 +++ ffmpeg//libavutil/imgutils.h 2012-10-01 19:36:15.541612053 +0100 @@ -107,6 +107,20 @@ int bytewidth, int height); /** + * Copy image plane from src to dst. + * That is, copy "height" number of lines of "bytewidth" bytes each. + * The first byte of each successive line is separated by *_linesize + * bytes. Skips copying the first n lines. + * + * @param dst_linesize linesize for the image plane in dst + * @param src_linesize linesize for the image plane in src + * @param nlines number of lines to skip before copying + */ +void av_image_copy_plane_skip(uint8_t *dst, int dst_linesize, + const uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines); + +/** * Copy image in src_data to dst_data. * * @param dst_linesizes linesizes for the image in dst_data @@ -117,6 +131,17 @@ enum PixelFormat pix_fmt, int width, int height); /** + * Copy image in src_data to dst_data. + * + * @param dst_linesizes linesizes for the image in dst_data + * @param src_linesizes linesizes for the image in src_data + * @param nlines number of lines to skip before copying + */ +void av_image_copy_skip(uint8_t *dst_data[4], int dst_linesizes[4], + const uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines); + +/** * Setup the data pointers and linesizes based on the specified image * parameters and the provided array. * diff -uNr ffmpeg-vanilla//smvjpegdec.c ffmpeg//smvjpegdec.c --- ffmpeg-vanilla//smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg//smvjpegdec.c 2012-10-01 19:42:32.295480490 +0100 @@ -0,0 +1,177 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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 "smvjpegdec.h" +#include "mjpegdec.h" + +int ff_smvjpeg_decode_init(AVCodecContext *avctx); +int ff_smvjpeg_decode_end(AVCodecContext *avctx); +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + AVPacket *avpkt); + +typedef struct SMVJpegDecodeContext { + MJpegDecodeContext jpg; + AVFrame picture[3]; /* pictures array */ + AVCodecContext* avctx[2]; + AVCodec *codec; + int frames_per_jpeg; + enum PixelFormat pix_fmt; +} SMVJpegDecodeContext; + +av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + SMVCodecExtra* xtra = (SMVCodecExtra*)(avctx->extradata); + + s->frames_per_jpeg = xtra->frames_per_block; + + s->picture[0].reference = s->picture[1].reference = 3; + s->picture[2].reference = 3; + s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + s->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!s->codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx[0] = avctx; + s->avctx[1] = avcodec_alloc_context3(s->codec); + + return 0; +} + +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + int* force_decode = (int*)avpkt->priv; + + /* Are we at the start of a block or forced to decode again? + Also call mjpeg if we start on an odd pts and buf_data isn't + allocated yet. */ + if (cur_frame == 0 || *force_decode == 1 || !buf_data->data[0]) { + *force_decode = 0; /* reset */ + /* we can't open or close the codec in init or end so we have + to do it all here. */ + if (avcodec_open2(s->avctx[1], s->codec, NULL) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } else { + /* Codec opened, create yet another buffer so the avcodec_close + will not wipe it. */ + ret = s->codec->decode(s->avctx[1], mjpeg_data, data_size, avpkt); + s->pix_fmt = s->avctx[1]->pix_fmt; + buf_data->width = s->avctx[1]->width; + buf_data->height = s->avctx[1]->height; + if (buf_data->data[0]) { + av_freep(&buf_data->data[0]); + } + u = av_image_alloc(buf_data->data, buf_data->linesize, + buf_data->width, buf_data->height, s->pix_fmt, 16); + if (u < 0) { + av_log(avctx, AV_LOG_ERROR, + "could not alloc raw picture buffer\n"); + return -1; + } + av_picture_copy((AVPicture*)&s->picture[2], (AVPicture*)mjpeg_data, + s->pix_fmt, buf_data->width, buf_data->height); + avcodec_close(s->avctx[1]); + } + } else { /*use the last lot... */ + *data_size = sizeof(AVPicture); + } + + avctx->pix_fmt = s->pix_fmt; + avcodec_set_dimensions(avctx, buf_data->width, + buf_data->height / s->frames_per_jpeg); + + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + if (u = avctx->get_buffer(avctx, &s->picture[1]) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + av_image_copy_skip(&s->picture[1].data, &s->picture[1].linesize, + buf_data->data, buf_data->linesize, avctx->pix_fmt, avctx->width, + avctx->height, cur_frame); + + *output = s->picture[1]; + + return ret; + +} + +av_cold int ff_smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + if (s->picture[2].data[0]) { + av_freep(&s->picture[2].data[0]); + } + av_free(s->avctx[1]); + return 0; +} + +static const AVOption options[] = { + { NULL }, +}; + +static const AVClass smvjpegdec_class = { + .class_name = "SMVJPEG decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_smvjpeg_decoder = { + .name = "smvjpeg", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_SMVJPEG, + .priv_data_size = sizeof(SMVJpegDecodeContext), + .init = ff_smvjpeg_decode_init, + .close = ff_smvjpeg_decode_end, + .decode = ff_smvjpeg_decode_frame, + .capabilities = CODEC_CAP_DR1, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff -uNr ffmpeg-vanilla//smvjpegdec.h ffmpeg//smvjpegdec.h --- ffmpeg-vanilla//smvjpegdec.h 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg//smvjpegdec.h 2012-10-01 19:42:32.295480490 +0100 @@ -0,0 +1,35 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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. + */ + +#ifndef AVCODEC_SMVJPEG_H +#define AVCODEC_SMVJPEG_H + +typedef struct SMVCodecExtra { + int frames_per_block; +} SMVCodecExtra; + +#endif +
On Tue, Oct 02, 2012 at 11:50:08AM +0100, Ash Hughes wrote:
+ vst->codec->extradata_size = sizeof(SMVCodecExtra); + vst->codec->extradata = av_malloc(sizeof(SMVCodecExtra) + FF_INPUT_BUFFER_PADDING_SIZE);
You can't store extradata directly like that, if you were to mux it like that in a container on a little-endian machine the resulting file would only be readable on a little-endian machine again. And that is not even going into issues like struct alignment which could break things even when just changing compilers.
@@ -617,9 +625,14 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++;
Are you trying to create one packet per final frame here? I don't think that should be done, IMHO it complicates things without much benefit.
+ s->picture[0].reference = s->picture[1].reference = 3; + s->picture[2].reference = 3;
Why? SMV doesn't use reference frames to my knowledge.
+ s->avctx[0] = avctx; + s->avctx[1] = avcodec_alloc_context3(s->codec);
You never actually use s->avctx[0]
+int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt)
The indentation seems a bit strange.
+{
Trailing whitespace can't be committed into the repository.
+ SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + int* force_decode = (int*)avpkt->priv;
I don't think you're supposed to use "priv" that way, it won't work in any external players like MPlayer or VLC. You could use side data, but as said I think this approach is wrong. There should be only one packet per frames_per_jpeg frames. The decoder will then return 0 for all but the first frame to produce multiple frames from one input packet. I suspect this is likely to uncover some bugs though.
+ /* we can't open or close the codec in init or end so we have + to do it all here. */
Why? Also opening/closing the codec is potentially slow, so it really should not be done each time.
+ av_picture_copy((AVPicture*)&s->picture[2], (AVPicture*)mjpeg_data, + s->pix_fmt, buf_data->width, buf_data->height); + av_image_copy_skip(&s->picture[1].data, &s->picture[1].linesize, + buf_data->data, buf_data->linesize, avctx->pix_fmt, avctx->width, + avctx->height, cur_frame);
This is 2 copies more than what should be necessary. The first obviously can be avoided by fixing things so you can open/close the JPEG decoder in the open/close decoder functions. The second can be avoided by not setting CODEC_CAP_DR1 and just exporting the appropriate part of the decoded frame. The whole point of CODEC_CAP_DR1 is to avoid copying, if you end up doing a copy in the codec because of it you do the opposite of what its purpose it. You are by far not the only one to misunderstand this (well, I assume you misunderstood it), so suggestions for documenting this better are welcome.
+ if (s->picture[2].data[0]) { + av_freep(&s->picture[2].data[0]); + }
The if() is unnecessary.
Date: Tue, 2 Oct 2012 21:17:35 +0200 From: Reimar.Doeffinger@gmx.de To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH] Support playing SMV files.
On Tue, Oct 02, 2012 at 11:50:08AM +0100, Ash Hughes wrote:
+ vst->codec->extradata_size = sizeof(SMVCodecExtra); + vst->codec->extradata = av_malloc(sizeof(SMVCodecExtra) + FF_INPUT_BUFFER_PADDING_SIZE);
You can't store extradata directly like that, if you were to mux it like that in a container on a little-endian machine the resulting file would only be readable on a little-endian machine again. And that is not even going into issues like struct alignment which could break things even when just changing compilers.
alright, I'll look more closely at how other codecs handle extradata
@@ -617,9 +625,14 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++;
Are you trying to create one packet per final frame here? I don't think that should be done, IMHO it complicates things without much benefit.
My intention was to have it reuse packets, like you mention below, but I got it wrong :S
+ s->picture[0].reference = s->picture[1].reference = 3; + s->picture[2].reference = 3;
Why? SMV doesn't use reference frames to my knowledge.
ok!
+ s->avctx[0] = avctx; + s->avctx[1] = avcodec_alloc_context3(s->codec);
You never actually use s->avctx[0]
other codecs (appear to me to) do it, e.g. kgv1dec.c so I assumed it was necessary.
+int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt)
The indentation seems a bit strange.
missing two spaces? copied from mjpegdec and renamed...
+{
Trailing whitespace can't be committed into the repository.
oops!
+ SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + int* force_decode = (int*)avpkt->priv;
I don't think you're supposed to use "priv" that way, it won't work in any external players like MPlayer or VLC. You could use side data, but as said I think this approach is wrong. There should be only one packet per frames_per_jpeg frames. The decoder will then return 0 for all but the first frame to produce multiple frames from one input packet. I suspect this is likely to uncover some bugs though.
ok, will rework.
+ /* we can't open or close the codec in init or end so we have + to do it all here. */
Why? Also opening/closing the codec is potentially slow, so it really should not be done each time.
I know :( The reason for this is that if you do it in init or end, you get "insufficient thread locking around avcodec_open/close()". In libavcodec/utils.c, in avcodec_open2, the line: if (entangled_thread_counter != 1) is what triggers this. We probably don't want to call somebodies lock function twice if they supply one and I'm not sure exactly what it is protecting so I don't know how to get round this neatly. Any advice would be much appreciated!
+ av_picture_copy((AVPicture*)&s->picture[2], (AVPicture*)mjpeg_data, + s->pix_fmt, buf_data->width, buf_data->height); + av_image_copy_skip(&s->picture[1].data, &s->picture[1].linesize, + buf_data->data, buf_data->linesize, avctx->pix_fmt, avctx->width, + avctx->height, cur_frame);
This is 2 copies more than what should be necessary. The first obviously can be avoided by fixing things so you can open/close the JPEG decoder in the open/close decoder functions. The second can be avoided by not setting CODEC_CAP_DR1 and just exporting the appropriate part of the decoded frame. The whole point of CODEC_CAP_DR1 is to avoid copying, if you end up doing a copy in the codec because of it you do the opposite of what its purpose it. You are by far not the only one to misunderstand this (well, I assume you misunderstood it), so suggestions for documenting this better are welcome.
Yes, I've misunderstood this, I'll have another read of documentation.
+ if (s->picture[2].data[0]) { + av_freep(&s->picture[2].data[0]); + }
The if() is unnecessary.
ok, and thanks for the feedback :)
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Hi, Here is an update to the SMV patch. For using one packet for multiple frames, I don't know how to get it to send the same packet multiple times to the decode_frame function. Also, the documentation at http://ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#g4fdb3084415a82e3... implies that this can't be done anyway, as there is only one video Frame per packet. Perhaps there's a property on AVPacket that I'm missing? With the threading issue, I wonder if avcodec_open could be split into a mutexed public part and an unmutexed but private part? This seems messy and potentially dangerous to me though, maybe it would be better to accept the inefficiency for this (rarely used) codec? Thanks, --- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-01 19:34:06.944974579 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-01 19:36:15.533611881 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-01 19:34:06.956974647 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-01 19:36:15.537611963 +0100 @@ -282,6 +282,7 @@ AV_CODEC_ID_PAF_VIDEO = MKBETAG('P','A','F','V'), AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), + AV_CODEC_ID_SMVJPEG, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-01 19:34:06.928974504 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-01 19:36:15.537611963 +0100 @@ -388,6 +388,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-06 13:59:15.540183753 +0100 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext; #if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,9 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +629,12 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -678,6 +694,7 @@ else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; } st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-06 13:59:33.880274691 +0100 @@ -0,0 +1,220 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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" + +int ff_smvjpeg_decode_init(AVCodecContext *avctx); +int ff_smvjpeg_decode_end(AVCodecContext *avctx); +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + AVPacket *avpkt); +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines); +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines); + +typedef struct SMVJpegDecodeContext { + MJpegDecodeContext jpg; + AVFrame picture[3]; /* pictures array */ + AVCodecContext* avctx; + AVCodec *codec; + int frames_per_jpeg; + enum PixelFormat pix_fmt; +} SMVJpegDecodeContext; + +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines) +{ + if (!dst || !src) + return; + src += (nlines) * src_linesize * height; + *dst = src; +} + +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + + if (desc->flags & PIX_FMT_HWACCEL) + return; + + if (desc->flags & PIX_FMT_PAL || + desc->flags & PIX_FMT_PSEUDOPAL) { + smv_image_pnt_plane(&dst_data[0], dst_linesizes[0], + src_data[0], src_linesizes[0], + width, height, nlines); + /* copy the palette */ + dst_data[1] = src_data[1]; + } else { + int i, planes_nb = 0; + + 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; + int bwidth = av_image_get_linesize(pix_fmt, width, i); + if (i == 1 || i == 2) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_image_pnt_plane(&dst_data[i], dst_linesizes[i], + src_data[i], src_linesizes[i], + bwidth, h, nlines); + } + } +} + +av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + + s->frames_per_jpeg = AV_RL32(avctx->extradata); + s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + s->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!s->codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx = avcodec_alloc_context3(s->codec); + + return 0; +} + +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u, i; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? + Also call mjpeg if we start on an odd pts and buf_data isn't + allocated yet. */ + if (cur_frame == 0 || !buf_data->data[0]) { + /* we can't open or close the codec in init or end so we have + to do it all here. */ + if (avcodec_open2(s->avctx, s->codec, NULL) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } else { + /* Codec opened, create yet another buffer so the avcodec_close + will not wipe it. */ + ret = s->codec->decode(s->avctx, mjpeg_data, data_size, avpkt); + s->pix_fmt = s->avctx->pix_fmt; + buf_data->width = s->avctx->width; + buf_data->height = s->avctx->height; + av_freep(&buf_data->data[0]); + u = av_image_alloc(buf_data->data, buf_data->linesize, + buf_data->width, buf_data->height, s->pix_fmt, 16); + if (u < 0) { + av_log(avctx, AV_LOG_ERROR, + "could not alloc raw picture buffer\n"); + return -1; + } + av_picture_copy((AVPicture*)&s->picture[2], (AVPicture*)mjpeg_data, + s->pix_fmt, buf_data->width, buf_data->height); + avcodec_close(s->avctx); + } + } else { /*use the last lot... */ + *data_size = sizeof(AVPicture); + } + + avctx->pix_fmt = s->pix_fmt; + avcodec_set_dimensions(avctx, buf_data->width, + buf_data->height / s->frames_per_jpeg); + + s->picture[1].type = FF_BUFFER_TYPE_INTERNAL; + s->picture[1].extended_data = s->picture[1].data; + s->picture[1].width = buf_data->width; + s->picture[1].height = avctx->height; + s->picture[1].format = avctx->pix_fmt; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + smv_image_pnt(s->picture[1].data, s->picture[1].linesize, + buf_data->data, buf_data->linesize, avctx->pix_fmt, avctx->width, + avctx->height, cur_frame); + for (i = 0; i < AV_NUM_DATA_POINTERS; i++) { + s->picture[1].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = buf_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; + +} + +av_cold int ff_smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + av_freep(&s->picture[2].data[0]); + av_free(s->avctx); + return 0; +} + +static const AVOption options[] = { + { NULL }, +}; + +static const AVClass smvjpegdec_class = { + .class_name = "SMVJPEG decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_smvjpeg_decoder = { + .name = "smvjpeg", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_SMVJPEG, + .priv_data_size = sizeof(SMVJpegDecodeContext), + .init = ff_smvjpeg_decode_init, + .close = ff_smvjpeg_decode_end, + .decode = ff_smvjpeg_decode_frame, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-01 19:34:06.944974579 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-01 19:36:15.533611881 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-01 19:34:06.956974647 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-01 19:36:15.537611963 +0100 @@ -282,6 +282,7 @@ AV_CODEC_ID_PAF_VIDEO = MKBETAG('P','A','F','V'), AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), + AV_CODEC_ID_SMVJPEG, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-01 19:34:06.928974504 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-01 19:36:15.537611963 +0100 @@ -388,6 +388,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-06 13:59:15.540183753 +0100 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext; #if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,9 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +629,12 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -678,6 +694,7 @@ else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; } st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-07 00:07:37.005184736 +0100 @@ -0,0 +1,220 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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" + +int ff_smvjpeg_decode_init(AVCodecContext *avctx); +int ff_smvjpeg_decode_end(AVCodecContext *avctx); +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + AVPacket *avpkt); +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines); +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines); + +typedef struct SMVJpegDecodeContext { + MJpegDecodeContext jpg; + AVFrame picture[3]; /* pictures array */ + AVCodecContext* avctx; + AVCodec *codec; + int frames_per_jpeg; + enum PixelFormat pix_fmt; +} SMVJpegDecodeContext; + +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines) +{ + if (!dst || !src) + return; + src += (nlines) * src_linesize * height; + *dst = src; +} + +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + + if (desc->flags & PIX_FMT_HWACCEL) + return; + + if (desc->flags & PIX_FMT_PAL || + desc->flags & PIX_FMT_PSEUDOPAL) { + smv_image_pnt_plane(&dst_data[0], dst_linesizes[0], + src_data[0], src_linesizes[0], + width, height, nlines); + /* copy the palette */ + dst_data[1] = src_data[1]; + } else { + int i, planes_nb = 0; + + 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; + int bwidth = av_image_get_linesize(pix_fmt, width, i); + if (i == 1 || i == 2) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_image_pnt_plane(&dst_data[i], dst_linesizes[i], + src_data[i], src_linesizes[i], + bwidth, h, nlines); + } + } +} + +av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + + s->frames_per_jpeg = AV_RL32(avctx->extradata); + s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + s->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!s->codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx = avcodec_alloc_context3(s->codec); + + return 0; +} + +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u, i; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? + Also call mjpeg if we start on an odd pts and buf_data isn't + allocated yet. */ + if (cur_frame == 0 || !buf_data->data[0]) { + /* we can't open or close the codec in init or end so we have + to do it all here. */ + if (avcodec_open2(s->avctx, s->codec, NULL) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } else { + /* Codec opened, create yet another buffer so the avcodec_close + will not wipe it. */ + ret = s->codec->decode(s->avctx, mjpeg_data, data_size, avpkt); + s->pix_fmt = s->avctx->pix_fmt; + buf_data->width = s->avctx->width; + buf_data->height = s->avctx->height; + av_freep(&buf_data->data[0]); + u = av_image_alloc(buf_data->data, buf_data->linesize, + buf_data->width, buf_data->height, s->pix_fmt, 16); + if (u < 0) { + av_log(avctx, AV_LOG_ERROR, + "could not alloc raw picture buffer\n"); + return -1; + } + av_picture_copy((AVPicture*)&s->picture[2], (AVPicture*)mjpeg_data, + s->pix_fmt, buf_data->width, buf_data->height); + avcodec_close(s->avctx); + } + } else { /*use the last lot... */ + *data_size = sizeof(AVPicture); + } + + avctx->pix_fmt = s->pix_fmt; + avcodec_set_dimensions(avctx, buf_data->width, + buf_data->height / s->frames_per_jpeg); + + s->picture[1].type = FF_BUFFER_TYPE_INTERNAL; + s->picture[1].extended_data = s->picture[1].data; + s->picture[1].width = buf_data->width; + s->picture[1].height = avctx->height; + s->picture[1].format = avctx->pix_fmt; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + smv_image_pnt(s->picture[1].data, s->picture[1].linesize, + buf_data->data, buf_data->linesize, avctx->pix_fmt, avctx->width, + avctx->height, cur_frame); + for (i = 0; i < AV_NUM_DATA_POINTERS; i++) { + s->picture[1].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = buf_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; + +} + +av_cold int ff_smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + av_freep(&s->picture[2].data[0]); + av_free(s->avctx); + return 0; +} + +static const AVOption options[] = { + { NULL }, +}; + +static const AVClass smvjpegdec_class = { + .class_name = "SMVJPEG decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_smvjpeg_decoder = { + .name = "smvjpeg", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_SMVJPEG, + .priv_data_size = sizeof(SMVJpegDecodeContext), + .init = ff_smvjpeg_decode_init, + .close = ff_smvjpeg_decode_end, + .decode = ff_smvjpeg_decode_frame, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
From: ashes-iontach@hotmail.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 2 Oct 2012 23:05:25 +0000 Subject: Re: [FFmpeg-devel] [PATCH] Support playing SMV files.
Date: Tue, 2 Oct 2012 21:17:35 +0200 From: Reimar.Doeffinger@gmx.de To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH] Support playing SMV files.
On Tue, Oct 02, 2012 at 11:50:08AM +0100, Ash Hughes wrote:
+ vst->codec->extradata_size = sizeof(SMVCodecExtra); + vst->codec->extradata = av_malloc(sizeof(SMVCodecExtra) + FF_INPUT_BUFFER_PADDING_SIZE);
You can't store extradata directly like that, if you were to mux it like that in a container on a little-endian machine the resulting file would only be readable on a little-endian machine again. And that is not even going into issues like struct alignment which could break things even when just changing compilers.
alright, I'll look more closely at how other codecs handle extradata
@@ -617,9 +625,14 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++;
Are you trying to create one packet per final frame here? I don't think that should be done, IMHO it complicates things without much benefit.
My intention was to have it reuse packets, like you mention below, but I got it wrong :S
+ s->picture[0].reference = s->picture[1].reference = 3; + s->picture[2].reference = 3;
Why? SMV doesn't use reference frames to my knowledge.
ok!
+ s->avctx[0] = avctx; + s->avctx[1] = avcodec_alloc_context3(s->codec);
You never actually use s->avctx[0]
other codecs (appear to me to) do it, e.g. kgv1dec.c so I assumed it was necessary.
+int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt)
The indentation seems a bit strange.
missing two spaces? copied from mjpegdec and renamed...
+{
Trailing whitespace can't be committed into the repository.
oops!
+ SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + int* force_decode = (int*)avpkt->priv;
I don't think you're supposed to use "priv" that way, it won't work in any external players like MPlayer or VLC. You could use side data, but as said I think this approach is wrong. There should be only one packet per frames_per_jpeg frames. The decoder will then return 0 for all but the first frame to produce multiple frames from one input packet. I suspect this is likely to uncover some bugs though.
ok, will rework.
+ /* we can't open or close the codec in init or end so we have + to do it all here. */
Why? Also opening/closing the codec is potentially slow, so it really should not be done each time.
I know :( The reason for this is that if you do it in init or end, you get "insufficient thread locking around avcodec_open/close()". In libavcodec/utils.c, in avcodec_open2, the line:
if (entangled_thread_counter != 1)
is what triggers this. We probably don't want to call somebodies lock function twice if they supply one and I'm not sure exactly what it is protecting so I don't know how to get round this neatly. Any advice would be much appreciated!
+ av_picture_copy((AVPicture*)&s->picture[2], (AVPicture*)mjpeg_data, + s->pix_fmt, buf_data->width, buf_data->height); + av_image_copy_skip(&s->picture[1].data, &s->picture[1].linesize, + buf_data->data, buf_data->linesize, avctx->pix_fmt, avctx->width, + avctx->height, cur_frame);
This is 2 copies more than what should be necessary. The first obviously can be avoided by fixing things so you can open/close the JPEG decoder in the open/close decoder functions. The second can be avoided by not setting CODEC_CAP_DR1 and just exporting the appropriate part of the decoded frame. The whole point of CODEC_CAP_DR1 is to avoid copying, if you end up doing a copy in the codec because of it you do the opposite of what its purpose it. You are by far not the only one to misunderstand this (well, I assume you misunderstood it), so suggestions for documenting this better are welcome.
Yes, I've misunderstood this, I'll have another read of documentation.
+ if (s->picture[2].data[0]) { + av_freep(&s->picture[2].data[0]); + }
The if() is unnecessary.
ok, and thanks for the feedback :)
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Sat, Oct 06, 2012 at 11:20:03PM +0000, Ash Hughes wrote:
Hi,
Here is an update to the SMV patch. For using one packet for multiple frames, I don't know how to get it to send the same packet multiple times to the decode_frame function. Also, the documentation at http://ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#g4fdb3084415a82e3... implies that this can't be done anyway, as there is only one video Frame per packet. Perhaps there's a property on AVPacket that I'm missing?
With the threading issue, I wonder if avcodec_open could be split into a mutexed public part and an unmutexed but private part? This seems messy and potentially dangerous to me though, maybe it would be better to accept the inefficiency for this (rarely used) codec?
calling avcodec_open2() from decode_frame is not an entirely safe act from a quick look unlocking the mutex and calling it from init seems safer. Maybe you could add a avcodec_open2_recursiv() that does this [...]
@@ -518,6 +527,9 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + vst->codec->height /= wav->smv_frames_per_jpeg;
division by 0 [...]
+void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + + if (desc->flags & PIX_FMT_HWACCEL) + return; + + if (desc->flags & PIX_FMT_PAL || + desc->flags & PIX_FMT_PSEUDOPAL) { + smv_image_pnt_plane(&dst_data[0], dst_linesizes[0], + src_data[0], src_linesizes[0], + width, height, nlines); + /* copy the palette */ + dst_data[1] = src_data[1];
is there some case that uses a paletted format ?
+ } else { + int i, planes_nb = 0; + + 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; + int bwidth = av_image_get_linesize(pix_fmt, width, i); + if (i == 1 || i == 2) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_image_pnt_plane(&dst_data[i], dst_linesizes[i], + src_data[i], src_linesizes[i], + bwidth, h, nlines); + } + } +} + +av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + + s->frames_per_jpeg = AV_RL32(avctx->extradata); + s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + s->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!s->codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx = avcodec_alloc_context3(s->codec); + + return 0; +} + +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u, i; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? + Also call mjpeg if we start on an odd pts and buf_data isn't + allocated yet. */ + if (cur_frame == 0 || !buf_data->data[0]) { + /* we can't open or close the codec in init or end so we have + to do it all here. */ + if (avcodec_open2(s->avctx, s->codec, NULL) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } else { + /* Codec opened, create yet another buffer so the avcodec_close + will not wipe it. */ + ret = s->codec->decode(s->avctx, mjpeg_data, data_size, avpkt);
this is not correct you cant mix highlevel (avcodec_open2) and lowlevel (codec->decode()) calls also make sure that the internal decoder you create gets threads=1 set, running it with multithreading could cause problems [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I am the wisest man alive, for I know one thing, and that is that I know nothing. -- Socrates
Thanks for the feedback, here's an update with fixes from your comments. Ash --- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-01 19:34:06.944974579 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-01 19:36:15.533611881 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-01 19:34:06.956974647 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-11 15:23:28.037828767 +0100 @@ -282,6 +282,7 @@ AV_CODEC_ID_PAF_VIDEO = MKBETAG('P','A','F','V'), AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), + AV_CODEC_ID_SMVJPEG, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-01 19:34:06.928974504 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-01 19:36:15.537611963 +0100 @@ -388,6 +388,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-11 15:08:30.253376898 +0100 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext; #if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-11 15:40:10.854801495 +0100 @@ -0,0 +1,203 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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" + +int ff_smvjpeg_decode_init(AVCodecContext *avctx); +int ff_smvjpeg_decode_end(AVCodecContext *avctx); +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + AVPacket *avpkt); +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines); +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines); + +typedef struct SMVJpegDecodeContext { + MJpegDecodeContext jpg; + AVFrame picture[2]; /* pictures array */ + AVCodecContext* avctx; + int frames_per_jpeg; +} SMVJpegDecodeContext; + +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines) +{ + if (!dst || !src) + return; + src += (nlines) * src_linesize * height; + *dst = src; +} + +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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; + int bwidth = av_image_get_linesize(pix_fmt, width, i); + if (i == 1 || i == 2) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_image_pnt_plane(&dst_data[i], dst_linesizes[i], src_data[i], + src_linesizes[i], bwidth, h, nlines); + } +} + +av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + + s->frames_per_jpeg = AV_RL32(avctx->extradata); + s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (avcodec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } + av_dict_free(&thread_opt); + + return 0; +} + +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, new_height; + int cur_frame = 0; + + if (s->frames_per_jpeg > 0) + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (cur_frame == 0) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + new_height = mjpeg_data->height; + if (s->frames_per_jpeg > 0) { + if (new_height % s->frames_per_jpeg != 0) { + av_log(avctx, AV_LOG_WARNING, + "There should be an integer number of frames per jpeg.\n"); + } + new_height /= s->frames_per_jpeg; + } else { + av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n"); + ret = -1; + } + + avcodec_set_dimensions(avctx, mjpeg_data->width, new_height); + + s->picture[1].type = FF_BUFFER_TYPE_INTERNAL; + s->picture[1].extended_data = s->picture[1].data; + s->picture[1].width = avctx->width; + s->picture[1].height = avctx->height; + s->picture[1].format = avctx->pix_fmt; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + smv_image_pnt(s->picture[1].data, s->picture[1].linesize, + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; + +} + +av_cold int ff_smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + avcodec_close_recursive(s->avctx); + av_free(s->avctx); + return 0; +} + +static const AVOption options[] = { + { NULL }, +}; + +static const AVClass smvjpegdec_class = { + .class_name = "SMVJPEG decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_smvjpeg_decoder = { + .name = "smvjpeg", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_SMVJPEG, + .priv_data_size = sizeof(SMVJpegDecodeContext), + .init = ff_smvjpeg_decode_init, + .close = ff_smvjpeg_decode_end, + .decode = ff_smvjpeg_decode_frame, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff -uNr ffmpeg-vanilla/libavcodec/utils.c ffmpeg/libavcodec/utils.c --- ffmpeg-vanilla/libavcodec/utils.c 2012-10-01 19:34:06.932974542 +0100 +++ ffmpeg/libavcodec/utils.c 2012-10-11 14:28:35.269500814 +0100 @@ -796,6 +796,27 @@ } #endif +int attribute_align_arg avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) +{ + int ret = 0; + + entangled_thread_counter--; + /* Release any user-supplied mutex. */ + if (ff_lockmgr_cb) { + (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); + } + + ret = avcodec_open2(avctx, codec, options); + + /* If there is a user-supplied mutex locking routine, call it. */ + if (ff_lockmgr_cb) { + if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) + return -1; + } + entangled_thread_counter++; + return ret; +} + int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) { int ret = 0; @@ -1796,6 +1817,27 @@ memset(sub, 0, sizeof(AVSubtitle)); } +av_cold int avcodec_close_recursive(AVCodecContext *avctx) +{ + int ret = 0; + + entangled_thread_counter--; + /* Release any user-supplied mutex. */ + if (ff_lockmgr_cb) { + (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); + } + + ret = avcodec_close(avctx); + + /* If there is a user-supplied mutex locking routine, call it. */ + if (ff_lockmgr_cb) { + if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) + return -1; + } + entangled_thread_counter++; + return ret; +} + av_cold int avcodec_close(AVCodecContext *avctx) { /* If there is a user-supplied mutex locking routine, call it. */ diff -uNr ffmpeg-vanilla/libavcodec/internal.h ffmpeg/libavcodec/internal.h --- ffmpeg-vanilla/libavcodec/internal.h 2012-10-01 19:34:06.928974504 +0100 +++ ffmpeg/libavcodec/internal.h 2012-10-11 16:20:17.890737307 +0100 @@ -178,4 +178,16 @@ int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx); +/** + * Call avcodec_open2 recursively by decrementing counter, unlocking mutex, + * calling the function and then restoring again. Assumes the mutex is + * already locked + */ +int avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); + +/** + * Call avcodec_close recursively, counterpart to avcodec_open2_recursive. + */ +int avcodec_close_recursive(AVCodecContext *avctx); + #endif /* AVCODEC_INTERNAL_H */ -- Date: Wed, 10 Oct 2012 03:07:19 +0200 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH] Support playing SMV files. On Sat, Oct 06, 2012 at 11:20:03PM +0000, Ash Hughes wrote:
Hi,
Here is an update to the SMV patch. For using one packet for multiple frames, I don't know how to get it to send the same packet multiple times to the decode_frame function. Also, the documentation at http://ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#g4fdb3084415a82e3... implies that this can't be done anyway, as there is only one video Frame per packet. Perhaps there's a property on AVPacket that I'm missing?
With the threading issue, I wonder if avcodec_open could be split into a mutexed public part and an unmutexed but private part? This seems messy and potentially dangerous to me though, maybe it would be better to accept the inefficiency for this (rarely used) codec?
calling avcodec_open2() from decode_frame is not an entirely safe act from a quick look unlocking the mutex and calling it from init seems safer. Maybe you could add a avcodec_open2_recursiv() that does this [...]
@@ -518,6 +527,9 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + vst->codec->height /= wav->smv_frames_per_jpeg;
division by 0 [...]
+void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + + if (desc->flags & PIX_FMT_HWACCEL) + return; + + if (desc->flags & PIX_FMT_PAL || + desc->flags & PIX_FMT_PSEUDOPAL) { + smv_image_pnt_plane(&dst_data[0], dst_linesizes[0], + src_data[0], src_linesizes[0], + width, height, nlines); + /* copy the palette */ + dst_data[1] = src_data[1];
is there some case that uses a paletted format ?
+ } else { + int i, planes_nb = 0; + + 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; + int bwidth = av_image_get_linesize(pix_fmt, width, i); + if (i == 1 || i == 2) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_image_pnt_plane(&dst_data[i], dst_linesizes[i], + src_data[i], src_linesizes[i], + bwidth, h, nlines); + } + } +} + +av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + + s->frames_per_jpeg = AV_RL32(avctx->extradata); + s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + s->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!s->codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx = avcodec_alloc_context3(s->codec); + + return 0; +} + +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* buf_data = &s->picture[2]; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int u, i; + int cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? + Also call mjpeg if we start on an odd pts and buf_data isn't + allocated yet. */ + if (cur_frame == 0 || !buf_data->data[0]) { + /* we can't open or close the codec in init or end so we have + to do it all here. */ + if (avcodec_open2(s->avctx, s->codec, NULL) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } else { + /* Codec opened, create yet another buffer so the avcodec_close + will not wipe it. */ + ret = s->codec->decode(s->avctx, mjpeg_data, data_size, avpkt);
this is not correct you cant mix highlevel (avcodec_open2) and lowlevel (codec->decode()) calls also make sure that the internal decoder you create gets threads=1 set, running it with multithreading could cause problems [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I am the wisest man alive, for I know one thing, and that is that I know nothing. -- Socrates _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Thu, Oct 11, 2012 at 05:06:07PM +0000, Ash Hughes wrote:
Thanks for the feedback, here's an update with fixes from your comments.
Ash --- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-01 19:34:06.944974579 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-01 19:36:15.533611881 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast);
diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-01 19:34:06.956974647 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-11 15:23:28.037828767 +0100 @@ -282,6 +282,7 @@ AV_CODEC_ID_PAF_VIDEO = MKBETAG('P','A','F','V'), AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), + AV_CODEC_ID_SMVJPEG,
missing MKBETAG
/* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-01 19:34:06.928974504 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-01 19:36:15.537611963 +0100 @@ -388,6 +388,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-11 15:08:30.253376898 +0100 @@ -25,6 +25,7 @@
#include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext;
#if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (wav->smv_cur_pt == 0) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } }
st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-11 15:40:10.854801495 +0100 @@ -0,0 +1,203 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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" + +int ff_smvjpeg_decode_init(AVCodecContext *avctx); +int ff_smvjpeg_decode_end(AVCodecContext *avctx); +int ff_smvjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + AVPacket *avpkt); +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines); +void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines); + +typedef struct SMVJpegDecodeContext { + MJpegDecodeContext jpg; + AVFrame picture[2]; /* pictures array */ + AVCodecContext* avctx; + int frames_per_jpeg; +} SMVJpegDecodeContext; +
+void smv_image_pnt_plane(uint8_t **dst, int dst_linesize, + uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines) +{ + if (!dst || !src) + return; + src += (nlines) * src_linesize * height; + *dst = src; +}
this is used just once, so certainly should be static, but maybe inlining it would be even clearer
+ +void smv_image_pnt(uint8_t *dst_data[4], int dst_linesizes[4], + uint8_t *src_data[4], const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, int nlines)
this too is used just once
+{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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; + int bwidth = av_image_get_linesize(pix_fmt, width, i); + if (i == 1 || i == 2) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_image_pnt_plane(&dst_data[i], dst_linesizes[i], src_data[i], + src_linesizes[i], bwidth, h, nlines); + } +} +
+av_cold int ff_smvjpeg_decode_init(AVCodecContext *avctx)
doesnt need ff_ prefix and can be static (applies to more functions)
+{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; +
+ s->frames_per_jpeg = AV_RL32(avctx->extradata);
this is missing a check on extradata_size
+ s->jpg.picture_ptr = &s->picture[0]; + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (avcodec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + return -1; + } + av_dict_free(&thread_opt); + + return 0; +} +
+int ff_smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + int ret = 0; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, new_height; + int cur_frame = 0; + + if (s->frames_per_jpeg > 0) + cur_frame = avpkt->pts % s->frames_per_jpeg;
does it make sense to not just fail in init for frames_per_jpeg <=0 ?
+ + /* Are we at the start of a block? */ + if (cur_frame == 0) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + new_height = mjpeg_data->height; + if (s->frames_per_jpeg > 0) { + if (new_height % s->frames_per_jpeg != 0) { + av_log(avctx, AV_LOG_WARNING, + "There should be an integer number of frames per jpeg.\n"); + } + new_height /= s->frames_per_jpeg; + } else { + av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n"); + ret = -1; + } + + avcodec_set_dimensions(avctx, mjpeg_data->width, new_height); +
+ s->picture[1].type = FF_BUFFER_TYPE_INTERNAL;
why do you need this ?
+ s->picture[1].extended_data = s->picture[1].data;
this is likely a bad idea, simply setting extended_data to NULL is safer
+ s->picture[1].width = avctx->width; + s->picture[1].height = avctx->height; + s->picture[1].format = avctx->pix_fmt; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + smv_image_pnt(s->picture[1].data, s->picture[1].linesize, + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; + +} + +av_cold int ff_smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + avcodec_close_recursive(s->avctx);
+ av_free(s->avctx);
av_freep() is safer
+ return 0; +} +
+static const AVOption options[] = { + { NULL }, +};
shouldnt be needed if its empty
+ +static const AVClass smvjpegdec_class = { + .class_name = "SMVJPEG decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_smvjpeg_decoder = { + .name = "smvjpeg", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_SMVJPEG, + .priv_data_size = sizeof(SMVJpegDecodeContext),
+ .init = ff_smvjpeg_decode_init, + .close = ff_smvjpeg_decode_end, + .decode = ff_smvjpeg_decode_frame,
+ .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
diff -uNr ffmpeg-vanilla/libavcodec/utils.c ffmpeg/libavcodec/utils.c --- ffmpeg-vanilla/libavcodec/utils.c 2012-10-01 19:34:06.932974542 +0100 +++ ffmpeg/libavcodec/utils.c 2012-10-11 14:28:35.269500814 +0100 @@ -796,6 +796,27 @@ } #endif
+int attribute_align_arg avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) +{ + int ret = 0;
this should be in a seperate patch [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Complexity theory is the science of finding the exact solution to an approximation. Benchmarking OTOH is finding an approximation of the exact
This patch updates libavcodec to enable recursively using avcodec_open2/close. Used in the SMV decoder to open an MJPEG decoder. Ash --- diff -uNr ffmpeg-vanilla/libavcodec/utils.c ffmpeg/libavcodec/utils.c --- ffmpeg-vanilla/libavcodec/utils.c 2012-10-16 18:09:41.283863114 +0100 +++ ffmpeg/libavcodec/utils.c 2012-10-16 17:58:22.784498604 +0100 @@ -798,6 +798,27 @@ } #endif +int attribute_align_arg avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) +{ + int ret = 0; + + entangled_thread_counter--; + /* Release any user-supplied mutex. */ + if (ff_lockmgr_cb) { + (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); + } + + ret = avcodec_open2(avctx, codec, options); + + /* If there is a user-supplied mutex locking routine, call it. */ + if (ff_lockmgr_cb) { + if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) + return -1; + } + entangled_thread_counter++; + return ret; +} + int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) { int ret = 0; @@ -1809,6 +1830,27 @@ memset(sub, 0, sizeof(AVSubtitle)); } +av_cold int avcodec_close_recursive(AVCodecContext *avctx) +{ + int ret = 0; + + entangled_thread_counter--; + /* Release any user-supplied mutex. */ + if (ff_lockmgr_cb) { + (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); + } + + ret = avcodec_close(avctx); + + /* If there is a user-supplied mutex locking routine, call it. */ + if (ff_lockmgr_cb) { + if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) + return -1; + } + entangled_thread_counter++; + return ret; +} + av_cold int avcodec_close(AVCodecContext *avctx) { /* If there is a user-supplied mutex locking routine, call it. */ diff -uNr ffmpeg-vanilla/libavcodec/internal.h ffmpeg/libavcodec/internal.h --- ffmpeg-vanilla/libavcodec/internal.h 2012-10-16 18:09:41.247862923 +0100 +++ ffmpeg/libavcodec/internal.h 2012-10-16 17:58:22.764498508 +0100 @@ -178,4 +178,16 @@ int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx); +/** + * Call avcodec_open2 recursively by decrementing counter, unlocking mutex, + * calling the function and then restoring again. Assumes the mutex is + * already locked + */ +int avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); + +/** + * Call avcodec_close recursively, counterpart to avcodec_open2_recursive. + */ +int avcodec_close_recursive(AVCodecContext *avctx); + #endif /* AVCODEC_INTERNAL_H */
On Tue, Oct 16, 2012 at 06:48:06PM +0000, Ash Hughes wrote:
This patch updates libavcodec to enable recursively using avcodec_open2/close. Used in the SMV decoder to open an MJPEG decoder.
Ash
--- diff -uNr ffmpeg-vanilla/libavcodec/utils.c ffmpeg/libavcodec/utils.c --- ffmpeg-vanilla/libavcodec/utils.c 2012-10-16 18:09:41.283863114 +0100 +++ ffmpeg/libavcodec/utils.c 2012-10-16 17:58:22.784498604 +0100 @@ -798,6 +798,27 @@ } #endif
+int attribute_align_arg avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
av prefixes are for external API, this is (at least currently) internal API so should have a ff prefix instead [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Awnsering whenever a program halts or runs forever is On a turing machine, in general impossible (turings halting problem). On any real computer, always possible as a real computer has a finite number of states N, and will either halt in less than N cycles or never halt.
Renamed functions to follow convention Ash --- diff -uNr ffmpeg-vanilla/libavcodec/utils.c ffmpeg/libavcodec/utils.c --- ffmpeg-vanilla/libavcodec/utils.c 2012-10-16 18:09:41.283863114 +0100 +++ ffmpeg/libavcodec/utils.c 2012-10-19 01:48:02.120851863 +0100 @@ -798,6 +798,27 @@ } #endif +int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) +{ + int ret = 0; + + entangled_thread_counter--; + /* Release any user-supplied mutex. */ + if (ff_lockmgr_cb) { + (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); + } + + ret = avcodec_open2(avctx, codec, options); + + /* If there is a user-supplied mutex locking routine, call it. */ + if (ff_lockmgr_cb) { + if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) + return -1; + } + entangled_thread_counter++; + return ret; +} + int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) { int ret = 0; @@ -1809,6 +1830,27 @@ memset(sub, 0, sizeof(AVSubtitle)); } +av_cold int ff_codec_close_recursive(AVCodecContext *avctx) +{ + int ret = 0; + + entangled_thread_counter--; + /* Release any user-supplied mutex. */ + if (ff_lockmgr_cb) { + (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); + } + + ret = avcodec_close(avctx); + + /* If there is a user-supplied mutex locking routine, call it. */ + if (ff_lockmgr_cb) { + if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) + return -1; + } + entangled_thread_counter++; + return ret; +} + av_cold int avcodec_close(AVCodecContext *avctx) { /* If there is a user-supplied mutex locking routine, call it. */ diff -uNr ffmpeg-vanilla/libavcodec/internal.h ffmpeg/libavcodec/internal.h --- ffmpeg-vanilla/libavcodec/internal.h 2012-10-16 18:09:41.247862923 +0100 +++ ffmpeg/libavcodec/internal.h 2012-10-19 01:46:35.420421944 +0100 @@ -178,4 +178,16 @@ int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx); +/** + * Call avcodec_open2 recursively by decrementing counter, unlocking mutex, + * calling the function and then restoring again. Assumes the mutex is + * already locked + */ +int ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); + +/** + * Call avcodec_close recursively, counterpart to avcodec_open2_recursive. + */ +int ff_codec_close_recursive(AVCodecContext *avctx); + #endif /* AVCODEC_INTERNAL_H */ Date: Thu, 18 Oct 2012 01:21:32 +0200 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 1/2] Support playing SMV files. On Tue, Oct 16, 2012 at 06:48:06PM +0000, Ash Hughes wrote:
This patch updates libavcodec to enable recursively using avcodec_open2/close. Used in the SMV decoder to open an MJPEG decoder.
Ash
--- diff -uNr ffmpeg-vanilla/libavcodec/utils.c ffmpeg/libavcodec/utils.c --- ffmpeg-vanilla/libavcodec/utils.c 2012-10-16 18:09:41.283863114 +0100 +++ ffmpeg/libavcodec/utils.c 2012-10-16 17:58:22.784498604 +0100 @@ -798,6 +798,27 @@ } #endif
+int attribute_align_arg avcodec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
av prefixes are for external API, this is (at least currently) internal API so should have a ff prefix instead [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Awnsering whenever a program halts or runs forever is On a turing machine, in general impossible (turings halting problem). On any real computer, always possible as a real computer has a finite number of states N, and will either halt in less than N cycles or never halt. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Fri, Oct 19, 2012 at 01:12:56AM +0000, Ash Hughes wrote:
Renamed functions to follow convention
applied thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB DNS cache poisoning attacks, popular search engine, Google internet authority dont be evil, please
Updated patch from previous comments to play SMV files. --- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-16 18:09:41.215862776 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-16 17:58:22.748498431 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-16 18:09:41.223862804 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-16 18:20:23.747048918 +0100 @@ -284,6 +284,7 @@ AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-16 18:09:41.211862745 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-16 17:58:22.748498432 +0100 @@ -380,6 +380,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-16 18:40:09.632929405 +0100 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext; #if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-16 19:31:49.572301175 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (avcodec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + avcodec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
Updated to use renamed functions. Ash --- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-16 18:09:41.215862776 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-16 17:58:22.748498431 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-16 18:09:41.223862804 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-16 18:20:23.747048918 +0100 @@ -284,6 +284,7 @@ AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-16 18:09:41.211862745 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-16 17:58:22.748498432 +0100 @@ -380,6 +380,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-16 18:40:09.632929405 +0100 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext; #if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-19 01:48:26.496972741 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
From: ashes-iontach@hotmail.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Oct 2012 18:53:14 +0000 Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files.
Updated patch from previous comments to play SMV files.
--- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-16 18:09:41.215862776 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-16 17:58:22.748498431 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-16 18:09:41.223862804 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-16 18:20:23.747048918 +0100 @@ -284,6 +284,7 @@ AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'),
/* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-16 18:09:41.211862745 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-16 17:58:22.748498432 +0100 @@ -380,6 +380,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-16 18:40:09.632929405 +0100 @@ -25,6 +25,7 @@
#include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext;
#if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } }
st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-16 19:31:49.572301175 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (avcodec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + avcodec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Updated to patch against trunk. Ash. --- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-25 20:34:52.725417510 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-25 20:29:10.195718993 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-25 20:34:52.725417510 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-25 20:29:10.219719122 +0100 @@ -284,6 +284,7 @@ AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-25 20:34:52.721417492 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-25 20:29:10.187718979 +0100 @@ -380,6 +380,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wavdec.c ffmpeg/libavformat/wavdec.c --- ffmpeg-vanilla/libavformat/wavdec.c 2012-10-25 20:34:52.769417736 +0100 +++ ffmpeg/libavformat/wavdec.c 2012-10-25 20:44:15.960210449 +0100 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -48,6 +49,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVDemuxContext; @@ -326,9 +328,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -338,6 +347,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -436,8 +449,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -496,7 +514,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-19 01:48:26.496972741 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
From: ashes-iontach@hotmail.com To: ffmpeg-devel@ffmpeg.org Date: Fri, 19 Oct 2012 01:14:33 +0000 Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files.
Updated to use renamed functions.
Ash
--- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-16 18:09:41.215862776 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-16 17:58:22.748498431 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-16 18:09:41.223862804 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-16 18:20:23.747048918 +0100 @@ -284,6 +284,7 @@ AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'),
/* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-16 18:09:41.211862745 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-16 17:58:22.748498432 +0100 @@ -380,6 +380,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-16 18:40:09.632929405 +0100 @@ -25,6 +25,7 @@
#include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext;
#if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } }
st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-19 01:48:26.496972741 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
From: ashes-iontach@hotmail.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Oct 2012 18:53:14 +0000 Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files.
Updated patch from previous comments to play SMV files.
--- diff -uNr ffmpeg-vanilla/libavcodec/allcodecs.c ffmpeg/libavcodec/allcodecs.c --- ffmpeg-vanilla/libavcodec/allcodecs.c 2012-10-16 18:09:41.215862776 +0100 +++ ffmpeg/libavcodec/allcodecs.c 2012-10-16 17:58:22.748498431 +0100 @@ -215,6 +215,7 @@ REGISTER_ENCDEC (SGI, sgi); REGISTER_DECODER (SMACKER, smacker); REGISTER_DECODER (SMC, smc); + REGISTER_DECODER (SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER (SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla/libavcodec/avcodec.h ffmpeg/libavcodec/avcodec.h --- ffmpeg-vanilla/libavcodec/avcodec.h 2012-10-16 18:09:41.223862804 +0100 +++ ffmpeg/libavcodec/avcodec.h 2012-10-16 18:20:23.747048918 +0100 @@ -284,6 +284,7 @@ AV_CODEC_ID_AVRN = MKBETAG('A','V','R','n'), AV_CODEC_ID_CPIA = MKBETAG('C','P','I','A'), AV_CODEC_ID_XFACE = MKBETAG('X','F','A','C'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'),
/* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla/libavcodec/Makefile ffmpeg/libavcodec/Makefile --- ffmpeg-vanilla/libavcodec/Makefile 2012-10-16 18:09:41.211862745 +0100 +++ ffmpeg/libavcodec/Makefile 2012-10-16 17:58:22.748498432 +0100 @@ -380,6 +380,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla/libavformat/wav.c ffmpeg/libavformat/wav.c --- ffmpeg-vanilla/libavformat/wav.c 2012-10-01 19:34:06.872974243 +0100 +++ ffmpeg/libavformat/wav.c 2012-10-16 18:40:09.632929405 +0100 @@ -25,6 +25,7 @@
#include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -53,6 +54,7 @@ int smv_eof; int audio_eof; int ignore_length; + int smv_cur_pt; } WAVContext;
#if CONFIG_WAV_MUXER @@ -506,9 +508,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -518,6 +527,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): list_type = avio_rl32(pb); @@ -617,8 +630,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -677,7 +695,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } }
st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-16 19:31:49.572301175 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (avcodec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + avcodec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +};
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Thu, Oct 25, 2012 at 07:59:30PM +0000, Ash Hughes wrote:
Updated to patch against trunk.
tried it with: ffmpeg -i smv/bear3.smv out.avi fails with: Unable to parse option value "-1" as pixel format [...]
@@ -326,9 +328,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -338,6 +347,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -436,8 +449,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -496,7 +514,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } }
st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-19 01:48:26.496972741 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + }
iam not sure its a good idea to continue on errors like this have you tested that this works ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is what and why we do it that matters, not just one of them.
Updated to fix parse errors, patch against trunk. Both plays and converts... Ash --- diff -uNr ffmpeg-vanilla//libavcodec/allcodecs.c ffmpeg-smv//libavcodec/allcodecs.c --- ffmpeg-vanilla//libavcodec/allcodecs.c 2013-02-25 16:21:26.273439321 +0000 +++ ffmpeg-smv//libavcodec/allcodecs.c 2013-02-25 16:53:43.203286417 +0000 @@ -243,6 +243,7 @@ REGISTER_DECODER(SGIRLE, sgirle); REGISTER_DECODER(SMACKER, smacker); REGISTER_DECODER(SMC, smc); + REGISTER_DECODER(SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER(SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff -uNr ffmpeg-vanilla//libavcodec/avcodec.h ffmpeg-smv//libavcodec/avcodec.h --- ffmpeg-vanilla//libavcodec/avcodec.h 2013-02-25 16:21:26.286439462 +0000 +++ ffmpeg-smv//libavcodec/avcodec.h 2013-02-25 16:54:44.031951057 +0000 @@ -290,6 +290,7 @@ AV_CODEC_ID_SGIRLE = MKBETAG('S','G','I','R'), AV_CODEC_ID_MVC1 = MKBETAG('M','V','C','1'), AV_CODEC_ID_MVC2 = MKBETAG('M','V','C','2'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff -uNr ffmpeg-vanilla//libavcodec/Makefile ffmpeg-smv//libavcodec/Makefile --- ffmpeg-vanilla//libavcodec/Makefile 2013-02-25 16:21:26.264439224 +0000 +++ ffmpeg-smv//libavcodec/Makefile 2013-02-25 16:55:27.815429521 +0000 @@ -396,6 +396,7 @@ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o snow_dwt.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o snow_dwt.o \ h263.o ituh263enc.o diff -uNr ffmpeg-vanilla//libavformat/wavdec.c ffmpeg-smv//libavformat/wavdec.c --- ffmpeg-vanilla//libavformat/wavdec.c 2013-02-25 16:21:26.531442106 +0000 +++ ffmpeg-smv//libavformat/wavdec.c 2013-02-26 21:12:01.970495165 +0000 @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -51,6 +52,7 @@ int audio_eof; int ignore_length; int spdif; + int smv_cur_pt; } WAVDemuxContext; @@ -329,9 +331,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -341,6 +350,8 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -429,10 +440,11 @@ smv_retry: audio_dts = s->streams[0]->cur_dts; video_dts = s->streams[1]->cur_dts; + if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) { - audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q); - video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q); - wav->smv_last_stream = video_dts >= audio_dts; + audio_dts = abs(av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q)); + video_dts = abs(av_rescale_q(video_dts, s->streams[0]->time_base, AV_TIME_BASE_Q)); + wav->smv_last_stream = video_dts < audio_dts; } wav->smv_last_stream = !wav->smv_last_stream; wav->smv_last_stream |= wav->audio_eof; @@ -450,8 +462,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -510,7 +527,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; diff -uNr ffmpeg-vanilla//libavcodec/smvjpegdec.c ffmpeg-smv//libavcodec/smvjpegdec.c --- ffmpeg-vanilla//libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg-smv//libavcodec/smvjpegdec.c 2013-02-26 01:45:24.519843486 +0000 @@ -0,0 +1,182 @@ +/* + * 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVFrame* mjpeg_data = &s->picture[0]; + AVFrame* output = data; + int i, cur_frame = 0, ret = 0; + + cur_frame = avpkt->pts % s->frames_per_jpeg; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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].base[i] = s->picture[1].data[i]; + s->picture[1].linesize[i] = mjpeg_data->linesize[i]; + } + + *output = s->picture[1]; + + return ret; +} + +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL; + if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff -uNr ffmpeg-vanilla//libavcodec/codec_desc.c ffmpeg-smv//libavcodec/codec_desc.c --- ffmpeg-vanilla//libavcodec/codec_desc.c 2013-02-25 16:21:26.293439537 +0000 +++ ffmpeg-smv//libavcodec/codec_desc.c 2013-02-25 16:50:42.657314358 +0000 @@ -1362,6 +1362,13 @@ .name = "brender_pix", .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"), }, + { + .id = AV_CODEC_ID_SMVJPEG, + .type = AVMEDIA_TYPE_VIDEO, + .name = "smv", + .long_name = NULL_IF_CONFIG_SMALL("Sigmatel Motion Video"), + }, + /* various PCM "codecs" */ { Date: Fri, 26 Oct 2012 04:04:21 +0200 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files. On Thu, Oct 25, 2012 at 07:59:30PM +0000, Ash Hughes wrote:
Updated to patch against trunk.
tried it with: ffmpeg -i smv/bear3.smv out.avi fails with: Unable to parse option value "-1" as pixel format [...]
@@ -326,9 +328,16 @@ avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -338,6 +347,10 @@ avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; + if (wav->smv_frames_per_jpeg > 0) + vst->codec->height /= wav->smv_frames_per_jpeg; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -436,8 +449,13 @@ if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -496,7 +514,10 @@ smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } }
st = s->streams[0]; diff -uNr ffmpeg-vanilla/libavcodec/smvjpegdec.c ffmpeg/libavcodec/smvjpegdec.c --- ffmpeg-vanilla/libavcodec/smvjpegdec.c 1970-01-01 01:00:00.000000000 +0100 +++ ffmpeg/libavcodec/smvjpegdec.c 2012-10-19 01:48:26.496972741 +0100 @@ -0,0 +1,182 @@ +/* + * SMV JPEG decoder + * Copyright (c) 2012 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(&s->picture[1]); + avctx->coded_frame = &s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + }
iam not sure its a good idea to continue on errors like this have you tested that this works ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is what and why we do it that matters, not just one of them. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Tue, Feb 26, 2013 at 09:22:21PM +0000, Ash Hughes wrote:
Updated to fix parse errors, patch against trunk. Both plays and converts... [...] +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL;
+ if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]);
this needs to be updated to the buffer reference API also please make sure to create the patch with git format-patch or git send-email thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Dictatorship naturally arises out of democracy, and the most aggravated form of tyranny and slavery out of the most extreme liberty. -- Plato
Updated, similar to other decoders, Ash ---
From faf3706922771f02c8e1d23d7bcafdcad3488a97 Mon Sep 17 00:00:00 2001 From: Ash Hughes <ashes-iontach@hotmail.com> Date: Wed, 17 Apr 2013 01:27:18 +0100 Subject: [PATCH 1/1] Support playing SMV files
--- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/smvjpegdec.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/wavdec.c | 38 ++++++++-- 6 files changed, 228 insertions(+), 7 deletions(-) create mode 100644 libavcodec/smvjpegdec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 357ac48..327f753 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -382,6 +382,7 @@ OBJS-$(CONFIG_SIPR_DECODER) += sipr.o acelp_pitch_delay.o \ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o snow_dwt.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o snow_dwt.o \ h263.o ituh263enc.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index b06e9a5..34aeb79 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -243,6 +243,7 @@ void avcodec_register_all(void) REGISTER_DECODER(SGIRLE, sgirle); REGISTER_DECODER(SMACKER, smacker); REGISTER_DECODER(SMC, smc); + REGISTER_DECODER(SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER(SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 3f52037..57cd8a1 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -293,6 +293,7 @@ enum AVCodecID { AV_CODEC_ID_MVC2 = MKBETAG('M','V','C','2'), AV_CODEC_ID_SNOW = MKBETAG('S','N','O','W'), AV_CODEC_ID_WEBP = MKBETAG('W','E','B','P'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 2b3eed7..4f629e2 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1371,6 +1371,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_SMVJPEG, + .type = AVMEDIA_TYPE_VIDEO, + .name = "smv", + .long_name = NULL_IF_CONFIG_SMALL("Sigmatel Motion Video"), + }, + /* various PCM "codecs" */ { diff --git a/libavcodec/smvjpegdec.c b/libavcodec/smvjpegdec.c new file mode 100644 index 0000000..431fd04 --- /dev/null +++ b/libavcodec/smvjpegdec.c @@ -0,0 +1,187 @@ +/* + * 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + + s->picture[0] = av_frame_alloc(); + if (!s->picture[0]) + return AVERROR(ENOMEM); + + s->picture[1] = av_frame_alloc(); + if (!s->picture[1]) + return AVERROR(ENOMEM); + + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(s->picture[1]); + avctx->coded_frame = s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + 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; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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]); + + return ret; +} + +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[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c index 14c52f8..963812c 100644 --- a/libavformat/wavdec.c +++ b/libavformat/wavdec.c @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -51,6 +52,8 @@ typedef struct WAVDemuxContext { int audio_eof; int ignore_length; int spdif; + int smv_cur_pt; + int smv_given_first; } WAVDemuxContext; @@ -323,15 +326,23 @@ static int wav_read_header(AVFormatContext *s) goto break_loop; } av_log(s, AV_LOG_DEBUG, "Found SMV data\n"); + wav->smv_given_first = 0; vst = avformat_new_stream(s, NULL); if (!vst) return AVERROR(ENOMEM); avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -341,6 +352,8 @@ static int wav_read_header(AVFormatContext *s) avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -432,10 +445,13 @@ static int wav_read_packet(AVFormatContext *s, smv_retry: audio_dts = s->streams[0]->cur_dts; video_dts = s->streams[1]->cur_dts; + if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) { - audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q); - video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q); - wav->smv_last_stream = video_dts >= audio_dts; + audio_dts = abs(av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q)); + video_dts = abs(av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q)); + /*We always return a video frame first to get the pixel format first*/ + wav->smv_last_stream = wav->smv_given_first ? video_dts < audio_dts : 0; + wav->smv_given_first = 1; } wav->smv_last_stream = !wav->smv_last_stream; wav->smv_last_stream |= wav->audio_eof; @@ -453,8 +469,13 @@ smv_retry: if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -513,7 +534,10 @@ static int wav_read_seek(AVFormatContext *s, smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; -- 1.8.1.4 Date: Wed, 20 Mar 2013 03:02:48 +0100 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files. On Tue, Feb 26, 2013 at 09:22:21PM +0000, Ash Hughes wrote:
Updated to fix parse errors, patch against trunk. Both plays and converts... [...] +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL;
+ if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]);
this needs to be updated to the buffer reference API also please make sure to create the patch with git format-patch or git send-email thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Dictatorship naturally arises out of democracy, and the most aggravated form of tyranny and slavery out of the most extreme liberty. -- Plato _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Does this still need more work? ffmpeg now plays and converts all of the samples correctly with this patch, so far as I can see. A From: ashes-iontach@hotmail.com To: ffmpeg-devel@ffmpeg.org Subject: RE: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files. Date: Wed, 17 Apr 2013 00:33:19 +0000 Updated, similar to other decoders, Ash ---
From faf3706922771f02c8e1d23d7bcafdcad3488a97 Mon Sep 17 00:00:00 2001 From: Ash Hughes <ashes-iontach@hotmail.com> Date: Wed, 17 Apr 2013 01:27:18 +0100 Subject: [PATCH 1/1] Support playing SMV files
--- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/smvjpegdec.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/wavdec.c | 38 ++++++++-- 6 files changed, 228 insertions(+), 7 deletions(-) create mode 100644 libavcodec/smvjpegdec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 357ac48..327f753 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -382,6 +382,7 @@ OBJS-$(CONFIG_SIPR_DECODER) += sipr.o acelp_pitch_delay.o \ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o snow_dwt.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o snow_dwt.o \ h263.o ituh263enc.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index b06e9a5..34aeb79 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -243,6 +243,7 @@ void avcodec_register_all(void) REGISTER_DECODER(SGIRLE, sgirle); REGISTER_DECODER(SMACKER, smacker); REGISTER_DECODER(SMC, smc); + REGISTER_DECODER(SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER(SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 3f52037..57cd8a1 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -293,6 +293,7 @@ enum AVCodecID { AV_CODEC_ID_MVC2 = MKBETAG('M','V','C','2'), AV_CODEC_ID_SNOW = MKBETAG('S','N','O','W'), AV_CODEC_ID_WEBP = MKBETAG('W','E','B','P'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 2b3eed7..4f629e2 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1371,6 +1371,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_SMVJPEG, + .type = AVMEDIA_TYPE_VIDEO, + .name = "smv", + .long_name = NULL_IF_CONFIG_SMALL("Sigmatel Motion Video"), + }, + /* various PCM "codecs" */ { diff --git a/libavcodec/smvjpegdec.c b/libavcodec/smvjpegdec.c new file mode 100644 index 0000000..431fd04 --- /dev/null +++ b/libavcodec/smvjpegdec.c @@ -0,0 +1,187 @@ +/* + * 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h= -((-height)>>desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + + s->picture[0] = av_frame_alloc(); + if (!s->picture[0]) + return AVERROR(ENOMEM); + + s->picture[1] = av_frame_alloc(); + if (!s->picture[1]) + return AVERROR(ENOMEM); + + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(s->picture[1]); + avctx->coded_frame = s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + 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; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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]); + + return ret; +} + +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[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c index 14c52f8..963812c 100644 --- a/libavformat/wavdec.c +++ b/libavformat/wavdec.c @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -51,6 +52,8 @@ typedef struct WAVDemuxContext { int audio_eof; int ignore_length; int spdif; + int smv_cur_pt; + int smv_given_first; } WAVDemuxContext; @@ -323,15 +326,23 @@ static int wav_read_header(AVFormatContext *s) goto break_loop; } av_log(s, AV_LOG_DEBUG, "Found SMV data\n"); + wav->smv_given_first = 0; vst = avformat_new_stream(s, NULL); if (!vst) return AVERROR(ENOMEM); avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -341,6 +352,8 @@ static int wav_read_header(AVFormatContext *s) avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -432,10 +445,13 @@ static int wav_read_packet(AVFormatContext *s, smv_retry: audio_dts = s->streams[0]->cur_dts; video_dts = s->streams[1]->cur_dts; + if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) { - audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q); - video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q); - wav->smv_last_stream = video_dts >= audio_dts; + audio_dts = abs(av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q)); + video_dts = abs(av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q)); + /*We always return a video frame first to get the pixel format first*/ + wav->smv_last_stream = wav->smv_given_first ? video_dts < audio_dts : 0; + wav->smv_given_first = 1; } wav->smv_last_stream = !wav->smv_last_stream; wav->smv_last_stream |= wav->audio_eof; @@ -453,8 +469,13 @@ smv_retry: if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -513,7 +534,10 @@ static int wav_read_seek(AVFormatContext *s, smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; -- 1.8.1.4 Date: Wed, 20 Mar 2013 03:02:48 +0100 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files. On Tue, Feb 26, 2013 at 09:22:21PM +0000, Ash Hughes wrote:
Updated to fix parse errors, patch against trunk. Both plays and converts... [...] +static av_cold int smvjpeg_decode_end(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + MJpegDecodeContext *jpg = &s->jpg; + + jpg->picture_ptr = NULL;
+ if (s->picture[1].data[0]) + avctx->release_buffer(avctx, &s->picture[1]);
this needs to be updated to the buffer reference API also please make sure to create the patch with git format-patch or git send-email thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Dictatorship naturally arises out of democracy, and the most aggravated form of tyranny and slavery out of the most extreme liberty. -- Plato _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Tue, May 21, 2013 at 07:21:08PM +0000, Ash Hughes wrote:
Does this still need more work? ffmpeg now plays and converts all of the samples correctly with this patch, so far as I can see.
seems i forgot about this patch thanks for the ping [...]
+static inline void smv_img_pnt(uint8_t *dst_data[4], uint8_t *src_data[4], + const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) {
+ h= -((-height)>>desc->log2_chroma_h);
FF_CEIL_RSHIFT() [...]
@@ -432,10 +445,13 @@ static int wav_read_packet(AVFormatContext *s, smv_retry: audio_dts = s->streams[0]->cur_dts; video_dts = s->streams[1]->cur_dts; + if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) { - audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q); - video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q); - wav->smv_last_stream = video_dts >= audio_dts; + audio_dts = abs(av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q)); + video_dts = abs(av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q));
what does the abs do here ? also you might want to add yourself to MAINTAINERS and setup a public git clone of ffmpeg where you maintain the code if you agree to maintain it once its in ffmpeg ... [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The misfortune of the wise is better than the prosperity of the fool. -- Epicurus
updated to use FF_CEIL_RSHIFT. The abs() was a throwback to testing, removing it changes the following '<' back to a '>' Ash ---
From cfbd2b64f895cced7c6add679ad3c1dfc3bb3bc7 Mon Sep 17 00:00:00 2001 From: Ash Hughes <ashes-iontach@hotmail.com> Date: Wed, 22 May 2013 02:05:34 +0100 Subject: [PATCH] Support playing SMV files
--- MAINTAINERS | 2 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/smvjpegdec.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/wavdec.c | 34 +++++++-- 7 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 libavcodec/smvjpegdec.c diff --git a/MAINTAINERS b/MAINTAINERS index 9f4301d..d316868 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -217,6 +217,7 @@ Codecs: s3tc* Ivo van Poorten smacker.c Kostya Shishkov smc.c Mike Melanson + smvjpegdec.c Ash Hughes snow.c Michael Niedermayer, Loren Merritt sonic.c Alex Beregszaszi srt* Aurelien Jacobs @@ -463,6 +464,7 @@ GnuPG Fingerprints of maintainers and contributors Anssi Hannula 1A92 FF42 2DD9 8D2E 8AF7 65A9 4278 C520 513D F3CB Anton Khirnov 6D0C 6625 56F8 65D1 E5F5 814B B50A 1241 C067 07AB +Ash Hughes 694D 43D2 D180 C7C7 6421 ABD3 A641 D0B7 623D 6029 Attila Kinali 11F0 F9A6 A1D2 11F6 C745 D10C 6520 BCDD F2DF E765 Baptiste Coudurier 8D77 134D 20CC 9220 201F C5DB 0AC9 325C 5C1A BAAA Ben Littler 3EE3 3723 E560 3214 A8CD 4DEB 2CDB FCE7 768C 8D2C diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 0982885..1a58c69 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -387,6 +387,7 @@ OBJS-$(CONFIG_SIPR_DECODER) += sipr.o acelp_pitch_delay.o \ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o OBJS-$(CONFIG_SMC_DECODER) += smc.o +OBJS-$(CONFIG_SMVJPEG_DECODER) += smvjpegdec.o OBJS-$(CONFIG_SNOW_DECODER) += snowdec.o snow.o snow_dwt.o OBJS-$(CONFIG_SNOW_ENCODER) += snowenc.o snow.o snow_dwt.o \ h263.o ituh263enc.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 1ebd46d..a15162d 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -245,6 +245,7 @@ void avcodec_register_all(void) REGISTER_DECODER(SGIRLE, sgirle); REGISTER_DECODER(SMACKER, smacker); REGISTER_DECODER(SMC, smc); + REGISTER_DECODER(SMVJPEG, smvjpeg); REGISTER_ENCDEC (SNOW, snow); REGISTER_DECODER(SP5X, sp5x); REGISTER_ENCDEC (SUNRAST, sunrast); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index e959811..7ce9df4 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -297,6 +297,7 @@ enum AVCodecID { AV_CODEC_ID_MVC2 = MKBETAG('M','V','C','2'), AV_CODEC_ID_SNOW = MKBETAG('S','N','O','W'), AV_CODEC_ID_WEBP = MKBETAG('W','E','B','P'), + AV_CODEC_ID_SMVJPEG = MKBETAG('S','M','V','J'), /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 72071b9..9309a8d 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1378,6 +1378,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_SMVJPEG, + .type = AVMEDIA_TYPE_VIDEO, + .name = "smv", + .long_name = NULL_IF_CONFIG_SMALL("Sigmatel Motion Video"), + }, + /* various PCM "codecs" */ { diff --git a/libavcodec/smvjpegdec.c b/libavcodec/smvjpegdec.c new file mode 100644 index 0000000..4d20316 --- /dev/null +++ b/libavcodec/smvjpegdec.c @@ -0,0 +1,187 @@ +/* + * 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; +} 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], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) { + h = FF_CEIL_RSHIFT(height, desc->log2_chroma_h); + } + smv_img_pnt_plane(&dst_data[i], src_data[i], + src_linesizes[i], h, nlines); + } +} + +static av_cold int smvjpeg_decode_init(AVCodecContext *avctx) +{ + SMVJpegDecodeContext *s = avctx->priv_data; + AVCodec *codec; + AVDictionary *thread_opt = NULL; + int ret = 0; + + s->frames_per_jpeg = 0; + + s->picture[0] = av_frame_alloc(); + if (!s->picture[0]) + return AVERROR(ENOMEM); + + s->picture[1] = av_frame_alloc(); + if (!s->picture[1]) + return AVERROR(ENOMEM); + + 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"); + ret = -1; + } + + avcodec_get_frame_defaults(s->picture[1]); + avctx->coded_frame = s->picture[1]; + codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + ret = -1; + } + + s->avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); + if (ff_codec_open2_recursive(s->avctx, codec, &thread_opt) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + ret = -1; + } + av_dict_free(&thread_opt); + + return ret; +} + +static int smvjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) +{ + 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; + + /* Are we at the start of a block? */ + if (!cur_frame) + ret = avcodec_decode_video2(s->avctx, mjpeg_data, data_size, avpkt); + else /*use the last lot... */ + *data_size = sizeof(AVPicture); + + avctx->pix_fmt = s->avctx->pix_fmt; + + /* We shouldn't get here if frames_per_jpeg <= 0 because this was rejected + in init */ + avcodec_set_dimensions(avctx, mjpeg_data->width, + mjpeg_data->height / s->frames_per_jpeg); + + 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; + /* ff_init_buffer_info(avctx, &s->picture[1]); */ + 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]); + + return ret; +} + +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[1]); + ff_codec_close_recursive(s->avctx); + av_freep(&s->avctx); + return 0; +} + +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", + .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, + .max_lowres = 3, + .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"), + .priv_class = &smvjpegdec_class, +}; diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c index 874847e..a94c0a9 100644 --- a/libavformat/wavdec.c +++ b/libavformat/wavdec.c @@ -25,6 +25,7 @@ #include "libavutil/avassert.h" #include "libavutil/dict.h" +#include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" @@ -51,6 +52,8 @@ typedef struct WAVDemuxContext { int audio_eof; int ignore_length; int spdif; + int smv_cur_pt; + int smv_given_first; } WAVDemuxContext; #if CONFIG_WAV_DEMUXER @@ -337,15 +340,23 @@ static int wav_read_header(AVFormatContext *s) goto break_loop; } av_log(s, AV_LOG_DEBUG, "Found SMV data\n"); + wav->smv_given_first = 0; vst = avformat_new_stream(s, NULL); if (!vst) return AVERROR(ENOMEM); avio_r8(pb); vst->id = 1; vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; - vst->codec->codec_id = AV_CODEC_ID_MJPEG; + vst->codec->codec_id = AV_CODEC_ID_SMVJPEG; vst->codec->width = avio_rl24(pb); vst->codec->height = avio_rl24(pb); + vst->codec->extradata_size = 4; + vst->codec->extradata = av_malloc(vst->codec->extradata_size + + FF_INPUT_BUFFER_PADDING_SIZE); + if (!vst->codec->extradata) { + av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n"); + return AVERROR(ENOMEM); + } size = avio_rl24(pb); wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3; avio_rl24(pb); @@ -355,6 +366,8 @@ static int wav_read_header(AVFormatContext *s) avio_rl24(pb); avio_rl24(pb); wav->smv_frames_per_jpeg = avio_rl24(pb); + AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg); + wav->smv_cur_pt = 0; goto break_loop; case MKTAG('L', 'I', 'S', 'T'): if (size < 4) { @@ -447,10 +460,13 @@ static int wav_read_packet(AVFormatContext *s, AVPacket *pkt) smv_retry: audio_dts = s->streams[0]->cur_dts; video_dts = s->streams[1]->cur_dts; + if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) { audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q); video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q); - wav->smv_last_stream = video_dts >= audio_dts; + /*We always return a video frame first to get the pixel format first*/ + wav->smv_last_stream = wav->smv_given_first ? video_dts > audio_dts : 0; + wav->smv_given_first = 1; } wav->smv_last_stream = !wav->smv_last_stream; wav->smv_last_stream |= wav->audio_eof; @@ -468,8 +484,13 @@ smv_retry: if (ret < 0) goto smv_out; pkt->pos -= 3; - pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg; - wav->smv_block++; + pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt; + wav->smv_cur_pt++; + if (wav->smv_frames_per_jpeg > 0) + wav->smv_cur_pt %= wav->smv_frames_per_jpeg; + if (!wav->smv_cur_pt) + wav->smv_block++; + pkt->stream_index = 1; smv_out: avio_seek(s->pb, old_pos, SEEK_SET); @@ -528,7 +549,10 @@ static int wav_read_seek(AVFormatContext *s, smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base); else timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base); - wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + if (wav->smv_frames_per_jpeg > 0) { + wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg; + wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg; + } } st = s->streams[0]; -- 1.8.1.4 Date: Tue, 21 May 2013 22:50:48 +0200 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files. On Tue, May 21, 2013 at 07:21:08PM +0000, Ash Hughes wrote:
Does this still need more work? ffmpeg now plays and converts all of the samples correctly with this patch, so far as I can see.
seems i forgot about this patch thanks for the ping [...]
+static inline void smv_img_pnt(uint8_t *dst_data[4], uint8_t *src_data[4], + const int src_linesizes[4], + enum PixelFormat pix_fmt, int width, int height, + int nlines) +{ + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt]; + int i, planes_nb = 0; + + if (desc->flags & PIX_FMT_HWACCEL) + 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) {
+ h= -((-height)>>desc->log2_chroma_h);
FF_CEIL_RSHIFT() [...]
@@ -432,10 +445,13 @@ static int wav_read_packet(AVFormatContext *s, smv_retry: audio_dts = s->streams[0]->cur_dts; video_dts = s->streams[1]->cur_dts; + if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) { - audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q); - video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q); - wav->smv_last_stream = video_dts >= audio_dts; + audio_dts = abs(av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q)); + video_dts = abs(av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q));
what does the abs do here ? also you might want to add yourself to MAINTAINERS and setup a public git clone of ffmpeg where you maintain the code if you agree to maintain it once its in ffmpeg ... [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The misfortune of the wise is better than the prosperity of the fool. -- Epicurus _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Wed, May 22, 2013 at 01:16:51AM +0000, Ash Hughes wrote:
updated to use FF_CEIL_RSHIFT. The abs() was a throwback to testing, removing it changes the following '<' back to a '>'
patch applied maybe you want to add a fate/regression test for it Thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Awnsering whenever a program halts or runs forever is On a turing machine, in general impossible (turings halting problem). On any real computer, always possible as a real computer has a finite number of states N, and will either halt in less than N cycles or never halt.
Hi, Here's a fate test, I hope it's acceptable. Also, if it's suitable, please add apple.smv from http://multimedia.cx/samples/smv/ to fate $(SAMPLES)/smv/ Thanks, Ash ---
From f1ca7c5d2a2cd22e665ca362f0915b219d1aa123 Mon Sep 17 00:00:00 2001 From: Ash Hughes <ashes-iontach@hotmail.com> Date: Fri, 24 May 2013 00:24:38 +0100 Subject: [PATCH] add wav demux/SMV fate test
--- tests/fate/demux.mak | 3 + tests/ref/fate/wav-demux | 182 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 tests/ref/fate/wav-demux diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak index ce54f8f..b68a0a9 100644 --- a/tests/fate/demux.mak +++ b/tests/fate/demux.mak @@ -91,6 +91,9 @@ fate-siff-demux: CMD = framecrc -i $(SAMPLES)/SIFF/INTRO_B.VB -c copy FATE_SAMPLES_DEMUX-$(CONFIG_SMJPEG_DEMUXER) += fate-smjpeg-demux fate-smjpeg-demux: CMD = framecrc -i $(SAMPLES)/smjpeg/scenwin.mjpg -c copy +FATE_SAMPLES_DEMUX-$(CONFIG_WAV_DEMUXER) += fate-wav-demux +fate-wav-demux: CMD = framecrc -i $(SAMPLES)/smv/apple.smv -c copy + FATE_SAMPLES_DEMUX-$(CONFIG_WSAUD_DEMUXER) += fate-westwood-aud fate-westwood-aud: CMD = framecrc -i $(SAMPLES)/westwood-aud/excellent.aud -c copy diff --git a/tests/ref/fate/wav-demux b/tests/ref/fate/wav-demux new file mode 100644 index 0000000..8a836be --- /dev/null +++ b/tests/ref/fate/wav-demux @@ -0,0 +1,182 @@ +#tb 0: 1/23 +#tb 1: 1/22050 +1, 0, 0, 8136, 4096, 0x0a17e101 +1, 8136, 8136, 8136, 4096, 0x0c448199 +1, 16272, 16272, 8136, 4096, 0x8bcd743c +1, 24408, 24408, 8136, 4096, 0x180559cd +1, 32544, 32544, 8136, 4096, 0x5cb98b92 +1, 40680, 40680, 8136, 4096, 0x958b6f1a +1, 48816, 48816, 8136, 4096, 0xf4f46fa3 +1, 56952, 56952, 8136, 4096, 0x488d6dc7 +1, 65088, 65088, 8136, 4096, 0x14dd64f7 +1, 73224, 73224, 8136, 4096, 0xf9b08083 +1, 81360, 81360, 8136, 4096, 0xf0805498 +1, 89496, 89496, 8136, 4096, 0xa76e79c8 +1, 97632, 97632, 8136, 4096, 0x935daa9f +1, 105768, 105768, 8136, 4096, 0x44787940 +1, 113904, 113904, 8136, 4096, 0xe19a4771 +1, 122040, 122040, 8136, 4096, 0x6751c05a +1, 130176, 130176, 8136, 4096, 0x7c1a41e1 +1, 138312, 138312, 8136, 4096, 0xf0e13be2 +1, 146448, 146448, 8136, 4096, 0x9f574f58 +1, 154584, 154584, 8136, 4096, 0x9b884a2e +1, 162720, 162720, 8136, 4096, 0x19cb2b0a +1, 170856, 170856, 8136, 4096, 0x964438a1 +1, 178992, 178992, 8136, 4096, 0xe8265953 +1, 187128, 187128, 8136, 4096, 0xfe0a77c7 +1, 195264, 195264, 8136, 4096, 0x786462cc +1, 203400, 203400, 8136, 4096, 0xf2bfa4d4 +1, 211536, 211536, 8136, 4096, 0xbf898057 +1, 219672, 219672, 8136, 4096, 0x99b16e38 +1, 227808, 227808, 8136, 4096, 0x675761db +1, 235944, 235944, 8136, 4096, 0x10d862d7 +1, 244080, 244080, 8136, 4096, 0x5e8b4711 +1, 252216, 252216, 8136, 4096, 0x88338926 +1, 260352, 260352, 8136, 4096, 0x147d5c03 +1, 268488, 268488, 8136, 4096, 0xe9d577f6 +1, 276624, 276624, 8136, 4096, 0xf2447922 +1, 284760, 284760, 8136, 4096, 0x4389b6b5 +1, 292896, 292896, 8136, 4096, 0x31ce474a +1, 301032, 301032, 8136, 4096, 0xe9932001 +1, 309168, 309168, 8136, 4096, 0x3e78948d +1, 317304, 317304, 8136, 4096, 0xa917ec79 +1, 325440, 325440, 8136, 4096, 0x1e301668 +1, 333576, 333576, 8136, 4096, 0xd8788706 +1, 341712, 341712, 8136, 4096, 0xfb6a1425 +1, 349848, 349848, 8136, 4096, 0x04d84d0f +1, 357984, 357984, 8136, 4096, 0xdb7671a1 +1, 366120, 366120, 8136, 4096, 0xe599677b +1, 374256, 374256, 8136, 4096, 0xf75d5d89 +1, 382392, 382392, 8136, 4096, 0xccda60e9 +1, 390528, 390528, 8136, 4096, 0x19b06d12 +1, 398664, 398664, 8136, 4096, 0x6cb47229 +1, 406800, 406800, 8136, 4096, 0x819349ae +1, 414936, 414936, 8136, 4096, 0x4a8a2582 +1, 423072, 423072, 8136, 4096, 0xe9690571 +1, 431208, 431208, 8136, 4096, 0xd98de6c9 +1, 439344, 439344, 8136, 4096, 0x6231db28 +1, 447480, 447480, 8136, 4096, 0x4e4d2c01 +1, 455616, 455616, 8136, 4096, 0xb15828d2 +1, 463752, 463752, 8136, 4096, 0x1b72a45f +1, 471888, 471888, 8136, 4096, 0x9a664f5f +1, 480024, 480024, 8136, 4096, 0x361136b1 +1, 488160, 488160, 8136, 4096, 0xe6711ac1 +1, 496296, 496296, 8136, 4096, 0xfe394336 +1, 504432, 504432, 8136, 4096, 0xe0061070 +1, 512568, 512568, 8136, 4096, 0xbd6453ac +1, 520704, 520704, 8136, 4096, 0x67aba86d +1, 528840, 528840, 8136, 4096, 0x4d86b32f +1, 536976, 536976, 8136, 4096, 0xe26053aa +1, 545112, 545112, 8136, 4096, 0x139466de +1, 553248, 553248, 8136, 4096, 0xfa0c96b7 +1, 561384, 561384, 8136, 4096, 0x46015f1a +1, 569520, 569520, 8136, 4096, 0xafb87159 +1, 577656, 577656, 8136, 4096, 0xaa9563c1 +1, 585792, 585792, 8136, 4096, 0xa84367c6 +1, 593928, 593928, 8136, 4096, 0xbb4367d6 +1, 602064, 602064, 8136, 4096, 0x4d435587 +1, 610200, 610200, 8136, 4096, 0x66425b2a +1, 618336, 618336, 8136, 4096, 0x72bc60c2 +1, 626472, 626472, 8136, 4096, 0xb8f2693a +1, 634608, 634608, 8136, 4096, 0xf6d07010 +1, 642744, 642744, 8136, 4096, 0xaf675ee0 +1, 650880, 650880, 8136, 4096, 0xba5f5919 +1, 659016, 659016, 8136, 4096, 0x7ea8593a +1, 667152, 667152, 8136, 4096, 0xe2144547 +1, 675288, 675288, 8136, 4096, 0x39a5645e +1, 683424, 683424, 8136, 4096, 0xa4e65eac +1, 691560, 691560, 8136, 4096, 0xc67760a8 +1, 699696, 699696, 8136, 4096, 0xe9a8fa02 +1, 707832, 707832, 8136, 4096, 0xff30e5da +1, 715968, 715968, 8136, 4096, 0x18316fb1 +1, 724104, 724104, 8136, 4096, 0x85f93aeb +1, 732240, 732240, 8136, 4096, 0xe37e9072 +1, 740376, 740376, 8136, 4096, 0xcb0415c1 +1, 748512, 748512, 8136, 4096, 0x14b8292b +1, 756648, 756648, 8136, 4096, 0xbd102aad +1, 764784, 764784, 8136, 4096, 0x0905b369 +1, 772920, 772920, 8136, 4096, 0x984e8240 +1, 781056, 781056, 8136, 4096, 0xc8769d1f +1, 789192, 789192, 8136, 4096, 0x524b9927 +1, 797328, 797328, 8136, 4096, 0x4280549e +1, 805464, 805464, 8136, 4096, 0x907b7564 +1, 813600, 813600, 8136, 4096, 0x5a4e57f0 +1, 821736, 821736, 8136, 4096, 0xdc0d6eec +1, 829872, 829872, 8136, 4096, 0x408d5ecd +1, 838008, 838008, 8136, 4096, 0x17a66492 +1, 846144, 846144, 8136, 4096, 0x377f6434 +1, 854280, 854280, 8136, 4096, 0x09935a3c +1, 862416, 862416, 8136, 4096, 0x07f56a5c +1, 870552, 870552, 8136, 4096, 0xffeb75ed +1, 878688, 878688, 8136, 4096, 0xb09566b7 +1, 886824, 886824, 8136, 4096, 0xadf34d16 +1, 894960, 894960, 8136, 4096, 0xb4624d32 +1, 903096, 903096, 8136, 4096, 0x667a5f17 +1, 911232, 911232, 8136, 4096, 0x7cb55251 +1, 919368, 919368, 8136, 4096, 0xd00756e8 +1, 927504, 927504, 8136, 4096, 0xd5846478 +1, 935640, 935640, 8136, 4096, 0x92c08318 +1, 943776, 943776, 8136, 4096, 0x4e2d5342 +1, 951912, 951912, 8136, 4096, 0xc0a739ee +1, 960048, 960048, 8136, 4096, 0x51176dc7 +1, 968184, 968184, 8136, 4096, 0xa2d531b4 +1, 976320, 976320, 8136, 4096, 0x68a4793f +1, 984456, 984456, 8136, 4096, 0x81ec7eed +1, 992592, 992592, 8136, 4096, 0xa7a74800 +1, 1000728, 1000728, 8136, 4096, 0x2388623a +1, 1008864, 1008864, 8136, 4096, 0x4aba3a0c +1, 1017000, 1017000, 8136, 4096, 0xa43f491e +1, 1025136, 1025136, 8136, 4096, 0xfb506102 +1, 1033272, 1033272, 8136, 4096, 0x425b5873 +1, 1041408, 1041408, 8136, 4096, 0x10487cce +1, 1049544, 1049544, 8136, 4096, 0x45946c56 +1, 1057680, 1057680, 8136, 4096, 0x861e587e +1, 1065816, 1065816, 8136, 4096, 0xb3fa60bf +1, 1073952, 1073952, 8136, 4096, 0x31ca5db4 +1, 1082088, 1082088, 8136, 4096, 0xc8db6230 +1, 1090224, 1090224, 8136, 4096, 0x9eab5927 +1, 1098360, 1098360, 8136, 4096, 0x7aec51c0 +1, 1106496, 1106496, 8136, 4096, 0xb04068d2 +1, 1114632, 1114632, 8136, 4096, 0x75905d55 +1, 1122768, 1122768, 8136, 4096, 0x63063b14 +1, 1130904, 1130904, 8136, 4096, 0xf2493413 +1, 1139040, 1139040, 8136, 4096, 0x00000000 +1, 1147176, 1147176, 8136, 4096, 0x00000000 +1, 1155312, 1155312, 8136, 4096, 0x34994ce0 +1, 1163448, 1163448, 8136, 4096, 0xbcc04c2b +1, 1171584, 1171584, 8136, 4096, 0x00000000 +1, 1179720, 1179720, 8136, 4096, 0x00000000 +1, 1187856, 1187856, 8136, 4096, 0x00000000 +1, 1195992, 1195992, 8136, 4096, 0x00000000 +1, 1204128, 1204128, 8136, 4096, 0x00000000 +1, 1212264, 1212264, 8136, 4096, 0x00000000 +1, 1220400, 1220400, 8136, 4096, 0x4fa4bff9 +1, 1228536, 1228536, 8136, 4096, 0x00000000 +1, 1236672, 1236672, 8136, 4096, 0x55d1c393 +1, 1244808, 1244808, 8136, 4096, 0x3902bc42 +1, 1252944, 1252944, 8136, 4096, 0x25111404 +1, 1261080, 1261080, 8136, 4096, 0xae7f2774 +1, 1269216, 1269216, 8136, 4096, 0xc28b5b39 +1, 1277352, 1277352, 8136, 4096, 0x619dd7fe +1, 1285488, 1285488, 8136, 4096, 0x00000000 +1, 1293624, 1293624, 8136, 4096, 0x00000000 +1, 1301760, 1301760, 8136, 4096, 0x00000000 +1, 1309896, 1309896, 8136, 4096, 0x00000000 +1, 1318032, 1318032, 8136, 4096, 0x00000000 +1, 1326168, 1326168, 8136, 4096, 0x00000000 +1, 1334304, 1334304, 8136, 4096, 0x00000000 +1, 1342440, 1342440, 8136, 4096, 0xe8337eba +1, 1350576, 1350576, 8136, 4096, 0x5ad76305 +1, 1358712, 1358712, 8136, 4096, 0xbbdf5eae +1, 1366848, 1366848, 8136, 4096, 0xadc67748 +1, 1374984, 1374984, 8136, 4096, 0x551d461e +1, 1383120, 1383120, 8136, 4096, 0xa8d162c8 +1, 1391256, 1391256, 8136, 4096, 0x1ba2533f +1, 1399392, 1399392, 8136, 4096, 0x709e4c9b +1, 1407528, 1407528, 8136, 4096, 0x6d004a4c +1, 1415664, 1415664, 8136, 4096, 0x3012eb1d +1, 1423800, 1423800, 8136, 4096, 0x4e6dc3bc +1, 1431936, 1431936, 8136, 4096, 0x64b36b64 +1, 1440072, 1440072, 8136, 4096, 0x2d1b7406 +1, 1448208, 1448208, 8136, 4096, 0x6ed8aad2 +1, 1456344, 1456344, 3051, 1536, 0x00000000 -- 1.8.1.4 Date: Wed, 22 May 2013 15:37:34 +0200 From: michaelni@gmx.at To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files. On Wed, May 22, 2013 at 01:16:51AM +0000, Ash Hughes wrote:
updated to use FF_CEIL_RSHIFT. The abs() was a throwback to testing, removing it changes the following '<' back to a '>'
patch applied maybe you want to add a fate/regression test for it Thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Awnsering whenever a program halts or runs forever is On a turing machine, in general impossible (turings halting problem). On any real computer, always possible as a real computer has a finite number of states N, and will either halt in less than N cycles or never halt. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Thu, May 23, 2013 at 11:39:39PM +0000, Ash Hughes wrote:
Hi,
Here's a fate test, I hope it's acceptable. Also, if it's suitable, please add apple.smv from http://multimedia.cx/samples/smv/ to fate $(SAMPLES)/smv/
would a smaller file suffice too ? apple.smv is 6mb [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Awnsering whenever a program halts or runs forever is On a turing machine, in general impossible (turings halting problem). On any real computer, always possible as a real computer has a finite number of states N, and will either halt in less than N cycles or never halt.
On 23/05/13 8:39 PM, Ash Hughes wrote:
Hi,
Here's a fate test, I hope it's acceptable. Also, if it's suitable, please add apple.smv from http://multimedia.cx/samples/smv/ to fate $(SAMPLES)/smv/
Thanks,
Ash
[...]
+FATE_SAMPLES_DEMUX-$(CONFIG_WAV_DEMUXER) += fate-wav-demux +fate-wav-demux: CMD = framecrc -i $(SAMPLES)/smv/apple.smv -c copy +
You want to test a decoder, not a demuxer. You should instead add a test to video.mak, something like FATE_VIDEO-$(call DEMDEC, WAV, SMVJPEG) += fate-smvjpeg fate-smvjpeg: CMD = framecrc -i $(SAMPLES)/smv/apple.smv -an Plus any extra commands it might need (-pix_fmt or such). Also, like Michael asked, try to get a smaller file since 6mb is a bit much. You can use dd to cut the file down if you don't have any other sample. Regards.
Attached is a patch with the test in video.mak, using a conversion of clock.avi to smv, available at https://www.dropbox.com/s/2zsv9r02ou6189o/clock.smv Ash ---
From 5f183f11f9703b6d351340c218d20d9487424bff Mon Sep 17 00:00:00 2001 From: Ash Hughes <ashes-iontach@hotmail.com> Date: Fri, 24 May 2013 14:50:30 +0100 Subject: [PATCH] add wav demux/SMV fate test
--- tests/fate/video.mak | 3 +++ tests/ref/fate/smvjpeg | 13 +++++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-) create mode 100644 tests/ref/fate/smvjpeg diff --git a/tests/fate/video.mak b/tests/fate/video.mak index ca0789b..39ff207 100644 --- a/tests/fate/video.mak +++ b/tests/fate/video.mak @@ -287,6 +287,9 @@ fate-yop: CMD = framecrc -i $(SAMPLES)/yop/test1.yop -pix_fmt rgb24 -an FATE_VIDEO-$(call DEMDEC, AVI, XAN_WC4) += fate-xxan-wc4 fate-xxan-wc4: CMD = framecrc -i $(SAMPLES)/wc4-xan/wc4trailer-partial.avi -an +FATE_VIDEO-$(call DEMDEC, WAV, SMVJPEG) += fate-smvjpeg +fate-smvjpeg: CMD = framecrc -i $(SAMPLES)/smv/clock.smv -an + FATE_VIDEO += $(FATE_VIDEO-yes) FATE_SAMPLES_FFMPEG += $(FATE_VIDEO) diff --git a/tests/ref/fate/smvjpeg b/tests/ref/fate/smvjpeg new file mode 100644 index 0000000..e941233 --- /dev/null +++ b/tests/ref/fate/smvjpeg @@ -0,0 +1,13 @@ +#tb 0: 1/1 +0, 0, 0, 1, 30720, 0x3a821807 +0, 1, 1, 1, 30720, 0x80248e58 +0, 2, 2, 1, 30720, 0xd4d98e45 +0, 3, 3, 1, 30720, 0x0bcba7e7 +0, 4, 4, 1, 30720, 0xb832a22d +0, 5, 5, 1, 30720, 0x1f3eb488 +0, 6, 6, 1, 30720, 0x6429ce43 +0, 7, 7, 1, 30720, 0xf44da231 +0, 8, 8, 1, 30720, 0x5c02aeff +0, 9, 9, 1, 30720, 0x4c7b1c9a +0, 10, 10, 1, 30720, 0x6f7a8313 +0, 11, 11, 1, 30720, 0xaa32fd72 -- 1.7.1
Date: Thu, 23 May 2013 21:59:05 -0300 From: jamrial@gmail.com To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 2/2] Support playing SMV files.
On 23/05/13 8:39 PM, Ash Hughes wrote:
Hi,
Here's a fate test, I hope it's acceptable. Also, if it's suitable, please add apple.smv from http://multimedia.cx/samples/smv/ to fate $(SAMPLES)/smv/
Thanks,
Ash
[...]
+FATE_SAMPLES_DEMUX-$(CONFIG_WAV_DEMUXER) += fate-wav-demux +fate-wav-demux: CMD = framecrc -i $(SAMPLES)/smv/apple.smv -c copy +
You want to test a decoder, not a demuxer. You should instead add a test to video.mak, something like
FATE_VIDEO-$(call DEMDEC, WAV, SMVJPEG) += fate-smvjpeg fate-smvjpeg: CMD = framecrc -i $(SAMPLES)/smv/apple.smv -an
Plus any extra commands it might need (-pix_fmt or such). Also, like Michael asked, try to get a smaller file since 6mb is a bit much. You can use dd to cut the file down if you don't have any other sample.
Regards. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On Fri, May 24, 2013 at 02:06:00PM +0000, Ash Hughes wrote:
Attached is a patch with the test in video.mak, using a conversion of clock.avi to smv, available at https://www.dropbox.com/s/2zsv9r02ou6189o/clock.smv
file uploaded patch will be applied in 24h unless i forget thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB DNS cache poisoning attacks, popular search engine, Google internet authority dont be evil, please
On Fri, May 24, 2013 at 10:26:06PM +0200, Michael Niedermayer wrote:
On Fri, May 24, 2013 at 02:06:00PM +0000, Ash Hughes wrote:
Attached is a patch with the test in video.mak, using a conversion of clock.avi to smv, available at https://www.dropbox.com/s/2zsv9r02ou6189o/clock.smv
file uploaded
patch will be applied in 24h unless i forget
patch applied [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The greatest way to live with honor in this world is to be what we pretend to be. -- Socrates
On date Tuesday 2012-10-02 11:50:08 +0100, Ash Hughes encoded: [...]
/** + * Copy image plane from src to dst. + * That is, copy "height" number of lines of "bytewidth" bytes each. + * The first byte of each successive line is separated by *_linesize + * bytes. Skips copying the first n lines. + * + * @param dst_linesize linesize for the image plane in dst + * @param src_linesize linesize for the image plane in src + * @param nlines number of lines to skip before copying + */ +void av_image_copy_plane_skip(uint8_t *dst, int dst_linesize, + const uint8_t *src, int src_linesize, + int bytewidth, int height, int nlines);
This function seems too much specialized for a generic utils, what about: void av_image_copy_plane_slice(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int y, int h); That would be consistent with the scaling API, and possibly more useful in different contexts. [...] -- FFmpeg = Fundamental and Frightening Mean Pure Exploitable Gnome
participants (5)
-
Ash Hughes -
James Almer -
Michael Niedermayer -
Reimar Döffinger -
Stefano Sabatini