blob: 4bb0f12d89f66fe98dc92b83a6d36f09c2458b8d
1 | /* |
2 | * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com > |
3 | * |
4 | * This file is part of FFmpeg. |
5 | * |
6 | * FFmpeg is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. |
10 | * |
11 | * FFmpeg is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with FFmpeg; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | /** |
22 | * @file |
23 | * function definitions common to libschroedinger decoder and encoder |
24 | */ |
25 | |
26 | #include "libavutil/attributes.h" |
27 | #include "libavutil/mem.h" |
28 | #include "libschroedinger.h" |
29 | #include "internal.h" |
30 | |
31 | static const SchroVideoFormatInfo ff_schro_video_format_info[] = { |
32 | { 640, 480, 24000, 1001}, |
33 | { 176, 120, 15000, 1001}, |
34 | { 176, 144, 25, 2 }, |
35 | { 352, 240, 15000, 1001}, |
36 | { 352, 288, 25, 2 }, |
37 | { 704, 480, 15000, 1001}, |
38 | { 704, 576, 25, 2 }, |
39 | { 720, 480, 30000, 1001}, |
40 | { 720, 576, 25, 1 }, |
41 | { 1280, 720, 60000, 1001}, |
42 | { 1280, 720, 50, 1 }, |
43 | { 1920, 1080, 30000, 1001}, |
44 | { 1920, 1080, 25, 1 }, |
45 | { 1920, 1080, 60000, 1001}, |
46 | { 1920, 1080, 50, 1 }, |
47 | { 2048, 1080, 24, 1 }, |
48 | { 4096, 2160, 24, 1 }, |
49 | }; |
50 | |
51 | static unsigned int get_video_format_idx(AVCodecContext *avctx) |
52 | { |
53 | unsigned int ret_idx = 0; |
54 | unsigned int idx; |
55 | unsigned int num_formats = sizeof(ff_schro_video_format_info) / |
56 | sizeof(ff_schro_video_format_info[0]); |
57 | |
58 | for (idx = 1; idx < num_formats; ++idx) { |
59 | const SchroVideoFormatInfo *vf = &ff_schro_video_format_info[idx]; |
60 | if (avctx->width == vf->width && |
61 | avctx->height == vf->height) { |
62 | ret_idx = idx; |
63 | if (avctx->time_base.den == vf->frame_rate_num && |
64 | avctx->time_base.num == vf->frame_rate_denom) |
65 | return idx; |
66 | } |
67 | } |
68 | return ret_idx; |
69 | } |
70 | |
71 | av_cold void ff_schro_queue_init(FFSchroQueue *queue) |
72 | { |
73 | queue->p_head = queue->p_tail = NULL; |
74 | queue->size = 0; |
75 | } |
76 | |
77 | void ff_schro_queue_free(FFSchroQueue *queue, void (*free_func)(void *)) |
78 | { |
79 | while (queue->p_head) |
80 | free_func(ff_schro_queue_pop(queue)); |
81 | } |
82 | |
83 | int ff_schro_queue_push_back(FFSchroQueue *queue, void *p_data) |
84 | { |
85 | FFSchroQueueElement *p_new = av_mallocz(sizeof(FFSchroQueueElement)); |
86 | |
87 | if (!p_new) |
88 | return -1; |
89 | |
90 | p_new->data = p_data; |
91 | |
92 | if (!queue->p_head) |
93 | queue->p_head = p_new; |
94 | else |
95 | queue->p_tail->next = p_new; |
96 | queue->p_tail = p_new; |
97 | |
98 | ++queue->size; |
99 | return 0; |
100 | } |
101 | |
102 | void *ff_schro_queue_pop(FFSchroQueue *queue) |
103 | { |
104 | FFSchroQueueElement *top = queue->p_head; |
105 | |
106 | if (top) { |
107 | void *data = top->data; |
108 | queue->p_head = queue->p_head->next; |
109 | --queue->size; |
110 | av_freep(&top); |
111 | return data; |
112 | } |
113 | |
114 | return NULL; |
115 | } |
116 | |
117 | /** |
118 | * Schroedinger video preset table. Ensure that this tables matches up correctly |
119 | * with the ff_schro_video_format_info table. |
120 | */ |
121 | static const SchroVideoFormatEnum ff_schro_video_formats[]={ |
122 | SCHRO_VIDEO_FORMAT_CUSTOM , |
123 | SCHRO_VIDEO_FORMAT_QSIF , |
124 | SCHRO_VIDEO_FORMAT_QCIF , |
125 | SCHRO_VIDEO_FORMAT_SIF , |
126 | SCHRO_VIDEO_FORMAT_CIF , |
127 | SCHRO_VIDEO_FORMAT_4SIF , |
128 | SCHRO_VIDEO_FORMAT_4CIF , |
129 | SCHRO_VIDEO_FORMAT_SD480I_60 , |
130 | SCHRO_VIDEO_FORMAT_SD576I_50 , |
131 | SCHRO_VIDEO_FORMAT_HD720P_60 , |
132 | SCHRO_VIDEO_FORMAT_HD720P_50 , |
133 | SCHRO_VIDEO_FORMAT_HD1080I_60 , |
134 | SCHRO_VIDEO_FORMAT_HD1080I_50 , |
135 | SCHRO_VIDEO_FORMAT_HD1080P_60 , |
136 | SCHRO_VIDEO_FORMAT_HD1080P_50 , |
137 | SCHRO_VIDEO_FORMAT_DC2K_24 , |
138 | SCHRO_VIDEO_FORMAT_DC4K_24 , |
139 | }; |
140 | |
141 | SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avctx) |
142 | { |
143 | unsigned int num_formats = sizeof(ff_schro_video_formats) / |
144 | sizeof(ff_schro_video_formats[0]); |
145 | |
146 | unsigned int idx = get_video_format_idx(avctx); |
147 | |
148 | return (idx < num_formats) ? ff_schro_video_formats[idx] : |
149 | SCHRO_VIDEO_FORMAT_CUSTOM; |
150 | } |
151 | |
152 | int ff_get_schro_frame_format (SchroChromaFormat schro_pix_fmt, |
153 | SchroFrameFormat *schro_frame_fmt) |
154 | { |
155 | unsigned int num_formats = sizeof(schro_pixel_format_map) / |
156 | sizeof(schro_pixel_format_map[0]); |
157 | |
158 | int idx; |
159 | |
160 | for (idx = 0; idx < num_formats; ++idx) { |
161 | if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) { |
162 | *schro_frame_fmt = schro_pixel_format_map[idx].schro_frame_fmt; |
163 | return 0; |
164 | } |
165 | } |
166 | return -1; |
167 | } |
168 | |
169 | static void free_schro_frame(SchroFrame *frame, void *priv) |
170 | { |
171 | AVFrame *p_pic = priv; |
172 | av_frame_free(&p_pic); |
173 | } |
174 | |
175 | SchroFrame *ff_create_schro_frame(AVCodecContext *avctx, |
176 | SchroFrameFormat schro_frame_fmt) |
177 | { |
178 | AVFrame *p_pic; |
179 | SchroFrame *p_frame; |
180 | int y_width, uv_width; |
181 | int y_height, uv_height; |
182 | int i; |
183 | |
184 | y_width = avctx->width; |
185 | y_height = avctx->height; |
186 | uv_width = y_width >> (SCHRO_FRAME_FORMAT_H_SHIFT(schro_frame_fmt)); |
187 | uv_height = y_height >> (SCHRO_FRAME_FORMAT_V_SHIFT(schro_frame_fmt)); |
188 | |
189 | p_pic = av_frame_alloc(); |
190 | if (!p_pic) |
191 | return NULL; |
192 | |
193 | if (ff_get_buffer(avctx, p_pic, AV_GET_BUFFER_FLAG_REF) < 0) { |
194 | av_frame_free(&p_pic); |
195 | return NULL; |
196 | } |
197 | |
198 | p_frame = schro_frame_new(); |
199 | p_frame->format = schro_frame_fmt; |
200 | p_frame->width = y_width; |
201 | p_frame->height = y_height; |
202 | schro_frame_set_free_callback(p_frame, free_schro_frame, p_pic); |
203 | |
204 | for (i = 0; i < 3; ++i) { |
205 | p_frame->components[i].width = i ? uv_width : y_width; |
206 | p_frame->components[i].stride = p_pic->linesize[i]; |
207 | p_frame->components[i].height = i ? uv_height : y_height; |
208 | p_frame->components[i].length = |
209 | p_frame->components[i].stride * p_frame->components[i].height; |
210 | p_frame->components[i].data = p_pic->data[i]; |
211 | |
212 | if (i) { |
213 | p_frame->components[i].v_shift = |
214 | SCHRO_FRAME_FORMAT_V_SHIFT(p_frame->format); |
215 | p_frame->components[i].h_shift = |
216 | SCHRO_FRAME_FORMAT_H_SHIFT(p_frame->format); |
217 | } |
218 | } |
219 | |
220 | return p_frame; |
221 | } |
222 |