FFmpeg
Loading...
Searching...
No Matches
bitpacked_dec.c
Go to the documentation of this file.
1/*
2 * Unpack bit-packed streams to formats supported by FFmpeg
3 * Copyright (c) 2017 Savoir-faire Linux, Inc
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/* Development sponsored by CBC/Radio-Canada */
23
24/**
25 * @file
26 * Bitpacked
27 */
28
29#include "avcodec.h"
30#include "bitpacked_dec.h"
31#include "bitpacked_dec_init.h"
32#include "codec_internal.h"
33#include "libavutil/imgutils.h"
34#include "thread.h"
35
36/* For this format, it's a simple passthrough */
38 const AVPacket *avpkt)
39{
40 int ret;
41
42 ret = av_image_fill_arrays(frame->data, frame->linesize, avpkt->data,
43 avctx->pix_fmt, avctx->width, avctx->height, 1);
44 if (ret < 0)
45 return ret;
46 if (ret > avpkt->size)
48
49 /* there is no need to copy as the data already match
50 * a known pixel format */
51 frame->buf[0] = av_buffer_ref(avpkt->buf);
52 if (!frame->buf[0]) {
53 return AVERROR(ENOMEM);
54 }
55
56 return 0;
57}
58
60 const AVPacket *avpkt)
61{
62 struct BitpackedContext *bc = avctx->priv_data;
63 uint64_t frame_size = (uint64_t)avctx->width * (uint64_t)avctx->height * 20;
64 uint64_t packet_size = (uint64_t)avpkt->size * 8;
65 const uint8_t *src;
66 uint16_t *y, *u, *v;
67 const int bulk = avctx->width & ~7;
68 int ret, i;
69
70 ret = ff_thread_get_buffer(avctx, frame, 0);
71 if (ret < 0)
72 return ret;
73
74 if (frame_size > packet_size)
76
77 if (avctx->width % 2)
79
80 src = avpkt->data;
81 for (i = 0; i < avctx->height; i++) {
82 y = (uint16_t*)(frame->data[0] + i * frame->linesize[0]);
83 u = (uint16_t*)(frame->data[1] + i * frame->linesize[1]);
84 v = (uint16_t*)(frame->data[2] + i * frame->linesize[2]);
85
86 if (bulk) {
87 bc->unpack_yuv422p10(src, y, u, v, bulk);
88 src += bulk * 5 / 2;
89 y += bulk;
90 u += bulk / 2;
91 v += bulk / 2;
92 }
93
94 bitpacked_unpack_yuv422p10_c(src, y, u, v, avctx->width - bulk);
95 src += (avctx->width - bulk) * 5 / 2;
96 }
97
98 return 0;
99}
100
102{
103 struct BitpackedContext *bc = avctx->priv_data;
104
105 if (!avctx->codec_tag || !avctx->width || !avctx->height)
106 return AVERROR_INVALIDDATA;
107
109
110 if (avctx->codec_tag == MKTAG('U', 'Y', 'V', 'Y')) {
111 if (avctx->bits_per_coded_sample == 16 &&
112 avctx->pix_fmt == AV_PIX_FMT_UYVY422)
114 else if (avctx->bits_per_coded_sample == 20 &&
117 else
118 return AVERROR_INVALIDDATA;
119 } else {
120 return AVERROR_INVALIDDATA;
121 }
122
123 return 0;
124}
125
127 int *got_frame, AVPacket *avpkt)
128{
129 struct BitpackedContext *bc = avctx->priv_data;
130 int buf_size = avpkt->size;
131 int res;
132
133 res = bc->decode(avctx, frame, avpkt);
134 if (res)
135 return res;
136
137 *got_frame = 1;
138 return buf_size;
139
140}
141
143 .p.name = "bitpacked",
144 CODEC_LONG_NAME("Bitpacked"),
145 .p.type = AVMEDIA_TYPE_VIDEO,
146 .p.id = AV_CODEC_ID_BITPACKED,
147 .p.capabilities = AV_CODEC_CAP_FRAME_THREADS,
148 .priv_data_size = sizeof(struct BitpackedContext),
151 .codec_tags = (const uint32_t []){
152 MKTAG('U', 'Y', 'V', 'Y'),
154 },
155};
const FFCodec ff_bitpacked_decoder
Libavcodec external API header.
static int bitpacked_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
static int bitpacked_decode_yuv422p10(AVCodecContext *avctx, AVFrame *frame, const AVPacket *avpkt)
static av_cold int bitpacked_init_decoder(AVCodecContext *avctx)
static int bitpacked_decode_uyvy422(AVCodecContext *avctx, AVFrame *frame, const AVPacket *avpkt)
static void bitpacked_unpack_yuv422p10_c(const uint8_t *src, uint16_t *y, uint16_t *u, uint16_t *v, int width)
static av_unused av_cold void ff_bitpackeddec_init(struct BitpackedContext *s)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_TAGS_END
FFCodec.codec_tags termination value.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_BITPACKED
Definition codec_id.h:276
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array.
Definition imgutils.c:446
misc image utilities
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
Multithreading API for decoders.
#define av_cold
Definition attributes.h:117
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1574
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:593
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
int(* decode)(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
void(* unpack_yuv422p10)(const uint8_t *src, uint16_t *y, uint16_t *u, uint16_t *v, int width)
#define src
Definition vp8dsp.c:248