]> git.sesse.net Git - ffmpeg/blob - libavutil/video_enc_params.c
635176ab91d0f70b3e5550683d9ac594223caa54
[ffmpeg] / libavutil / video_enc_params.c
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 <limits.h>
20 #include <stddef.h>
21 #include <stdint.h>
22
23 #include "buffer.h"
24 #include "common.h"
25 #include "frame.h"
26 #include "mem.h"
27 #include "video_enc_params.h"
28
29 AVVideoEncParams *av_video_enc_params_alloc(enum AVVideoEncParamsType type,
30                                             unsigned int nb_blocks, size_t *out_size)
31 {
32     AVVideoEncParams *par;
33     size_t size;
34
35     size = sizeof(*par);
36     if (nb_blocks > (SIZE_MAX - size) / sizeof(AVVideoBlockParams))
37         return NULL;
38     size += sizeof(AVVideoBlockParams) * nb_blocks;
39
40     par = av_mallocz(size);
41     if (!par)
42         return NULL;
43
44     par->type          = type;
45     par->nb_blocks     = nb_blocks;
46     par->block_size    = sizeof(AVVideoBlockParams);
47     par->blocks_offset = sizeof(*par);
48
49     if (out_size)
50         *out_size = size;
51
52     return par;
53 }
54
55 AVVideoEncParams*
56 av_video_enc_params_create_side_data(AVFrame *frame, enum AVVideoEncParamsType type,
57                                      unsigned int nb_blocks)
58 {
59     AVBufferRef      *buf;
60     AVVideoEncParams *par;
61     size_t size;
62
63     par = av_video_enc_params_alloc(type, nb_blocks, &size);
64     if (!par)
65         return NULL;
66     if (size > INT_MAX) {
67         av_free(par);
68         return NULL;
69     }
70     buf = av_buffer_create((uint8_t *)par, size, NULL, NULL, 0);
71     if (!buf) {
72         av_freep(&par);
73         return NULL;
74     }
75
76     if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS, buf)) {
77         av_buffer_unref(&buf);
78         return NULL;
79     }
80
81     return par;
82 }