Message ID | 20170228183908.26339-1-damien.riegel@savoirfairelinux.com |
---|---|
State | Superseded |
Headers | show |
On Tue, Feb 28, 2017 at 01:39:07PM -0500, Damien Riegel wrote: > Add a codec capable of decoding some formats of the RFC4175. For now > it's only capable of handling YCbCr-4:2:2 with 8-bit or 10-bit depth. > > For 8-bit it's a simple pass-through, for 10-bit it depacks the stream > in the AV_PIX_FMT_YUV422P10 pixel format. > > Signed-off-by: Damien Riegel <damien.riegel@savoirfairelinux.com> > --- > Changes in v3: > - Codec has been renamed bitpacked (instead of vrawdepay) > - A decoding function is now chosen at codec init based on the pixel > format > - Codec marked as experimental > > libavcodec/Makefile | 1 + > libavcodec/allcodecs.c | 1 + > libavcodec/avcodec.h | 1 + > libavcodec/bitpacked.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ > libavcodec/codec_desc.c | 7 +++ > 5 files changed, 158 insertions(+) > create mode 100644 libavcodec/bitpacked.c > > diff --git a/libavcodec/Makefile b/libavcodec/Makefile > index 1bea44ad91..dc5e684f10 100644 > --- a/libavcodec/Makefile > +++ b/libavcodec/Makefile > @@ -214,6 +214,7 @@ OBJS-$(CONFIG_BINK_DECODER) += bink.o binkdsp.o > OBJS-$(CONFIG_BINKAUDIO_DCT_DECODER) += binkaudio.o > OBJS-$(CONFIG_BINKAUDIO_RDFT_DECODER) += binkaudio.o > OBJS-$(CONFIG_BINTEXT_DECODER) += bintext.o cga_data.o > +OBJS-$(CONFIG_BITPACKED_DECODER) += bitpacked.o > OBJS-$(CONFIG_BMP_DECODER) += bmp.o msrledec.o > OBJS-$(CONFIG_BMP_ENCODER) += bmpenc.o > OBJS-$(CONFIG_BMV_AUDIO_DECODER) += bmvaudio.o > diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c > index eee322b2b8..86a8fd56c5 100644 > --- a/libavcodec/allcodecs.c > +++ b/libavcodec/allcodecs.c > @@ -366,6 +366,7 @@ void avcodec_register_all(void) > REGISTER_DECODER(VP8, vp8); > REGISTER_DECODER(VP9, vp9); > REGISTER_DECODER(VQA, vqa); > + REGISTER_DECODER(BITPACKED, bitpacked); > REGISTER_DECODER(WEBP, webp); > REGISTER_ENCODER(WRAPPED_AVFRAME, wrapped_avframe); > REGISTER_ENCDEC (WMV1, wmv1); > diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h > index 925a8c7277..a0d07c64bc 100644 > --- a/libavcodec/avcodec.h > +++ b/libavcodec/avcodec.h > @@ -416,6 +416,7 @@ enum AVCodecID { > AV_CODEC_ID_SPEEDHQ, > AV_CODEC_ID_FMVC, > AV_CODEC_ID_SCPR, > + AV_CODEC_ID_BITPACKED, > > /* various PCM "codecs" */ > AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs > diff --git a/libavcodec/bitpacked.c b/libavcodec/bitpacked.c > new file mode 100644 > index 0000000000..1d935b71a1 > --- /dev/null > +++ b/libavcodec/bitpacked.c > @@ -0,0 +1,148 @@ > +/* > + * Unpack bit-packed streams to formats supported by FFmpeg > + * Copyright (c) 2017 Savoir-faire Linux, Inc > + * > + * 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 > + */ > + > +/* Development sponsored by CBC/Radio-Canada */ > + > +/** > + * @file > + * Bitpacked > + */ > + > +#include <libavutil/imgutils.h> wrong directory > +#include "avcodec.h" > +#include "internal.h" > +#include "get_bits.h" > + > +struct BitpackedContext { > + int (*decode)(AVCodecContext *avctx, AVFrame *frame, > + AVPacket *pkt); > +}; > + > +/* For this format, it's a simple passthrough */ > +static int bitpacked_decode_uyvy422(AVCodecContext *avctx, AVFrame *frame, > + AVPacket *avpkt) > +{ > + int ret; > + > + /* there is no need to copy as the data already match > + * a known pixel format */ > + frame->buf[0] = av_buffer_ref(avpkt->buf); > + ret = av_image_fill_arrays(frame->data, frame->linesize, avpkt->data, > + avctx->pix_fmt, avctx->width, avctx->height, 1); linesize and the initial data pointers have some alignment requirements they may need to be checked before pass through > + if (ret < 0) { > + av_buffer_unref(&frame->buf[0]); > + return ret; > + } > + > + return 0; > +} > + > +static int bitpacked_decode_yuv422p10(AVCodecContext *avctx, AVFrame *frame, > + AVPacket *avpkt) > +{ > + GetBitContext bc; > + uint16_t *y, *u, *v; > + int ret, i; > + > + ret = ff_get_buffer(avctx, frame, 0); > + if (ret < 0) > + return ret; > + > + y = (uint16_t*)frame->data[0]; > + u = (uint16_t*)frame->data[1]; > + v = (uint16_t*)frame->data[2]; > + > + if (avctx->width * avctx->height * 20 > avpkt->size * 8) > + return AVERROR_INVALIDDATA; this should use 64bit > + > + ret = init_get_bits(&bc, avpkt->data, avctx->width * avctx->height * 20); > + if (ret) > + return ret; > + > + for (i = 0; i < (avctx->width * avctx->height) / 2; i++) { > + *u++ = get_bits(&bc, 10); > + *y++ = get_bits(&bc, 10); > + *v++ = get_bits(&bc, 10); > + *y++ = get_bits(&bc, 10); custom code to read this would likely be faster tha the generic bit reader also this looks wrong, there is no access to linesize also is there a check for width to be even ? > + } > + > + return 0; > +} > + > +static av_cold int bitpacked_init_decoder(AVCodecContext *avctx) > +{ > + struct BitpackedContext *bc = avctx->priv_data; > + > + if (!avctx->codec_tag || !avctx->width || !avctx->height) > + return -1; > + > + if (avctx->codec_tag == MKTAG('U', 'Y', 'V', 'Y')) { > + if (avctx->bits_per_coded_sample == 16 && > + avctx->pix_fmt == AV_PIX_FMT_UYVY422) > + bc->decode = bitpacked_decode_uyvy422; > + else if (avctx->bits_per_coded_sample == 20 && > + avctx->pix_fmt == AV_PIX_FMT_YUV422P10) > + bc->decode = bitpacked_decode_yuv422p10; > + else > + return -1; > + } else { > + return -1; > + } please use AVERROR... codes > + > + return 0; > +} > + > +static av_cold int bitpacked_close_decoder(AVCodecContext *avctx) > +{ > + return 0; > +} uneeded if it does nothing [...]
On Tue, Feb 28, 2017 at 11:10:23PM +0100, Michael Niedermayer wrote: > On Tue, Feb 28, 2017 at 01:39:07PM -0500, Damien Riegel wrote: > > Add a codec capable of decoding some formats of the RFC4175. For now > > it's only capable of handling YCbCr-4:2:2 with 8-bit or 10-bit depth. > > > > For 8-bit it's a simple pass-through, for 10-bit it depacks the stream > > in the AV_PIX_FMT_YUV422P10 pixel format. > > > > Signed-off-by: Damien Riegel <damien.riegel@savoirfairelinux.com> > > --- > > Changes in v3: > > - Codec has been renamed bitpacked (instead of vrawdepay) > > - A decoding function is now chosen at codec init based on the pixel > > format > > - Codec marked as experimental > > > > libavcodec/Makefile | 1 + > > libavcodec/allcodecs.c | 1 + > > libavcodec/avcodec.h | 1 + > > libavcodec/bitpacked.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ > > libavcodec/codec_desc.c | 7 +++ > > 5 files changed, 158 insertions(+) > > create mode 100644 libavcodec/bitpacked.c > > > > diff --git a/libavcodec/Makefile b/libavcodec/Makefile > > index 1bea44ad91..dc5e684f10 100644 > > --- a/libavcodec/Makefile > > +++ b/libavcodec/Makefile > > @@ -214,6 +214,7 @@ OBJS-$(CONFIG_BINK_DECODER) += bink.o binkdsp.o > > OBJS-$(CONFIG_BINKAUDIO_DCT_DECODER) += binkaudio.o > > OBJS-$(CONFIG_BINKAUDIO_RDFT_DECODER) += binkaudio.o > > OBJS-$(CONFIG_BINTEXT_DECODER) += bintext.o cga_data.o > > +OBJS-$(CONFIG_BITPACKED_DECODER) += bitpacked.o > > OBJS-$(CONFIG_BMP_DECODER) += bmp.o msrledec.o > > OBJS-$(CONFIG_BMP_ENCODER) += bmpenc.o > > OBJS-$(CONFIG_BMV_AUDIO_DECODER) += bmvaudio.o > > diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c > > index eee322b2b8..86a8fd56c5 100644 > > --- a/libavcodec/allcodecs.c > > +++ b/libavcodec/allcodecs.c > > @@ -366,6 +366,7 @@ void avcodec_register_all(void) > > REGISTER_DECODER(VP8, vp8); > > REGISTER_DECODER(VP9, vp9); > > REGISTER_DECODER(VQA, vqa); > > + REGISTER_DECODER(BITPACKED, bitpacked); > > REGISTER_DECODER(WEBP, webp); > > REGISTER_ENCODER(WRAPPED_AVFRAME, wrapped_avframe); > > REGISTER_ENCDEC (WMV1, wmv1); > > diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h > > index 925a8c7277..a0d07c64bc 100644 > > --- a/libavcodec/avcodec.h > > +++ b/libavcodec/avcodec.h > > @@ -416,6 +416,7 @@ enum AVCodecID { > > AV_CODEC_ID_SPEEDHQ, > > AV_CODEC_ID_FMVC, > > AV_CODEC_ID_SCPR, > > + AV_CODEC_ID_BITPACKED, > > > > /* various PCM "codecs" */ > > AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs > > diff --git a/libavcodec/bitpacked.c b/libavcodec/bitpacked.c > > new file mode 100644 > > index 0000000000..1d935b71a1 > > --- /dev/null > > +++ b/libavcodec/bitpacked.c > > @@ -0,0 +1,148 @@ > > +/* > > + * Unpack bit-packed streams to formats supported by FFmpeg > > + * Copyright (c) 2017 Savoir-faire Linux, Inc > > + * > > + * 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 > > + */ > > + > > +/* Development sponsored by CBC/Radio-Canada */ > > + > > +/** > > + * @file > > + * Bitpacked > > + */ > > + > > > +#include <libavutil/imgutils.h> > > wrong directory > > > > +#include "avcodec.h" > > +#include "internal.h" > > +#include "get_bits.h" > > + > > +struct BitpackedContext { > > + int (*decode)(AVCodecContext *avctx, AVFrame *frame, > > + AVPacket *pkt); > > +}; > > + > > > +/* For this format, it's a simple passthrough */ > > +static int bitpacked_decode_uyvy422(AVCodecContext *avctx, AVFrame *frame, > > + AVPacket *avpkt) > > +{ > > + int ret; > > + > > + /* there is no need to copy as the data already match > > + * a known pixel format */ > > + frame->buf[0] = av_buffer_ref(avpkt->buf); > > + ret = av_image_fill_arrays(frame->data, frame->linesize, avpkt->data, > > + avctx->pix_fmt, avctx->width, avctx->height, 1); > > linesize and the initial data pointers have some alignment requirements > > they may need to be checked before pass through I addressed others comments but I'm not sure I understand this one. What are these alignment requirements and what needs to be checked? > > [...] > > + > > + ret = init_get_bits(&bc, avpkt->data, avctx->width * avctx->height * 20); > > + if (ret) > > + return ret; > > + > > > + for (i = 0; i < (avctx->width * avctx->height) / 2; i++) { > > + *u++ = get_bits(&bc, 10); > > + *y++ = get_bits(&bc, 10); > > + *v++ = get_bits(&bc, 10); > > + *y++ = get_bits(&bc, 10); > > custom code to read this would likely be faster tha the generic > bit reader Probably, but I'd rather have a very straightforward first implementation. Optimization could come a bit later (like hardware accelerated unpacking) if that's okay with you.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 1bea44ad91..dc5e684f10 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -214,6 +214,7 @@ OBJS-$(CONFIG_BINK_DECODER) += bink.o binkdsp.o OBJS-$(CONFIG_BINKAUDIO_DCT_DECODER) += binkaudio.o OBJS-$(CONFIG_BINKAUDIO_RDFT_DECODER) += binkaudio.o OBJS-$(CONFIG_BINTEXT_DECODER) += bintext.o cga_data.o +OBJS-$(CONFIG_BITPACKED_DECODER) += bitpacked.o OBJS-$(CONFIG_BMP_DECODER) += bmp.o msrledec.o OBJS-$(CONFIG_BMP_ENCODER) += bmpenc.o OBJS-$(CONFIG_BMV_AUDIO_DECODER) += bmvaudio.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index eee322b2b8..86a8fd56c5 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -366,6 +366,7 @@ void avcodec_register_all(void) REGISTER_DECODER(VP8, vp8); REGISTER_DECODER(VP9, vp9); REGISTER_DECODER(VQA, vqa); + REGISTER_DECODER(BITPACKED, bitpacked); REGISTER_DECODER(WEBP, webp); REGISTER_ENCODER(WRAPPED_AVFRAME, wrapped_avframe); REGISTER_ENCDEC (WMV1, wmv1); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 925a8c7277..a0d07c64bc 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -416,6 +416,7 @@ enum AVCodecID { AV_CODEC_ID_SPEEDHQ, AV_CODEC_ID_FMVC, AV_CODEC_ID_SCPR, + AV_CODEC_ID_BITPACKED, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/bitpacked.c b/libavcodec/bitpacked.c new file mode 100644 index 0000000000..1d935b71a1 --- /dev/null +++ b/libavcodec/bitpacked.c @@ -0,0 +1,148 @@ +/* + * Unpack bit-packed streams to formats supported by FFmpeg + * Copyright (c) 2017 Savoir-faire Linux, Inc + * + * 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 + */ + +/* Development sponsored by CBC/Radio-Canada */ + +/** + * @file + * Bitpacked + */ + +#include <libavutil/imgutils.h> +#include "avcodec.h" +#include "internal.h" +#include "get_bits.h" + +struct BitpackedContext { + int (*decode)(AVCodecContext *avctx, AVFrame *frame, + AVPacket *pkt); +}; + +/* For this format, it's a simple passthrough */ +static int bitpacked_decode_uyvy422(AVCodecContext *avctx, AVFrame *frame, + AVPacket *avpkt) +{ + int ret; + + /* there is no need to copy as the data already match + * a known pixel format */ + frame->buf[0] = av_buffer_ref(avpkt->buf); + ret = av_image_fill_arrays(frame->data, frame->linesize, avpkt->data, + avctx->pix_fmt, avctx->width, avctx->height, 1); + if (ret < 0) { + av_buffer_unref(&frame->buf[0]); + return ret; + } + + return 0; +} + +static int bitpacked_decode_yuv422p10(AVCodecContext *avctx, AVFrame *frame, + AVPacket *avpkt) +{ + GetBitContext bc; + uint16_t *y, *u, *v; + int ret, i; + + ret = ff_get_buffer(avctx, frame, 0); + if (ret < 0) + return ret; + + y = (uint16_t*)frame->data[0]; + u = (uint16_t*)frame->data[1]; + v = (uint16_t*)frame->data[2]; + + if (avctx->width * avctx->height * 20 > avpkt->size * 8) + return AVERROR_INVALIDDATA; + + ret = init_get_bits(&bc, avpkt->data, avctx->width * avctx->height * 20); + if (ret) + return ret; + + for (i = 0; i < (avctx->width * avctx->height) / 2; i++) { + *u++ = get_bits(&bc, 10); + *y++ = get_bits(&bc, 10); + *v++ = get_bits(&bc, 10); + *y++ = get_bits(&bc, 10); + } + + return 0; +} + +static av_cold int bitpacked_init_decoder(AVCodecContext *avctx) +{ + struct BitpackedContext *bc = avctx->priv_data; + + if (!avctx->codec_tag || !avctx->width || !avctx->height) + return -1; + + if (avctx->codec_tag == MKTAG('U', 'Y', 'V', 'Y')) { + if (avctx->bits_per_coded_sample == 16 && + avctx->pix_fmt == AV_PIX_FMT_UYVY422) + bc->decode = bitpacked_decode_uyvy422; + else if (avctx->bits_per_coded_sample == 20 && + avctx->pix_fmt == AV_PIX_FMT_YUV422P10) + bc->decode = bitpacked_decode_yuv422p10; + else + return -1; + } else { + return -1; + } + + return 0; +} + +static av_cold int bitpacked_close_decoder(AVCodecContext *avctx) +{ + return 0; +} + +static int bitpacked_decode(AVCodecContext *avctx, void *data, int *got_frame, + AVPacket *avpkt) +{ + struct BitpackedContext *bc = avctx->priv_data; + int buf_size = avpkt->size; + AVFrame *frame = data; + int res; + + frame->pict_type = AV_PICTURE_TYPE_I; + frame->key_frame = 1; + + res = bc->decode(avctx, frame, avpkt); + if (res) + return res; + + *got_frame = 1; + return buf_size; + +} + +AVCodec ff_bitpacked_decoder = { + .name = "bitpacked", + .long_name = NULL_IF_CONFIG_SMALL("Bitpacked"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_BITPACKED, + .priv_data_size = sizeof(struct BitpackedContext), + .init = bitpacked_init_decoder, + .close = bitpacked_close_decoder, + .decode = bitpacked_decode, + .capabilities = AV_CODEC_CAP_EXPERIMENTAL, +}; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 42935d7560..94a9c5116c 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1367,6 +1367,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"), .props = AV_CODEC_PROP_LOSSLESS | AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_BITPACKED, + .type = AVMEDIA_TYPE_VIDEO, + .name = "bitpacked", + .long_name = NULL_IF_CONFIG_SMALL("Bitpacked"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, + }, /* image codecs */ {
Add a codec capable of decoding some formats of the RFC4175. For now it's only capable of handling YCbCr-4:2:2 with 8-bit or 10-bit depth. For 8-bit it's a simple pass-through, for 10-bit it depacks the stream in the AV_PIX_FMT_YUV422P10 pixel format. Signed-off-by: Damien Riegel <damien.riegel@savoirfairelinux.com> --- Changes in v3: - Codec has been renamed bitpacked (instead of vrawdepay) - A decoding function is now chosen at codec init based on the pixel format - Codec marked as experimental libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/bitpacked.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/codec_desc.c | 7 +++ 5 files changed, 158 insertions(+) create mode 100644 libavcodec/bitpacked.c