lavc: export DV profile API used by muxer/demuxer as public
[libav.git] / libavcodec / dv_profile.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdint.h>
20
21 #include "config.h"
22
23 #include "libavutil/common.h"
24 #include "libavutil/log.h"
25 #include "libavutil/pixdesc.h"
26 #include "avcodec.h"
27 #include "dv_profile.h"
28 #include "dv_profile_internal.h"
29
30 #if CONFIG_DVPROFILE
31
32 static const uint8_t dv_audio_shuffle525[10][9] = {
33 { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
34 { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
35 { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
36 { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
37 { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
38
39 { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
40 { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
41 { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
42 { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
43 { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
44 };
45
46 static const uint8_t dv_audio_shuffle625[12][9] = {
47 { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
48 { 6, 42, 78, 32, 68, 104, 22, 58, 94},
49 { 12, 48, 84, 2, 38, 74, 28, 64, 100},
50 { 18, 54, 90, 8, 44, 80, 34, 70, 106},
51 { 24, 60, 96, 14, 50, 86, 4, 40, 76},
52 { 30, 66, 102, 20, 56, 92, 10, 46, 82},
53
54 { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
55 { 7, 43, 79, 33, 69, 105, 23, 59, 95},
56 { 13, 49, 85, 3, 39, 75, 29, 65, 101},
57 { 19, 55, 91, 9, 45, 81, 35, 71, 107},
58 { 25, 61, 97, 15, 51, 87, 5, 41, 77},
59 { 31, 67, 103, 21, 57, 93, 11, 47, 83},
60 };
61
62 /* macroblock bit budgets */
63 static const uint8_t block_sizes_dv2550[8] = {
64 112, 112, 112, 112, 80, 80, 0, 0,
65 };
66
67 static const uint8_t block_sizes_dv100[8] = {
68 80, 80, 80, 80, 80, 80, 64, 64,
69 };
70
71 static const AVDVProfile dv_profiles[] = {
72 { .dsf = 0,
73 .video_stype = 0x0,
74 .frame_size = 120000, /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
75 .difseg_size = 10,
76 .n_difchan = 1,
77 .time_base = { 1001, 30000 },
78 .ltc_divisor = 30,
79 .height = 480,
80 .width = 720,
81 .sar = {{8, 9}, {32, 27}},
82 .pix_fmt = AV_PIX_FMT_YUV411P,
83 .bpm = 6,
84 .block_sizes = block_sizes_dv2550,
85 .audio_stride = 90,
86 .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
87 .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
88 .audio_shuffle = dv_audio_shuffle525,
89 },
90 { .dsf = 1,
91 .video_stype = 0x0,
92 .frame_size = 144000, /* IEC 61834 - 625/50 (PAL) */
93 .difseg_size = 12,
94 .n_difchan = 1,
95 .time_base = { 1, 25 },
96 .ltc_divisor = 25,
97 .height = 576,
98 .width = 720,
99 .sar = {{16, 15}, {64, 45}},
100 .pix_fmt = AV_PIX_FMT_YUV420P,
101 .bpm = 6,
102 .block_sizes = block_sizes_dv2550,
103 .audio_stride = 108,
104 .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
105 .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
106 .audio_shuffle = dv_audio_shuffle625,
107 },
108 { .dsf = 1,
109 .video_stype = 0x0,
110 .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
111 .difseg_size = 12,
112 .n_difchan = 1,
113 .time_base = { 1, 25 },
114 .ltc_divisor = 25,
115 .height = 576,
116 .width = 720,
117 .sar = {{16, 15}, {64, 45}},
118 .pix_fmt = AV_PIX_FMT_YUV411P,
119 .bpm = 6,
120 .block_sizes = block_sizes_dv2550,
121 .audio_stride = 108,
122 .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
123 .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
124 .audio_shuffle = dv_audio_shuffle625,
125 },
126 { .dsf = 0,
127 .video_stype = 0x4,
128 .frame_size = 240000, /* SMPTE-314M - 525/60 (NTSC) 50 Mbps */
129 .difseg_size = 10, /* also known as "DVCPRO50" */
130 .n_difchan = 2,
131 .time_base = { 1001, 30000 },
132 .ltc_divisor = 30,
133 .height = 480,
134 .width = 720,
135 .sar = {{8, 9}, {32, 27}},
136 .pix_fmt = AV_PIX_FMT_YUV422P,
137 .bpm = 6,
138 .block_sizes = block_sizes_dv2550,
139 .audio_stride = 90,
140 .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
141 .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
142 .audio_shuffle = dv_audio_shuffle525,
143 },
144 { .dsf = 1,
145 .video_stype = 0x4,
146 .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
147 .difseg_size = 12, /* also known as "DVCPRO50" */
148 .n_difchan = 2,
149 .time_base = { 1, 25 },
150 .ltc_divisor = 25,
151 .height = 576,
152 .width = 720,
153 .sar = {{16, 15}, {64, 45}},
154 .pix_fmt = AV_PIX_FMT_YUV422P,
155 .bpm = 6,
156 .block_sizes = block_sizes_dv2550,
157 .audio_stride = 108,
158 .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
159 .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
160 .audio_shuffle = dv_audio_shuffle625,
161 },
162 { .dsf = 0,
163 .video_stype = 0x14,
164 .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
165 .difseg_size = 10, /* also known as "DVCPRO HD" */
166 .n_difchan = 4,
167 .time_base = { 1001, 30000 },
168 .ltc_divisor = 30,
169 .height = 1080,
170 .width = 1280,
171 .sar = {{1, 1}, {3, 2}},
172 .pix_fmt = AV_PIX_FMT_YUV422P,
173 .bpm = 8,
174 .block_sizes = block_sizes_dv100,
175 .audio_stride = 90,
176 .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
177 .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
178 .audio_shuffle = dv_audio_shuffle525,
179 },
180 { .dsf = 1,
181 .video_stype = 0x14,
182 .frame_size = 576000, /* SMPTE-370M - 1080i50 100 Mbps */
183 .difseg_size = 12, /* also known as "DVCPRO HD" */
184 .n_difchan = 4,
185 .time_base = { 1, 25 },
186 .ltc_divisor = 25,
187 .height = 1080,
188 .width = 1440,
189 .sar = {{1, 1}, {4, 3}},
190 .pix_fmt = AV_PIX_FMT_YUV422P,
191 .bpm = 8,
192 .block_sizes = block_sizes_dv100,
193 .audio_stride = 108,
194 .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
195 .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
196 .audio_shuffle = dv_audio_shuffle625,
197 },
198 { .dsf = 0,
199 .video_stype = 0x18,
200 .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
201 .difseg_size = 10, /* also known as "DVCPRO HD" */
202 .n_difchan = 2,
203 .time_base = { 1001, 60000 },
204 .ltc_divisor = 60,
205 .height = 720,
206 .width = 960,
207 .sar = {{1, 1}, {4, 3}},
208 .pix_fmt = AV_PIX_FMT_YUV422P,
209 .bpm = 8,
210 .block_sizes = block_sizes_dv100,
211 .audio_stride = 90,
212 .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
213 .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
214 .audio_shuffle = dv_audio_shuffle525,
215 },
216 { .dsf = 1,
217 .video_stype = 0x18,
218 .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
219 .difseg_size = 12, /* also known as "DVCPRO HD" */
220 .n_difchan = 2,
221 .time_base = { 1, 50 },
222 .ltc_divisor = 50,
223 .height = 720,
224 .width = 960,
225 .sar = {{1, 1}, {4, 3}},
226 .pix_fmt = AV_PIX_FMT_YUV422P,
227 .bpm = 8,
228 .block_sizes = block_sizes_dv100,
229 .audio_stride = 90,
230 .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
231 .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
232 .audio_shuffle = dv_audio_shuffle625,
233 },
234 { .dsf = 1,
235 .video_stype = 0x1,
236 .frame_size = 144000, /* IEC 61883-5 - 625/50 (PAL) */
237 .difseg_size = 12,
238 .n_difchan = 1,
239 .time_base = { 1, 25 },
240 .ltc_divisor = 25,
241 .height = 576,
242 .width = 720,
243 .sar = {{16, 15}, {64, 45}},
244 .pix_fmt = AV_PIX_FMT_YUV420P,
245 .bpm = 6,
246 .block_sizes = block_sizes_dv2550,
247 .audio_stride = 108,
248 .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
249 .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
250 .audio_shuffle = dv_audio_shuffle625,
251 }
252 };
253
254 void ff_dv_print_profiles(void *logctx, int loglevel)
255 {
256 int i;
257 for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++) {
258 const AVDVProfile *p = &dv_profiles[i];
259 av_log(logctx, loglevel, "Frame size: %dx%d; pixel format: %s, "
260 "framerate: %d/%d\n", p->width, p->height, av_get_pix_fmt_name(p->pix_fmt),
261 p->time_base.den, p->time_base.num);
262 }
263 }
264
265 #endif /* CONFIG_DVPROFILE */
266
267 const AVDVProfile *av_dv_frame_profile(const AVDVProfile *sys,
268 const uint8_t* frame, unsigned buf_size)
269 {
270 #if CONFIG_DVPROFILE
271 int i, dsf, stype;
272
273 if (buf_size < 80 * 5 + 48 + 4)
274 return NULL;
275
276 dsf = (frame[3] & 0x80) >> 7;
277 stype = frame[80 * 5 + 48 + 3] & 0x1f;
278
279 /* 576i50 25Mbps 4:1:1 is a special case */
280 if (dsf == 1 && stype == 0 && frame[4] & 0x07 /* the APT field */) {
281 return &dv_profiles[2];
282 }
283
284 for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
285 if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)
286 return &dv_profiles[i];
287
288 /* check if old sys matches and assumes corrupted input */
289 if (sys && buf_size == sys->frame_size)
290 return sys;
291 #endif
292
293 return NULL;
294 }
295
296 const AVDVProfile *av_dv_codec_profile(int width, int height,
297 enum AVPixelFormat pix_fmt)
298 {
299 #if CONFIG_DVPROFILE
300 int i;
301
302 for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)
303 if (height == dv_profiles[i].height &&
304 pix_fmt == dv_profiles[i].pix_fmt &&
305 width == dv_profiles[i].width)
306 return &dv_profiles[i];
307 #endif
308
309 return NULL;
310 }
311
312 #if LIBAVCODEC_VERSION_MAJOR < 56
313 const AVDVProfile *avpriv_dv_frame_profile(const AVDVProfile *sys,
314 const uint8_t* frame, unsigned buf_size)
315 {
316 return av_dv_frame_profile(sys, frame, buf_size);
317 }
318
319 const AVDVProfile *avpriv_dv_codec_profile(AVCodecContext *codec)
320 {
321 return av_dv_codec_profile(codec->width, codec->height, codec->pix_fmt);
322 }
323 #endif