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