2017-03-31 15:36:15 +00:00
|
|
|
/*
|
|
|
|
* 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 "avcodec.h"
|
2022-03-16 17:18:28 +00:00
|
|
|
#include "codec_internal.h"
|
2017-03-31 15:36:15 +00:00
|
|
|
#include "get_bits.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
2021-11-26 12:20:15 +00:00
|
|
|
#include "thread.h"
|
2017-03-31 15:36:15 +00:00
|
|
|
|
|
|
|
struct BitpackedContext {
|
|
|
|
int (*decode)(AVCodecContext *avctx, AVFrame *frame,
|
2021-03-04 12:13:20 +00:00
|
|
|
const AVPacket *pkt);
|
2017-03-31 15:36:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* For this format, it's a simple passthrough */
|
|
|
|
static int bitpacked_decode_uyvy422(AVCodecContext *avctx, AVFrame *frame,
|
2021-03-04 12:13:20 +00:00
|
|
|
const AVPacket *avpkt)
|
2017-03-31 15:36:15 +00:00
|
|
|
{
|
|
|
|
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);
|
2021-10-02 14:23:19 +00:00
|
|
|
if (!frame->buf[0]) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2017-03-31 15:36:15 +00:00
|
|
|
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,
|
2021-03-04 12:13:20 +00:00
|
|
|
const AVPacket *avpkt)
|
2017-03-31 15:36:15 +00:00
|
|
|
{
|
|
|
|
uint64_t frame_size = (uint64_t)avctx->width * (uint64_t)avctx->height * 20;
|
2017-04-05 19:51:13 +00:00
|
|
|
uint64_t packet_size = (uint64_t)avpkt->size * 8;
|
2017-03-31 15:36:15 +00:00
|
|
|
GetBitContext bc;
|
|
|
|
uint16_t *y, *u, *v;
|
2017-04-06 22:43:46 +00:00
|
|
|
int ret, i, j;
|
2017-03-31 15:36:15 +00:00
|
|
|
|
2022-02-06 13:49:23 +00:00
|
|
|
ret = ff_thread_get_buffer(avctx, frame, 0);
|
2017-03-31 15:36:15 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (frame_size > packet_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (avctx->width % 2)
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
|
|
|
|
ret = init_get_bits(&bc, avpkt->data, avctx->width * avctx->height * 20);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < avctx->height; i++) {
|
|
|
|
y = (uint16_t*)(frame->data[0] + i * frame->linesize[0]);
|
|
|
|
u = (uint16_t*)(frame->data[1] + i * frame->linesize[1]);
|
|
|
|
v = (uint16_t*)(frame->data[2] + i * frame->linesize[2]);
|
|
|
|
|
2017-04-06 22:43:46 +00:00
|
|
|
for (j = 0; j < avctx->width; j += 2) {
|
2017-03-31 15:36:15 +00:00
|
|
|
*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 AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
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 AVERROR_INVALIDDATA;
|
|
|
|
} else {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:33:24 +00:00
|
|
|
static int bitpacked_decode(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2017-03-31 15:36:15 +00:00
|
|
|
{
|
|
|
|
struct BitpackedContext *bc = avctx->priv_data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = bc->decode(avctx, frame, avpkt);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2021-11-26 11:49:01 +00:00
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
frame->key_frame = 1;
|
|
|
|
|
2017-03-31 15:36:15 +00:00
|
|
|
*got_frame = 1;
|
|
|
|
return buf_size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_bitpacked_decoder = {
|
|
|
|
.p.name = "bitpacked",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Bitpacked"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_BITPACKED,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_FRAME_THREADS,
|
2017-03-31 15:36:15 +00:00
|
|
|
.priv_data_size = sizeof(struct BitpackedContext),
|
|
|
|
.init = bitpacked_init_decoder,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_DECODE_CB(bitpacked_decode),
|
2020-02-07 13:17:28 +00:00
|
|
|
.codec_tags = (const uint32_t []){
|
2020-06-03 17:17:34 +00:00
|
|
|
MKTAG('U', 'Y', 'V', 'Y'),
|
2020-02-07 13:17:28 +00:00
|
|
|
FF_CODEC_TAGS_END,
|
|
|
|
},
|
2017-03-31 15:36:15 +00:00
|
|
|
};
|