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 "encode.h"
25 #include "internal.h"
26 #include "put_bits.h"
27 #include "libavutil/pixdesc.h"
28 
29 struct BitpackedContext {
31 };
32 
34 {
35  const int buf_size = avctx->height * avctx->width * avctx->bits_per_coded_sample / 8;
36  int ret;
37  uint8_t *dst;
38  const uint16_t *y;
39  const uint16_t *u;
40  const uint16_t *v;
41  PutBitContext pb;
42 
43  ret = ff_get_encode_buffer(avctx, pkt, buf_size, 0);
44  if (ret < 0) {
45  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
46  return ret;
47  }
48  dst = pkt->data;
49 
50  init_put_bits(&pb, dst, buf_size);
51 
52  for (int i = 0; i < avctx->height; i++) {
53  y = (uint16_t*)(frame->data[0] + i * frame->linesize[0]);
54  u = (uint16_t*)(frame->data[1] + i * frame->linesize[1]);
55  v = (uint16_t*)(frame->data[2] + i * frame->linesize[2]);
56 
57  for (int j = 0; j < avctx->width; j += 2) {
58  /* u, y0, v, y1 */
59  put_bits(&pb, 10, av_clip_uintp2(*u++, 10));
60  put_bits(&pb, 10, av_clip_uintp2(*y++, 10));
61  put_bits(&pb, 10, av_clip_uintp2(*v++, 10));
62  put_bits(&pb, 10, av_clip_uintp2(*y++, 10));
63  }
64  }
65  flush_put_bits(&pb);
66 
67  return 0;
68 }
69 
70 
72 {
73  struct BitpackedContext *s = avctx->priv_data;
75 
76  if (avctx->width & 1) {
77  av_log(avctx, AV_LOG_ERROR, "bitpacked needs even width\n");
78  return AVERROR(EINVAL);
79  }
80 
82  avctx->bit_rate = ff_guess_coded_bitrate(avctx);
83 
84  if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10)
85  s->encode = encode_yuv422p10;
86  else
87  return AVERROR(EINVAL);
88 
89  return 0;
90 }
91 
93  const AVFrame *frame, int *got_packet)
94 {
95  struct BitpackedContext *s = avctx->priv_data;
96  int ret;
97 
98  ret = s->encode(avctx, pkt, frame);
99  if (ret)
100  return ret;
101 
102  *got_packet = 1;
103  return 0;
104 }
105 
107  .name = "bitpacked",
108  .long_name = NULL_IF_CONFIG_SMALL("Bitpacked"),
109  .type = AVMEDIA_TYPE_VIDEO,
110  .id = AV_CODEC_ID_BITPACKED,
111  .priv_data_size = sizeof(struct BitpackedContext),
113  .init = encode_init,
114  .encode2 = encode_frame,
115  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10,
116  AV_PIX_FMT_NONE },
117  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
118 };
ff_bitpacked_encoder
const AVCodec ff_bitpacked_encoder
Definition: bitpacked_enc.c:106
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:1094
AVCodec
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
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:264
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:120
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
encode.h
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:2612
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: bitpacked_enc.c:71
BitpackedContext::encode
int(* encode)(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Definition: bitpacked_enc.c:30
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
PutBitContext
Definition: put_bits.h:49
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:113
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: bitpacked_enc.c:92
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:405
BitpackedContext
Definition: bitpacked_dec.c:35
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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
encode_yuv422p10
static int encode_yuv422p10(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Definition: bitpacked_enc.c:33
AV_CODEC_ID_BITPACKED
@ AV_CODEC_ID_BITPACKED
Definition: codec_id.h:280
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1418
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
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:383
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:78
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
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:142
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:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
int
int
Definition: ffmpeg_filter.c:153
put_bits.h