/src/ffmpeg/libavutil/video_enc_params.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of FFmpeg. |
3 | | * |
4 | | * FFmpeg is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * FFmpeg is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with FFmpeg; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | */ |
18 | | |
19 | | #include <stddef.h> |
20 | | #include <stdint.h> |
21 | | |
22 | | #include "buffer.h" |
23 | | #include "frame.h" |
24 | | #include "mem.h" |
25 | | #include "video_enc_params.h" |
26 | | |
27 | | AVVideoEncParams *av_video_enc_params_alloc(enum AVVideoEncParamsType type, |
28 | | unsigned int nb_blocks, size_t *out_size) |
29 | 766k | { |
30 | 766k | struct TestStruct { |
31 | 766k | AVVideoEncParams p; |
32 | 766k | AVVideoBlockParams b; |
33 | 766k | }; |
34 | 766k | const size_t blocks_offset = offsetof(struct TestStruct, b); |
35 | 766k | size_t size = blocks_offset; |
36 | 766k | AVVideoEncParams *par; |
37 | | |
38 | 766k | if (nb_blocks > (SIZE_MAX - size) / sizeof(AVVideoBlockParams)) |
39 | 0 | return NULL; |
40 | 766k | size += sizeof(AVVideoBlockParams) * nb_blocks; |
41 | | |
42 | 766k | par = av_mallocz(size); |
43 | 766k | if (!par) |
44 | 0 | return NULL; |
45 | | |
46 | 766k | par->type = type; |
47 | 766k | par->nb_blocks = nb_blocks; |
48 | 766k | par->block_size = sizeof(AVVideoBlockParams); |
49 | 766k | par->blocks_offset = blocks_offset; |
50 | | |
51 | 766k | if (out_size) |
52 | 766k | *out_size = size; |
53 | | |
54 | 766k | return par; |
55 | 766k | } |
56 | | |
57 | | AVVideoEncParams* |
58 | | av_video_enc_params_create_side_data(AVFrame *frame, enum AVVideoEncParamsType type, |
59 | | unsigned int nb_blocks) |
60 | 766k | { |
61 | 766k | AVBufferRef *buf; |
62 | 766k | AVVideoEncParams *par; |
63 | 766k | size_t size; |
64 | | |
65 | 766k | par = av_video_enc_params_alloc(type, nb_blocks, &size); |
66 | 766k | if (!par) |
67 | 0 | return NULL; |
68 | 766k | buf = av_buffer_create((uint8_t *)par, size, NULL, NULL, 0); |
69 | 766k | if (!buf) { |
70 | 0 | av_freep(&par); |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 766k | if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS, buf)) { |
75 | 0 | av_buffer_unref(&buf); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 766k | return par; |
80 | 766k | } |