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