2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/common.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/log.h"
24 #include "libavutil/pixdesc.h"
26 #include "dv_profile.h"
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 },
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 },
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},
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},
58 /* macroblock bit budgets */
59 static const uint8_t block_sizes_dv2550[8] = {
60 112, 112, 112, 112, 80, 80, 0, 0,
63 static const uint8_t block_sizes_dv100[8] = {
64 80, 80, 80, 80, 80, 80, 64, 64,
67 static const DVprofile dv_profiles[] = {
70 .frame_size = 120000, /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
73 .time_base = { 1001, 30000 },
77 .sar = {{8, 9}, {32, 27}},
78 .pix_fmt = AV_PIX_FMT_YUV411P,
80 .block_sizes = block_sizes_dv2550,
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,
88 .frame_size = 144000, /* IEC 61834 - 625/50 (PAL) */
91 .time_base = { 1, 25 },
95 .sar = {{16, 15}, {64, 45}},
96 .pix_fmt = AV_PIX_FMT_YUV420P,
98 .block_sizes = block_sizes_dv2550,
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,
106 .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
109 .time_base = { 1, 25 },
113 .sar = {{16, 15}, {64, 45}},
114 .pix_fmt = AV_PIX_FMT_YUV411P,
116 .block_sizes = block_sizes_dv2550,
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,
124 .frame_size = 240000, /* SMPTE-314M - 525/60 (NTSC) 50 Mbps */
125 .difseg_size = 10, /* also known as "DVCPRO50" */
127 .time_base = { 1001, 30000 },
131 .sar = {{8, 9}, {32, 27}},
132 .pix_fmt = AV_PIX_FMT_YUV422P,
134 .block_sizes = block_sizes_dv2550,
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,
142 .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
143 .difseg_size = 12, /* also known as "DVCPRO50" */
145 .time_base = { 1, 25 },
149 .sar = {{16, 15}, {64, 45}},
150 .pix_fmt = AV_PIX_FMT_YUV422P,
152 .block_sizes = block_sizes_dv2550,
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,
160 .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
161 .difseg_size = 10, /* also known as "DVCPRO HD" */
163 .time_base = { 1001, 30000 },
167 .sar = {{1, 1}, {3, 2}},
168 .pix_fmt = AV_PIX_FMT_YUV422P,
170 .block_sizes = block_sizes_dv100,
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,
178 .frame_size = 576000, /* SMPTE-370M - 1080i50 100 Mbps */
179 .difseg_size = 12, /* also known as "DVCPRO HD" */
181 .time_base = { 1, 25 },
185 .sar = {{1, 1}, {4, 3}},
186 .pix_fmt = AV_PIX_FMT_YUV422P,
188 .block_sizes = block_sizes_dv100,
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,
196 .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
197 .difseg_size = 10, /* also known as "DVCPRO HD" */
199 .time_base = { 1001, 60000 },
203 .sar = {{1, 1}, {4, 3}},
204 .pix_fmt = AV_PIX_FMT_YUV422P,
206 .block_sizes = block_sizes_dv100,
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,
214 .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
215 .difseg_size = 12, /* also known as "DVCPRO HD" */
217 .time_base = { 1, 50 },
221 .sar = {{1, 1}, {4, 3}},
222 .pix_fmt = AV_PIX_FMT_YUV422P,
224 .block_sizes = block_sizes_dv100,
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,
232 .frame_size = 144000, /* IEC 61883-5 - 625/50 (PAL) */
235 .time_base = { 1, 25 },
239 .sar = {{16, 15}, {64, 45}},
240 .pix_fmt = AV_PIX_FMT_YUV420P,
242 .block_sizes = block_sizes_dv2550,
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,
250 const DVprofile* avpriv_dv_frame_profile2(AVCodecContext* codec, const DVprofile *sys,
251 const uint8_t* frame, unsigned buf_size)
255 if(buf_size < DV_PROFILE_BYTES)
258 dsf = (frame[3] & 0x80) >> 7;
259 stype = frame[80 * 5 + 48 + 3] & 0x1f;
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];
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];
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];
278 /* check if old sys matches and assumes corrupted input */
279 if (sys && buf_size == sys->frame_size)
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];
289 const DVprofile* avpriv_dv_frame_profile(const DVprofile *sys,
290 const uint8_t* frame, unsigned buf_size)
292 return avpriv_dv_frame_profile2(NULL, sys, frame, buf_size);
295 const DVprofile* avpriv_dv_codec_profile(AVCodecContext* codec)
300 if (codec->coded_width || codec->coded_height) {
301 w = codec->coded_width;
302 h = codec->coded_height;
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];
317 void ff_dv_print_profiles(void *logctx, int loglevel)
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);