FFmpeg
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 
30 struct 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;
42  PutBitContext pb;
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,
117  .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10,
118  AV_PIX_FMT_NONE },
119 };
ff_guess_coded_bitrate
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:1038
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:251
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:539
encode.h
FFCodec
Definition: codec_internal.h:127
av_get_bits_per_pixel
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:3122
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: bitpacked_enc.c:72
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
BitpackedContext::encode
int(* encode)(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Definition: bitpacked_enc.c:31
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#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:159
ff_bitpacked_encoder
const FFCodec ff_bitpacked_encoder
Definition: bitpacked_enc.c:107
PutBitContext
Definition: put_bits.h:50
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: bitpacked_enc.c:93
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:503
BitpackedContext
Definition: bitpacked_dec.c:34
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
encode_yuv422p10
static int encode_yuv422p10(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Definition: bitpacked_enc.c:34
AV_CODEC_ID_BITPACKED
@ AV_CODEC_ID_BITPACKED
Definition: codec_id.h:285
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1586
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:632
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:671
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
put_bits.h