/src/ffmpeg/libavcodec/bitpacked_enc.c
Line | Count | Source |
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 | | |
34 | | static int encode_yuv422p10(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame) |
35 | 6.08k | { |
36 | 6.08k | const int buf_size = avctx->height * avctx->width * avctx->bits_per_coded_sample / 8; |
37 | 6.08k | int ret; |
38 | 6.08k | uint8_t *dst; |
39 | 6.08k | const uint16_t *y; |
40 | 6.08k | const uint16_t *u; |
41 | 6.08k | const uint16_t *v; |
42 | 6.08k | PutBitContext pb; |
43 | | |
44 | 6.08k | ret = ff_get_encode_buffer(avctx, pkt, buf_size, 0); |
45 | 6.08k | if (ret < 0) { |
46 | 0 | av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n"); |
47 | 0 | return ret; |
48 | 0 | } |
49 | 6.08k | dst = pkt->data; |
50 | | |
51 | 6.08k | init_put_bits(&pb, dst, buf_size); |
52 | | |
53 | 818k | for (int i = 0; i < avctx->height; i++) { |
54 | 812k | y = (uint16_t*)(frame->data[0] + i * frame->linesize[0]); |
55 | 812k | u = (uint16_t*)(frame->data[1] + i * frame->linesize[1]); |
56 | 812k | v = (uint16_t*)(frame->data[2] + i * frame->linesize[2]); |
57 | | |
58 | 4.71M | for (int j = 0; j < avctx->width; j += 2) { |
59 | | /* u, y0, v, y1 */ |
60 | 3.90M | put_bits(&pb, 10, av_clip_uintp2(*u++, 10)); |
61 | 3.90M | put_bits(&pb, 10, av_clip_uintp2(*y++, 10)); |
62 | 3.90M | put_bits(&pb, 10, av_clip_uintp2(*v++, 10)); |
63 | 3.90M | put_bits(&pb, 10, av_clip_uintp2(*y++, 10)); |
64 | 3.90M | } |
65 | 812k | } |
66 | 6.08k | flush_put_bits(&pb); |
67 | | |
68 | 6.08k | return 0; |
69 | 6.08k | } |
70 | | |
71 | | |
72 | | static av_cold int encode_init(AVCodecContext *avctx) |
73 | 272 | { |
74 | 272 | struct BitpackedContext *s = avctx->priv_data; |
75 | 272 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); |
76 | | |
77 | 272 | if (avctx->width & 1) { |
78 | 6 | av_log(avctx, AV_LOG_ERROR, "bitpacked needs even width\n"); |
79 | 6 | return AVERROR(EINVAL); |
80 | 6 | } |
81 | | |
82 | 266 | avctx->bits_per_coded_sample = av_get_bits_per_pixel(desc); |
83 | 266 | avctx->bit_rate = ff_guess_coded_bitrate(avctx); |
84 | | |
85 | 266 | if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) |
86 | 266 | s->encode = encode_yuv422p10; |
87 | 0 | else |
88 | 0 | return AVERROR(EINVAL); |
89 | | |
90 | 266 | return 0; |
91 | 266 | } |
92 | | |
93 | | static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
94 | | const AVFrame *frame, int *got_packet) |
95 | 6.08k | { |
96 | 6.08k | struct BitpackedContext *s = avctx->priv_data; |
97 | 6.08k | int ret; |
98 | | |
99 | 6.08k | ret = s->encode(avctx, pkt, frame); |
100 | 6.08k | if (ret) |
101 | 0 | return ret; |
102 | | |
103 | 6.08k | *got_packet = 1; |
104 | 6.08k | return 0; |
105 | 6.08k | } |
106 | | |
107 | | const FFCodec ff_bitpacked_encoder = { |
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 | |
114 | | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
115 | | .init = encode_init, |
116 | | FF_CODEC_ENCODE_CB(encode_frame), |
117 | | CODEC_PIXFMTS(AV_PIX_FMT_YUV422P10), |
118 | | }; |