FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dv_profile.c
Go to the documentation of this file.
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 "config.h"
20 
21 #include <stdint.h>
22 
23 #include "libavutil/common.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/pixdesc.h"
27 
28 #include "avcodec.h"
29 #include "dv_profile.h"
30 #include "dv_profile_internal.h"
31 
32 #if CONFIG_DVPROFILE
33 
34 static const uint8_t dv_audio_shuffle525[10][9] = {
35  { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
36  { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
37  { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
38  { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
39  { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
40 
41  { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
42  { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
43  { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
44  { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
45  { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
46 };
47 
48 static const uint8_t dv_audio_shuffle625[12][9] = {
49  { 0, 36, 72, 26, 62, 98, 16, 52, 88 }, /* 1st channel */
50  { 6, 42, 78, 32, 68, 104, 22, 58, 94 },
51  { 12, 48, 84, 2, 38, 74, 28, 64, 100 },
52  { 18, 54, 90, 8, 44, 80, 34, 70, 106 },
53  { 24, 60, 96, 14, 50, 86, 4, 40, 76 },
54  { 30, 66, 102, 20, 56, 92, 10, 46, 82 },
55 
56  { 1, 37, 73, 27, 63, 99, 17, 53, 89 }, /* 2nd channel */
57  { 7, 43, 79, 33, 69, 105, 23, 59, 95 },
58  { 13, 49, 85, 3, 39, 75, 29, 65, 101 },
59  { 19, 55, 91, 9, 45, 81, 35, 71, 107 },
60  { 25, 61, 97, 15, 51, 87, 5, 41, 77 },
61  { 31, 67, 103, 21, 57, 93, 11, 47, 83 },
62 };
63 
64 /* macroblock bit budgets */
65 static const uint8_t block_sizes_dv2550[8] = {
66  112, 112, 112, 112, 80, 80, 0, 0,
67 };
68 
69 static const uint8_t block_sizes_dv100[8] = {
70  80, 80, 80, 80, 80, 80, 64, 64,
71 };
72 
73 static const AVDVProfile dv_profiles[] = {
74  { .dsf = 0,
75  .video_stype = 0x0,
76  .frame_size = 120000, /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
77  .difseg_size = 10,
78  .n_difchan = 1,
79  .time_base = { 1001, 30000 },
80  .ltc_divisor = 30,
81  .height = 480,
82  .width = 720,
83  .sar = { { 8, 9 }, { 32, 27 } },
84  .pix_fmt = AV_PIX_FMT_YUV411P,
85  .bpm = 6,
86  .block_sizes = block_sizes_dv2550,
87  .audio_stride = 90,
88  .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
89  .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
90  .audio_shuffle = dv_audio_shuffle525, },
91  { .dsf = 1,
92  .video_stype = 0x0,
93  .frame_size = 144000, /* IEC 61834 - 625/50 (PAL) */
94  .difseg_size = 12,
95  .n_difchan = 1,
96  .time_base = { 1, 25 },
97  .ltc_divisor = 25,
98  .height = 576,
99  .width = 720,
100  .sar = { { 16, 15 }, { 64, 45 } },
101  .pix_fmt = AV_PIX_FMT_YUV420P,
102  .bpm = 6,
103  .block_sizes = block_sizes_dv2550,
104  .audio_stride = 108,
105  .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
106  .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
107  .audio_shuffle = dv_audio_shuffle625, },
108  { .dsf = 1,
109  .video_stype = 0x0,
110  .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
111  .difseg_size = 12,
112  .n_difchan = 1,
113  .time_base = { 1, 25 },
114  .ltc_divisor = 25,
115  .height = 576,
116  .width = 720,
117  .sar = { { 16, 15 }, { 64, 45 } },
118  .pix_fmt = AV_PIX_FMT_YUV411P,
119  .bpm = 6,
120  .block_sizes = block_sizes_dv2550,
121  .audio_stride = 108,
122  .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
123  .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
124  .audio_shuffle = dv_audio_shuffle625, },
125  { .dsf = 0,
126  .video_stype = 0x4,
127  .frame_size = 240000, /* SMPTE-314M - 525/60 (NTSC) 50 Mbps */
128  .difseg_size = 10, /* also known as "DVCPRO50" */
129  .n_difchan = 2,
130  .time_base = { 1001, 30000 },
131  .ltc_divisor = 30,
132  .height = 480,
133  .width = 720,
134  .sar = { { 8, 9 }, { 32, 27 } },
135  .pix_fmt = AV_PIX_FMT_YUV422P,
136  .bpm = 6,
137  .block_sizes = block_sizes_dv2550,
138  .audio_stride = 90,
139  .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
140  .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
141  .audio_shuffle = dv_audio_shuffle525, },
142  { .dsf = 1,
143  .video_stype = 0x4,
144  .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
145  .difseg_size = 12, /* also known as "DVCPRO50" */
146  .n_difchan = 2,
147  .time_base = { 1, 25 },
148  .ltc_divisor = 25,
149  .height = 576,
150  .width = 720,
151  .sar = { { 16, 15 }, { 64, 45 } },
152  .pix_fmt = AV_PIX_FMT_YUV422P,
153  .bpm = 6,
154  .block_sizes = block_sizes_dv2550,
155  .audio_stride = 108,
156  .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
157  .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
158  .audio_shuffle = dv_audio_shuffle625, },
159  { .dsf = 0,
160  .video_stype = 0x14,
161  .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
162  .difseg_size = 10, /* also known as "DVCPRO HD" */
163  .n_difchan = 4,
164  .time_base = { 1001, 30000 },
165  .ltc_divisor = 30,
166  .height = 1080,
167  .width = 1280,
168  .sar = { { 1, 1 }, { 3, 2 } },
169  .pix_fmt = AV_PIX_FMT_YUV422P,
170  .bpm = 8,
171  .block_sizes = block_sizes_dv100,
172  .audio_stride = 90,
173  .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
174  .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
175  .audio_shuffle = dv_audio_shuffle525, },
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  { .dsf = 0,
194  .video_stype = 0x18,
195  .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
196  .difseg_size = 10, /* also known as "DVCPRO HD" */
197  .n_difchan = 2,
198  .time_base = { 1001, 60000 },
199  .ltc_divisor = 60,
200  .height = 720,
201  .width = 960,
202  .sar = { { 1, 1 }, { 4, 3 } },
203  .pix_fmt = AV_PIX_FMT_YUV422P,
204  .bpm = 8,
205  .block_sizes = block_sizes_dv100,
206  .audio_stride = 90,
207  .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
208  .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
209  .audio_shuffle = dv_audio_shuffle525, },
210  { .dsf = 1,
211  .video_stype = 0x18,
212  .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
213  .difseg_size = 12, /* also known as "DVCPRO HD" */
214  .n_difchan = 2,
215  .time_base = { 1, 50 },
216  .ltc_divisor = 50,
217  .height = 720,
218  .width = 960,
219  .sar = { { 1, 1 }, { 4, 3 } },
220  .pix_fmt = AV_PIX_FMT_YUV422P,
221  .bpm = 8,
222  .block_sizes = block_sizes_dv100,
223  .audio_stride = 90,
224  .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
225  .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
226  .audio_shuffle = dv_audio_shuffle625, },
227  { .dsf = 1,
228  .video_stype = 0x1,
229  .frame_size = 144000, /* IEC 61883-5 - 625/50 (PAL) */
230  .difseg_size = 12,
231  .n_difchan = 1,
232  .time_base = { 1, 25 },
233  .ltc_divisor = 25,
234  .height = 576,
235  .width = 720,
236  .sar = { { 16, 15 }, { 64, 45 } },
237  .pix_fmt = AV_PIX_FMT_YUV420P,
238  .bpm = 6,
239  .block_sizes = block_sizes_dv2550,
240  .audio_stride = 108,
241  .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
242  .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
243  .audio_shuffle = dv_audio_shuffle625, }
244 };
245 
246 void ff_dv_print_profiles(void *logctx, int loglevel)
247 {
248  int i;
249  for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++) {
250  const AVDVProfile *p = &dv_profiles[i];
251  av_log(logctx, loglevel,
252  "Frame size: %dx%d; pixel format: %s, framerate: %d/%d\n",
254  p->time_base.den, p->time_base.num);
255  }
256 }
257 
258 #endif /* CONFIG_DVPROFILE */
259 
261  const uint8_t *frame, unsigned buf_size)
262 {
263 #if CONFIG_DVPROFILE
264  int i, dsf, stype;
265 
266  if(buf_size < DV_PROFILE_BYTES)
267  return NULL;
268 
269  dsf = (frame[3] & 0x80) >> 7;
270  stype = frame[80 * 5 + 48 + 3] & 0x1f;
271 
272  /* 576i50 25Mbps 4:1:1 is a special case */
273  if ((dsf == 1 && stype == 0 && frame[4] & 0x07 /* the APT field */) ||
274  (stype == 31 && codec && codec->codec_tag==AV_RL32("SL25") && codec->coded_width==720 && codec->coded_height==576))
275  return &dv_profiles[2];
276 
277  if( stype == 0
278  && codec
279  && (codec->codec_tag==AV_RL32("dvsd") || codec->codec_tag==AV_RL32("CDVC"))
280  && codec->coded_width ==720
281  && codec->coded_height==576)
282  return &dv_profiles[1];
283 
284  for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
285  if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)
286  return &dv_profiles[i];
287 
288  /* check if old sys matches and assumes corrupted input */
289  if (sys && buf_size == sys->frame_size)
290  return sys;
291 
292  /* hack for trac issue #217, dv files created with QuickTime 3 */
293  if ((frame[3] & 0x7f) == 0x3f && frame[80 * 5 + 48 + 3] == 0xff)
294  return &dv_profiles[dsf];
295 #endif
296 
297  return NULL;
298 }
299 
301  const uint8_t *frame, unsigned buf_size)
302 {
303  return ff_dv_frame_profile(NULL, sys, frame, buf_size);
304 }
305 
307  enum AVPixelFormat pix_fmt)
308 {
309 #if CONFIG_DVPROFILE
310  return av_dv_codec_profile2(width, height, pix_fmt, (AVRational){0, 0});
311 #endif
312 
313  return NULL;
314 }
315 
317  enum AVPixelFormat pix_fmt,
318  AVRational frame_rate)
319 {
320  const AVDVProfile *p = NULL;
321 #if CONFIG_DVPROFILE
322  int i;
323  /* frame rate is necessary to select between 720p50 and 720p60 profiles */
324  int invalid_framerate = frame_rate.num == 0 || frame_rate.den == 0;
325 
326  for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
327  if (height == dv_profiles[i].height &&
328  pix_fmt == dv_profiles[i].pix_fmt &&
329  width == dv_profiles[i].width)
330  {
331  if( invalid_framerate || av_div_q(dv_profiles[i].time_base, frame_rate).num == 1 )
332  return &dv_profiles[i];
333 
334  if(!p)
335  p = &dv_profiles[i];
336  }
337 #endif
338 
339  return p;
340 }
341 
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1705
int num
Numerator.
Definition: rational.h:59
void ff_dv_print_profiles(void *logctx, int loglevel)
Print all allowed DV profiles into logctx at specified logging level.
uint8_t
const AVDVProfile * sys
Definition: dvenc.c:49
const AVDVProfile * ff_dv_frame_profile(AVCodecContext *codec, const AVDVProfile *sys, const uint8_t *frame, unsigned buf_size)
Get a DV profile for the provided compressed frame.
Definition: dv_profile.c:260
static AVFrame * frame
#define height
enum AVPixelFormat pix_fmt
Definition: dv_profile.h:50
AVRational time_base
Definition: dv_profile.h:45
#define av_log(a,...)
const AVDVProfile * av_dv_frame_profile(const AVDVProfile *sys, const uint8_t *frame, unsigned buf_size)
Get a DV profile for the provided compressed frame.
Definition: dv_profile.c:300
int frame_size
Definition: dv_profile.h:42
uint16_t width
Definition: gdv.c:47
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
const AVDVProfile * av_dv_codec_profile(int width, int height, enum AVPixelFormat pix_fmt)
Get a DV profile for the provided stream parameters.
Definition: dv_profile.c:306
#define FF_ARRAY_ELEMS(a)
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1518
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1543
int coded_height
Definition: avcodec.h:1705
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define DV_PROFILE_BYTES
Definition: dv_profile.h:30
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal and external API header
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
int den
Denominator.
Definition: rational.h:60
const AVDVProfile * av_dv_codec_profile2(int width, int height, enum AVPixelFormat pix_fmt, AVRational frame_rate)
Get a DV profile for the provided stream parameters.
Definition: dv_profile.c:316
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2279
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60