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