FFmpeg
Loading...
Searching...
No Matches
bitpacked_enc.c
Go to the documentation of this file.
1/*
2 * bitpacked encoder
3 *
4 * Copyright (c) 2021 Limin Wang
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "avcodec.h"
24#include "codec_internal.h"
25#include "encode.h"
26#include "internal.h"
27#include "put_bits.h"
28#include "libavutil/pixdesc.h"
29
30struct BitpackedContext {
31 int (*encode)(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame);
32};
33
35{
36 const int buf_size = avctx->height * avctx->width * avctx->bits_per_coded_sample / 8;
37 int ret;
38 uint8_t *dst;
39 const uint16_t *y;
40 const uint16_t *u;
41 const uint16_t *v;
43
44 ret = ff_get_encode_buffer(avctx, pkt, buf_size, 0);
45 if (ret < 0) {
46 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
47 return ret;
48 }
49 dst = pkt->data;
50
51 init_put_bits(&pb, dst, buf_size);
52
53 for (int i = 0; i < avctx->height; i++) {
54 y = (uint16_t*)(frame->data[0] + i * frame->linesize[0]);
55 u = (uint16_t*)(frame->data[1] + i * frame->linesize[1]);
56 v = (uint16_t*)(frame->data[2] + i * frame->linesize[2]);
57
58 for (int j = 0; j < avctx->width; j += 2) {
59 /* u, y0, v, y1 */
60 put_bits(&pb, 10, av_clip_uintp2(*u++, 10));
61 put_bits(&pb, 10, av_clip_uintp2(*y++, 10));
62 put_bits(&pb, 10, av_clip_uintp2(*v++, 10));
63 put_bits(&pb, 10, av_clip_uintp2(*y++, 10));
64 }
65 }
66 flush_put_bits(&pb);
67
68 return 0;
69}
70
71
73{
74 struct BitpackedContext *s = avctx->priv_data;
76
77 if (avctx->width & 1) {
78 av_log(avctx, AV_LOG_ERROR, "bitpacked needs even width\n");
79 return AVERROR(EINVAL);
80 }
81
83 avctx->bit_rate = ff_guess_coded_bitrate(avctx);
84
85 if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10)
86 s->encode = encode_yuv422p10;
87 else
88 return AVERROR(EINVAL);
89
90 return 0;
91}
92
94 const AVFrame *frame, int *got_packet)
95{
96 struct BitpackedContext *s = avctx->priv_data;
97 int ret;
98
99 ret = s->encode(avctx, pkt, frame);
100 if (ret)
101 return ret;
102
103 *got_packet = 1;
104 return 0;
105}
106
108 .p.name = "bitpacked",
109 CODEC_LONG_NAME("Bitpacked"),
110 .p.type = AVMEDIA_TYPE_VIDEO,
111 .p.id = AV_CODEC_ID_BITPACKED,
112 .priv_data_size = sizeof(struct BitpackedContext),
113 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
115 .init = encode_init,
118};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_bitpacked_encoder
static av_cold int encode_init(AVCodecContext *avctx)
Definition asvenc.c:373
Libavcodec external API header.
static av_cold int encode_init(AVCodecContext *avctx)
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static int encode_yuv422p10(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip_uintp2
Definition common.h:124
static AVPacket * pkt
static AVFrame * frame
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition ffmpeg_enc.c:694
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_BITPACKED
Definition codec_id.h:276
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
common internal api header.
int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
Get an estimated video bitrate based on frame size, frame rate and coded bits per pixel.
Definition utils.c:1075
#define av_cold
Definition attributes.h:117
const char * desc
Definition libsvtav1.c:83
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition pixdesc.c:3412
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int(* encode)(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
#define av_log(a,...)