]> git.sesse.net Git - ffmpeg/blob - libavcodec/libschroedinger.c
mpegvideo: dont call draw edges on lowres
[ffmpeg] / libavcodec / libschroedinger.c
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 "libschroedinger.h"
27
28 static const SchroVideoFormatInfo ff_schro_video_format_info[] = {
29     { 640,  480,  24000, 1001},
30     { 176,  120,  15000, 1001},
31     { 176,  144,  25,    2   },
32     { 352,  240,  15000, 1001},
33     { 352,  288,  25,    2   },
34     { 704,  480,  15000, 1001},
35     { 704,  576,  25,    2   },
36     { 720,  480,  30000, 1001},
37     { 720,  576,  25,    1   },
38     { 1280, 720,  60000, 1001},
39     { 1280, 720,  50,    1   },
40     { 1920, 1080, 30000, 1001},
41     { 1920, 1080, 25,    1   },
42     { 1920, 1080, 60000, 1001},
43     { 1920, 1080, 50,    1   },
44     { 2048, 1080, 24,    1   },
45     { 4096, 2160, 24,    1   },
46 };
47
48 static unsigned int get_video_format_idx(AVCodecContext *avccontext)
49 {
50     unsigned int ret_idx = 0;
51     unsigned int idx;
52     unsigned int num_formats = sizeof(ff_schro_video_format_info) /
53                                sizeof(ff_schro_video_format_info[0]);
54
55     for (idx = 1; idx < num_formats; ++idx) {
56         const SchroVideoFormatInfo *vf = &ff_schro_video_format_info[idx];
57         if (avccontext->width  == vf->width &&
58             avccontext->height == vf->height) {
59             ret_idx = idx;
60             if (avccontext->time_base.den == vf->frame_rate_num &&
61                 avccontext->time_base.num == vf->frame_rate_denom)
62                 return idx;
63         }
64     }
65     return ret_idx;
66 }
67
68 void ff_schro_queue_init(FFSchroQueue *queue)
69 {
70     queue->p_head = queue->p_tail = NULL;
71     queue->size = 0;
72 }
73
74 void ff_schro_queue_free(FFSchroQueue *queue, void (*free_func)(void *))
75 {
76     while (queue->p_head)
77         free_func(ff_schro_queue_pop(queue));
78 }
79
80 int ff_schro_queue_push_back(FFSchroQueue *queue, void *p_data)
81 {
82     FFSchroQueueElement *p_new = av_mallocz(sizeof(FFSchroQueueElement));
83
84     if (!p_new)
85         return -1;
86
87     p_new->data = p_data;
88
89     if (!queue->p_head)
90         queue->p_head = p_new;
91     else
92         queue->p_tail->next = p_new;
93     queue->p_tail = p_new;
94
95     ++queue->size;
96     return 0;
97 }
98
99 void *ff_schro_queue_pop(FFSchroQueue *queue)
100 {
101     FFSchroQueueElement *top = queue->p_head;
102
103     if (top) {
104         void *data = top->data;
105         queue->p_head = queue->p_head->next;
106         --queue->size;
107         av_freep(&top);
108         return data;
109     }
110
111     return NULL;
112 }
113
114 /**
115 * Schroedinger video preset table. Ensure that this tables matches up correctly
116 * with the ff_schro_video_format_info table.
117 */
118 static const SchroVideoFormatEnum ff_schro_video_formats[]={
119     SCHRO_VIDEO_FORMAT_CUSTOM     ,
120     SCHRO_VIDEO_FORMAT_QSIF       ,
121     SCHRO_VIDEO_FORMAT_QCIF       ,
122     SCHRO_VIDEO_FORMAT_SIF        ,
123     SCHRO_VIDEO_FORMAT_CIF        ,
124     SCHRO_VIDEO_FORMAT_4SIF       ,
125     SCHRO_VIDEO_FORMAT_4CIF       ,
126     SCHRO_VIDEO_FORMAT_SD480I_60  ,
127     SCHRO_VIDEO_FORMAT_SD576I_50  ,
128     SCHRO_VIDEO_FORMAT_HD720P_60  ,
129     SCHRO_VIDEO_FORMAT_HD720P_50  ,
130     SCHRO_VIDEO_FORMAT_HD1080I_60 ,
131     SCHRO_VIDEO_FORMAT_HD1080I_50 ,
132     SCHRO_VIDEO_FORMAT_HD1080P_60 ,
133     SCHRO_VIDEO_FORMAT_HD1080P_50 ,
134     SCHRO_VIDEO_FORMAT_DC2K_24    ,
135     SCHRO_VIDEO_FORMAT_DC4K_24    ,
136 };
137
138 SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avccontext)
139 {
140     unsigned int num_formats = sizeof(ff_schro_video_formats) /
141                                sizeof(ff_schro_video_formats[0]);
142
143     unsigned int idx = get_video_format_idx(avccontext);
144
145     return (idx < num_formats) ? ff_schro_video_formats[idx] :
146                                  SCHRO_VIDEO_FORMAT_CUSTOM;
147 }
148
149 int ff_get_schro_frame_format (SchroChromaFormat schro_pix_fmt,
150                                SchroFrameFormat  *schro_frame_fmt)
151 {
152     unsigned int num_formats = sizeof(schro_pixel_format_map) /
153                                sizeof(schro_pixel_format_map[0]);
154
155     int idx;
156
157     for (idx = 0; idx < num_formats; ++idx) {
158         if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
159             *schro_frame_fmt = schro_pixel_format_map[idx].schro_frame_fmt;
160             return 0;
161         }
162     }
163     return -1;
164 }
165
166 static void free_schro_frame(SchroFrame *frame, void *priv)
167 {
168     AVPicture *p_pic = priv;
169
170     if (!p_pic)
171         return;
172
173     avpicture_free(p_pic);
174     av_freep(&p_pic);
175 }
176
177 SchroFrame *ff_create_schro_frame(AVCodecContext *avccontext,
178                                   SchroFrameFormat schro_frame_fmt)
179 {
180     AVPicture *p_pic;
181     SchroFrame *p_frame;
182     int y_width, uv_width;
183     int y_height, uv_height;
184     int i;
185
186     y_width   = avccontext->width;
187     y_height  = avccontext->height;
188     uv_width  = y_width  >> (SCHRO_FRAME_FORMAT_H_SHIFT(schro_frame_fmt));
189     uv_height = y_height >> (SCHRO_FRAME_FORMAT_V_SHIFT(schro_frame_fmt));
190
191     p_pic = av_mallocz(sizeof(AVPicture));
192     if (!p_pic || avpicture_alloc(p_pic, avccontext->pix_fmt, y_width, y_height) < 0) {
193         av_free(p_pic);
194         return NULL;
195     }
196
197     p_frame         = schro_frame_new();
198     p_frame->format = schro_frame_fmt;
199     p_frame->width  = y_width;
200     p_frame->height = y_height;
201     schro_frame_set_free_callback(p_frame, free_schro_frame, (void *)p_pic);
202
203     for (i = 0; i < 3; ++i) {
204         p_frame->components[i].width  = i ? uv_width : y_width;
205         p_frame->components[i].stride = p_pic->linesize[i];
206         p_frame->components[i].height = i ? uv_height : y_height;
207         p_frame->components[i].length =
208                  p_frame->components[i].stride * p_frame->components[i].height;
209         p_frame->components[i].data   = p_pic->data[i];
210
211         if (i) {
212             p_frame->components[i].v_shift =
213                 SCHRO_FRAME_FORMAT_V_SHIFT(p_frame->format);
214             p_frame->components[i].h_shift =
215                 SCHRO_FRAME_FORMAT_H_SHIFT(p_frame->format);
216         }
217     }
218
219     return p_frame;
220 }