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