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