Commit | Line | Data |
---|---|---|
f20dca40 | 1 | /* |
7458ccbb RS |
2 | * General DV muxer/demuxer |
3 | * Copyright (c) 2003 Roman Shaposhnick | |
4 | * | |
5 | * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth | |
6 | * of DV technical info. | |
7 | * | |
f20dca40 FB |
8 | * Raw DV format |
9 | * Copyright (c) 2002 Fabrice Bellard. | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | */ | |
7458ccbb | 25 | #include <time.h> |
f20dca40 | 26 | #include "avformat.h" |
7458ccbb | 27 | #include "dvdata.h" |
ddaae6a9 RS |
28 | #include "dv.h" |
29 | ||
30 | struct DVDemuxContext { | |
31 | AVFormatContext* fctx; | |
32 | AVStream* vst; | |
33 | AVStream* ast[2]; | |
34 | AVPacket audio_pkt[2]; | |
664f5836 | 35 | uint8_t audio_buf[2][8192]; |
ddaae6a9 | 36 | int ach; |
8066e59f RS |
37 | int frames; |
38 | uint64_t abytes; | |
ddaae6a9 | 39 | }; |
f20dca40 | 40 | |
ddaae6a9 | 41 | struct DVMuxContext { |
7458ccbb RS |
42 | const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */ |
43 | uint8_t frame_buf[144000]; /* frame under contruction */ | |
44 | FifoBuffer audio_data; /* Fifo for storing excessive amounts of PCM */ | |
45 | int frames; /* Number of a current frame */ | |
46 | time_t start_time; /* Start time of recording */ | |
47 | uint8_t aspect; /* Aspect ID 0 - 4:3, 7 - 16:9 */ | |
48 | int has_audio; /* frame under contruction has audio */ | |
49 | int has_video; /* frame under contruction has video */ | |
ddaae6a9 | 50 | }; |
7458ccbb RS |
51 | |
52 | enum dv_section_type { | |
53 | dv_sect_header = 0x1f, | |
54 | dv_sect_subcode = 0x3f, | |
55 | dv_sect_vaux = 0x56, | |
56 | dv_sect_audio = 0x76, | |
57 | dv_sect_video = 0x96, | |
58 | }; | |
59 | ||
60 | enum dv_pack_type { | |
61 | dv_header525 = 0x3f, /* see dv_write_pack for important details on */ | |
62 | dv_header625 = 0xbf, /* these two packs */ | |
63 | dv_timecode = 0x13, | |
64 | dv_audio_source = 0x50, | |
65 | dv_audio_control = 0x51, | |
66 | dv_audio_recdate = 0x52, | |
67 | dv_audio_rectime = 0x53, | |
68 | dv_video_source = 0x60, | |
69 | dv_video_control = 0x61, | |
d156a7b7 | 70 | dv_video_recdate = 0x62, |
7458ccbb RS |
71 | dv_video_rectime = 0x63, |
72 | dv_unknown_pack = 0xff, | |
73 | }; | |
74 | ||
75 | ||
76 | ||
77 | /* | |
78 | * The reason why the following three big ugly looking tables are | |
79 | * here is my lack of DV spec IEC 61834. The tables were basically | |
80 | * constructed to make code that places packs in SSYB, VAUX and | |
81 | * AAUX blocks very simple and table-driven. They conform to the | |
82 | * SMPTE 314M and the output of my personal DV camcorder, neither | |
83 | * of which is sufficient for a reliable DV stream producing. Thus | |
84 | * while code is still in development I'll be gathering input from | |
85 | * people with different DV equipment and modifying the tables to | |
86 | * accommodate all the quirks. Later on, if possible, some of them | |
87 | * will be folded into smaller tables and/or switch-if logic. For | |
88 | * now, my only excuse is -- they don't eat up that much of a space. | |
89 | */ | |
90 | ||
91 | static const int dv_ssyb_packs_dist[12][6] = { | |
92 | { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, | |
93 | { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, | |
94 | { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, | |
95 | { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, | |
96 | { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, | |
97 | { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, | |
98 | { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, | |
99 | { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, | |
100 | { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, | |
101 | { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, | |
102 | { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, | |
103 | { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, | |
104 | }; | |
105 | ||
106 | static const int dv_vaux_packs_dist[12][15] = { | |
107 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
108 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
109 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
110 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
111 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
112 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
113 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
114 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
115 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
116 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
117 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
118 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
119 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
120 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
121 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
122 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
123 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
124 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
125 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
126 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
127 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
128 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
129 | { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, | |
130 | 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, | |
131 | }; | |
132 | ||
133 | static const int dv_aaux_packs_dist[12][9] = { | |
134 | { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, | |
135 | { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, | |
136 | { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, | |
137 | { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, | |
138 | { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, | |
139 | { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, | |
140 | { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, | |
141 | { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, | |
142 | { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, | |
143 | { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, | |
144 | { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, | |
145 | { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, | |
146 | }; | |
147 | ||
148 | static inline uint16_t dv_audio_12to16(uint16_t sample) | |
149 | { | |
150 | uint16_t shift, result; | |
151 | ||
152 | sample = (sample < 0x800) ? sample : sample | 0xf000; | |
153 | shift = (sample & 0xf00) >> 8; | |
154 | ||
155 | if (shift < 0x2 || shift > 0xd) { | |
156 | result = sample; | |
157 | } else if (shift < 0x8) { | |
158 | shift--; | |
159 | result = (sample - (256 * shift)) << shift; | |
160 | } else { | |
161 | shift = 0xe - shift; | |
162 | result = ((sample + ((256 * shift) + 1)) << shift) - 1; | |
163 | } | |
164 | ||
165 | return result; | |
166 | } | |
167 | ||
168 | static int dv_audio_frame_size(const DVprofile* sys, int frame) | |
f20dca40 | 169 | { |
7458ccbb RS |
170 | return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/ |
171 | sizeof(sys->audio_samples_dist[0]))]; | |
172 | } | |
f20dca40 | 173 | |
7458ccbb RS |
174 | static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf) |
175 | { | |
176 | struct tm tc; | |
177 | time_t ct; | |
178 | int ltc_frame; | |
425ed6e2 | 179 | |
7458ccbb RS |
180 | buf[0] = (uint8_t)pack_id; |
181 | switch (pack_id) { | |
182 | case dv_header525: /* I can't imagine why these two weren't defined as real */ | |
183 | case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */ | |
184 | buf[1] = 0xf8 | /* reserved -- always 1 */ | |
185 | (0 & 0x07); /* APT: Track application ID */ | |
186 | buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */ | |
187 | (0x0f << 3) | /* reserved -- always 1 */ | |
188 | (0 & 0x07); /* AP1: Audio application ID */ | |
189 | buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */ | |
190 | (0x0f << 3) | /* reserved -- always 1 */ | |
191 | (0 & 0x07); /* AP2: Video application ID */ | |
192 | buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */ | |
193 | (0x0f << 3) | /* reserved -- always 1 */ | |
194 | (0 & 0x07); /* AP3: Subcode application ID */ | |
195 | break; | |
196 | case dv_timecode: | |
197 | ct = (time_t)(c->frames / ((float)c->sys->frame_rate / | |
198 | (float)c->sys->frame_rate_base)); | |
0c9fc6e1 | 199 | brktimegm(ct, &tc); |
7458ccbb RS |
200 | /* |
201 | * LTC drop-frame frame counter drops two frames (0 and 1) every | |
202 | * minute, unless it is exactly divisible by 10 | |
203 | */ | |
204 | ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor; | |
205 | buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */ | |
206 | (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */ | |
207 | ((ltc_frame / 10) << 4) | /* Tens of frames */ | |
208 | (ltc_frame % 10); /* Units of frames */ | |
209 | buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */ | |
210 | ((tc.tm_sec / 10) << 4) | /* Tens of seconds */ | |
211 | (tc.tm_sec % 10); /* Units of seconds */ | |
212 | buf[3] = (1 << 7) | /* Binary group flag BGF0 */ | |
213 | ((tc.tm_min / 10) << 4) | /* Tens of minutes */ | |
214 | (tc.tm_min % 10); /* Units of minutes */ | |
215 | buf[4] = (1 << 7) | /* Binary group flag BGF2 */ | |
216 | (1 << 6) | /* Binary group flag BGF1 */ | |
217 | ((tc.tm_hour / 10) << 4) | /* Tens of hours */ | |
218 | (tc.tm_hour % 10); /* Units of hours */ | |
219 | break; | |
220 | case dv_audio_source: /* AAUX source pack */ | |
221 | buf[1] = (0 << 7) | /* locked mode */ | |
222 | (1 << 6) | /* reserved -- always 1 */ | |
223 | (dv_audio_frame_size(c->sys, c->frames) - | |
224 | c->sys->audio_min_samples[0]); | |
225 | /* # of samples */ | |
226 | buf[2] = (0 << 7) | /* multi-stereo */ | |
227 | (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */ | |
228 | (0 << 4) | /* pair bit: 0 -- one pair of channels */ | |
229 | 0; /* audio mode */ | |
230 | buf[3] = (1 << 7) | /* res */ | |
231 | (1 << 6) | /* multi-language flag */ | |
232 | (c->sys->dsf << 5) | /* system: 60fields/50fields */ | |
233 | 0; /* definition: 0 -- SD (525/625) */ | |
234 | buf[4] = (1 << 7) | /* emphasis: 1 -- off */ | |
235 | (0 << 6) | /* emphasis time constant: 0 -- reserved */ | |
236 | (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */ | |
237 | 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */ | |
238 | break; | |
239 | case dv_audio_control: | |
240 | buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ | |
241 | (1 << 4) | /* input source: 1 -- digital input */ | |
242 | (3 << 2) | /* compression: 3 -- no information */ | |
243 | 0; /* misc. info/SMPTE emphasis off */ | |
244 | buf[2] = (1 << 7) | /* recording start point: 1 -- no */ | |
245 | (1 << 6) | /* recording end point: 1 -- no */ | |
246 | (1 << 3) | /* recording mode: 1 -- original */ | |
247 | 7; | |
248 | buf[3] = (1 << 7) | /* direction: 1 -- forward */ | |
249 | 0x20; /* speed */ | |
250 | buf[4] = (1 << 7) | /* reserved -- always 1 */ | |
251 | 0x7f; /* genre category */ | |
252 | break; | |
253 | case dv_audio_recdate: | |
d156a7b7 | 254 | case dv_video_recdate: /* VAUX recording date */ |
7458ccbb RS |
255 | ct = c->start_time + (time_t)(c->frames / |
256 | ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base)); | |
0c9fc6e1 | 257 | brktimegm(ct, &tc); |
7458ccbb RS |
258 | buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */ |
259 | /* 0xff is very likely to be "unknown" */ | |
260 | buf[2] = (3 << 6) | /* reserved -- always 1 */ | |
261 | ((tc.tm_mday / 10) << 4) | /* Tens of day */ | |
262 | (tc.tm_mday % 10); /* Units of day */ | |
263 | buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */ | |
0c9fc6e1 RS |
264 | ((tc.tm_mon / 10) << 4) | /* Tens of month */ |
265 | (tc.tm_mon % 10); /* Units of month */ | |
7458ccbb RS |
266 | buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */ |
267 | (tc.tm_year % 10); /* Units of year */ | |
268 | break; | |
269 | case dv_audio_rectime: /* AAUX recording time */ | |
270 | case dv_video_rectime: /* VAUX recording time */ | |
271 | ct = c->start_time + (time_t)(c->frames / | |
272 | ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base)); | |
0c9fc6e1 | 273 | brktimegm(ct, &tc); |
7458ccbb RS |
274 | buf[1] = (3 << 6) | /* reserved -- always 1 */ |
275 | 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */ | |
276 | buf[2] = (1 << 7) | /* reserved -- always 1 */ | |
277 | ((tc.tm_sec / 10) << 4) | /* Tens of seconds */ | |
278 | (tc.tm_sec % 10); /* Units of seconds */ | |
279 | buf[3] = (1 << 7) | /* reserved -- always 1 */ | |
280 | ((tc.tm_min / 10) << 4) | /* Tens of minutes */ | |
281 | (tc.tm_min % 10); /* Units of minutes */ | |
282 | buf[4] = (3 << 6) | /* reserved -- always 1 */ | |
283 | ((tc.tm_hour / 10) << 4) | /* Tens of hours */ | |
284 | (tc.tm_hour % 10); /* Units of hours */ | |
285 | break; | |
286 | case dv_video_source: | |
287 | buf[1] = 0xff; /* reserved -- always 1 */ | |
288 | buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */ | |
289 | (1 << 6) | /* following CLF is valid - 0, invalid - 1 */ | |
290 | (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */ | |
291 | 0xf; /* reserved -- always 1 */ | |
292 | buf[3] = (3 << 6) | /* reserved -- always 1 */ | |
293 | (c->sys->dsf << 5) | /* system: 60fields/50fields */ | |
294 | 0; /* signal type video compression */ | |
295 | buf[4] = 0xff; /* VISC: 0xff -- no information */ | |
296 | break; | |
297 | case dv_video_control: | |
298 | buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */ | |
299 | 0x3f; /* reserved -- always 1 */ | |
300 | buf[2] = 0xc8 | /* reserved -- always b11001xxx */ | |
301 | c->aspect; | |
302 | buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */ | |
303 | (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */ | |
304 | (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */ | |
305 | (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */ | |
306 | 0xc; /* reserved -- always b1100 */ | |
307 | buf[4] = 0xff; /* reserved -- always 1 */ | |
308 | break; | |
309 | default: | |
310 | buf[1] = buf[2] = buf[3] = buf[4] = 0xff; | |
311 | } | |
312 | return 5; | |
313 | } | |
f20dca40 | 314 | |
7458ccbb RS |
315 | static inline int dv_write_dif_id(enum dv_section_type t, uint8_t seq_num, |
316 | uint8_t dif_num, uint8_t* buf) | |
317 | { | |
318 | buf[0] = (uint8_t)t; /* Section type */ | |
319 | buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */ | |
320 | (0 << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */ | |
321 | 7; /* reserved -- always 1 */ | |
322 | buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */ | |
323 | return 3; | |
324 | } | |
425ed6e2 | 325 | |
7458ccbb RS |
326 | static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf) |
327 | { | |
328 | if (syb_num == 0 || syb_num == 6) { | |
329 | buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */ | |
330 | (0<<4) | /* AP3 (Subcode application ID) */ | |
331 | 0x0f; /* reserved -- always 1 */ | |
332 | } | |
333 | else if (syb_num == 11) { | |
334 | buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */ | |
335 | 0x7f; /* reserved -- always 1 */ | |
336 | } | |
337 | else { | |
338 | buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */ | |
339 | (0<<4) | /* APT (Track application ID) */ | |
340 | 0x0f; /* reserved -- always 1 */ | |
341 | } | |
342 | buf[1] = 0xf0 | /* reserved -- always 1 */ | |
343 | (syb_num & 0x0f); /* SSYB number 0 - 11 */ | |
344 | buf[2] = 0xff; /* reserved -- always 1 */ | |
345 | return 3; | |
346 | } | |
347 | ||
348 | static void dv_format_frame(DVMuxContext *c, uint8_t* buf) | |
349 | { | |
350 | int i, j, k; | |
351 | ||
352 | for (i = 0; i < c->sys->difseg_size; i++) { | |
353 | memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */ | |
354 | ||
355 | /* DV header: 1DIF */ | |
356 | buf += dv_write_dif_id(dv_sect_header, i, 0, buf); | |
357 | buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf); | |
358 | buf += 72; /* unused bytes */ | |
359 | ||
360 | /* DV subcode: 2DIFs */ | |
361 | for (j = 0; j < 2; j++) { | |
362 | buf += dv_write_dif_id( dv_sect_subcode, i, j, buf); | |
363 | for (k = 0; k < 6; k++) { | |
364 | buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf); | |
365 | buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf); | |
366 | } | |
367 | buf += 29; /* unused bytes */ | |
368 | } | |
369 | ||
370 | /* DV VAUX: 3DIFs */ | |
371 | for (j = 0; j < 3; j++) { | |
372 | buf += dv_write_dif_id(dv_sect_vaux, i, j, buf); | |
373 | for (k = 0; k < 15 ; k++) | |
374 | buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf); | |
375 | buf += 2; /* unused bytes */ | |
376 | } | |
377 | ||
378 | /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */ | |
379 | for (j = 0; j < 135; j++) { | |
380 | if (j%15 == 0) { | |
96319c39 | 381 | memset(buf, 0xff, 80); |
7458ccbb | 382 | buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf); |
96319c39 | 383 | buf += 77; /* audio control & shuffled PCM audio */ |
7458ccbb RS |
384 | } |
385 | buf += dv_write_dif_id(dv_sect_video, i, j, buf); | |
386 | buf += 77; /* 1 video macro block: 1 bytes control | |
387 | 4 * 14 bytes Y 8x8 data | |
388 | 10 bytes Cr 8x8 data | |
389 | 10 bytes Cb 8x8 data */ | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | static void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, uint8_t* frame_ptr) | |
395 | { | |
3ea1ce21 RS |
396 | int i, j, d, of, size; |
397 | size = 4 * dv_audio_frame_size(c->sys, c->frames); | |
7458ccbb RS |
398 | for (i = 0; i < c->sys->difseg_size; i++) { |
399 | frame_ptr += 6 * 80; /* skip DIF segment header */ | |
400 | for (j = 0; j < 9; j++) { | |
96319c39 | 401 | dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]); |
7458ccbb RS |
402 | for (d = 8; d < 80; d+=2) { |
403 | of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride; | |
3ea1ce21 RS |
404 | if (of*2 >= size) |
405 | continue; | |
406 | ||
7458ccbb RS |
407 | frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit |
408 | frame_ptr[d+1] = pcm[of*2]; // that DV is a big endian PCM | |
409 | } | |
410 | frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ | |
411 | } | |
412 | } | |
413 | } | |
414 | ||
415 | static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr) | |
416 | { | |
417 | int i, j; | |
418 | int ptr = 0; | |
419 | ||
420 | for (i = 0; i < c->sys->difseg_size; i++) { | |
421 | ptr += 6 * 80; /* skip DIF segment header */ | |
422 | for (j = 0; j < 135; j++) { | |
423 | if (j%15 == 0) | |
424 | ptr += 80; /* skip Audio DIF */ | |
425 | ptr += 3; | |
426 | memcpy(frame_ptr + ptr, video_data + ptr, 77); | |
427 | ptr += 77; | |
428 | } | |
429 | } | |
430 | } | |
431 | ||
432 | /* | |
433 | * This is the dumbest implementation of all -- it simply looks at | |
434 | * a fixed offset and if pack isn't there -- fails. We might want | |
435 | * to have a fallback mechanism for complete search of missing packs. | |
436 | */ | |
437 | static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t) | |
438 | { | |
439 | int offs; | |
440 | ||
441 | switch (t) { | |
442 | case dv_audio_source: | |
443 | offs = (80*6 + 80*16*3 + 3); | |
444 | break; | |
445 | case dv_audio_control: | |
446 | offs = (80*6 + 80*16*4 + 3); | |
447 | break; | |
448 | case dv_video_control: | |
449 | offs = (80*5 + 48 + 5); | |
450 | break; | |
451 | default: | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | return (frame[offs] == t ? &frame[offs] : NULL); | |
456 | } | |
457 | ||
458 | /* | |
459 | * There's a couple of assumptions being made here: | |
460 | * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples. | |
461 | * We can pass them upwards when ffmpeg will be ready to deal with them. | |
462 | * 2. We don't do software emphasis. | |
463 | * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples | |
464 | * are converted into 16bit linear ones. | |
465 | */ | |
ddaae6a9 | 466 | static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2) |
7458ccbb | 467 | { |
ddaae6a9 | 468 | int size, i, j, d, of, smpls, freq, quant, half_ch; |
7458ccbb RS |
469 | uint16_t lc, rc; |
470 | const DVprofile* sys; | |
471 | const uint8_t* as_pack; | |
472 | ||
473 | as_pack = dv_extract_pack(frame, dv_audio_source); | |
474 | if (!as_pack) /* No audio ? */ | |
475 | return 0; | |
476 | ||
477 | sys = dv_frame_profile(frame); | |
478 | smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ | |
479 | freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ | |
480 | quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ | |
481 | ||
482 | if (quant > 1) | |
483 | return -1; /* Unsupported quantization */ | |
484 | ||
485 | size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */ | |
ddaae6a9 | 486 | half_ch = sys->difseg_size/2; |
7458ccbb RS |
487 | |
488 | /* for each DIF segment */ | |
489 | for (i = 0; i < sys->difseg_size; i++) { | |
490 | frame += 6 * 80; /* skip DIF segment header */ | |
ddaae6a9 RS |
491 | if (quant == 1 && i == half_ch) { |
492 | if (!pcm2) | |
493 | break; | |
494 | else | |
495 | pcm = pcm2; | |
496 | } | |
497 | ||
7458ccbb RS |
498 | for (j = 0; j < 9; j++) { |
499 | for (d = 8; d < 80; d += 2) { | |
500 | if (quant == 0) { /* 16bit quantization */ | |
501 | of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride; | |
502 | if (of*2 >= size) | |
503 | continue; | |
504 | ||
505 | pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit | |
506 | pcm[of*2+1] = frame[d]; // that DV is a big endian PCM | |
507 | if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00) | |
508 | pcm[of*2+1] = 0; | |
509 | } else { /* 12bit quantization */ | |
7458ccbb RS |
510 | lc = ((uint16_t)frame[d] << 4) | |
511 | ((uint16_t)frame[d+2] >> 4); | |
512 | rc = ((uint16_t)frame[d+1] << 4) | | |
513 | ((uint16_t)frame[d+2] & 0x0f); | |
514 | lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); | |
515 | rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); | |
516 | ||
ddaae6a9 | 517 | of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride; |
7458ccbb RS |
518 | if (of*2 >= size) |
519 | continue; | |
520 | ||
521 | pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit | |
522 | pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM | |
ddaae6a9 | 523 | of = sys->audio_shuffle[i%half_ch+half_ch][j] + |
7458ccbb RS |
524 | (d - 8)/3 * sys->audio_stride; |
525 | pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit | |
526 | pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM | |
527 | ++d; | |
528 | } | |
529 | } | |
530 | ||
531 | frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ | |
532 | } | |
533 | } | |
534 | ||
7458ccbb RS |
535 | return size; |
536 | } | |
537 | ||
ddaae6a9 | 538 | static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame) |
7458ccbb RS |
539 | { |
540 | const uint8_t* as_pack; | |
541 | const DVprofile* sys; | |
50883632 | 542 | int freq, smpls, quant, i, ach; |
7458ccbb RS |
543 | |
544 | sys = dv_frame_profile(frame); | |
545 | as_pack = dv_extract_pack(frame, dv_audio_source); | |
ddaae6a9 RS |
546 | if (!as_pack || !sys) { /* No audio ? */ |
547 | c->ach = 0; | |
664f5836 | 548 | return 0; |
ddaae6a9 | 549 | } |
7458ccbb RS |
550 | |
551 | smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ | |
552 | freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ | |
ddaae6a9 | 553 | quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ |
50883632 RS |
554 | ach = (quant && freq == 2) ? 2 : 1; |
555 | ||
556 | /* Dynamic handling of the audio streams in DV */ | |
557 | for (i=0; i<ach; i++) { | |
558 | if (!c->ast[i]) { | |
559 | c->ast[i] = av_new_stream(c->fctx, 0); | |
560 | if (!c->ast[i]) | |
561 | break; | |
562 | av_set_pts_info(c->ast[i], 64, 1, 30000); | |
01f4895c MN |
563 | c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO; |
564 | c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE; | |
664f5836 RS |
565 | |
566 | av_init_packet(&c->audio_pkt[i]); | |
567 | c->audio_pkt[i].size = 0; | |
568 | c->audio_pkt[i].data = c->audio_buf[i]; | |
569 | c->audio_pkt[i].stream_index = c->ast[i]->index; | |
570 | c->audio_pkt[i].flags |= PKT_FLAG_KEY; | |
50883632 | 571 | } |
01f4895c MN |
572 | c->ast[i]->codec->sample_rate = dv_audio_frequency[freq]; |
573 | c->ast[i]->codec->channels = 2; | |
574 | c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16; | |
50883632 | 575 | c->ast[i]->start_time = 0; |
ddaae6a9 | 576 | } |
50883632 | 577 | c->ach = i; |
ddaae6a9 | 578 | |
7458ccbb RS |
579 | return (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */; |
580 | } | |
581 | ||
ddaae6a9 | 582 | static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame) |
7458ccbb RS |
583 | { |
584 | const DVprofile* sys; | |
585 | const uint8_t* vsc_pack; | |
ddaae6a9 RS |
586 | AVCodecContext* avctx; |
587 | int apt, is16_9; | |
7458ccbb RS |
588 | int size = 0; |
589 | ||
590 | sys = dv_frame_profile(frame); | |
591 | if (sys) { | |
01f4895c | 592 | avctx = c->vst->codec; |
ddaae6a9 | 593 | |
eb1bde3e | 594 | av_set_pts_info(c->vst, 64, sys->frame_rate_base, sys->frame_rate); |
c0df9d75 | 595 | avctx->time_base= (AVRational){sys->frame_rate_base, sys->frame_rate}; |
4325ffd0 MN |
596 | if(!avctx->width){ |
597 | avctx->width = sys->width; | |
598 | avctx->height = sys->height; | |
599 | } | |
7458ccbb | 600 | avctx->pix_fmt = sys->pix_fmt; |
ddaae6a9 | 601 | |
12cccabd | 602 | /* finding out SAR is a little bit messy */ |
ddaae6a9 RS |
603 | vsc_pack = dv_extract_pack(frame, dv_video_control); |
604 | apt = frame[4] & 0x07; | |
12cccabd RS |
605 | is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || |
606 | (!apt && (vsc_pack[2] & 0x07) == 0x07))); | |
ddaae6a9 RS |
607 | avctx->sample_aspect_ratio = sys->sar[is16_9]; |
608 | ||
7458ccbb RS |
609 | size = sys->frame_size; |
610 | } | |
611 | return size; | |
612 | } | |
613 | ||
614 | /* | |
615 | * The following 6 functions constitute our interface to the world | |
616 | */ | |
617 | ||
618 | int dv_assemble_frame(DVMuxContext *c, AVStream* st, | |
619 | const uint8_t* data, int data_size, uint8_t** frame) | |
620 | { | |
621 | uint8_t pcm[8192]; | |
622 | int fsize, reqasize; | |
623 | ||
624 | *frame = &c->frame_buf[0]; | |
625 | if (c->has_audio && c->has_video) { /* must be a stale frame */ | |
626 | dv_format_frame(c, *frame); | |
627 | c->frames++; | |
96319c39 RS |
628 | if (c->has_audio > 0) |
629 | c->has_audio = 0; | |
630 | c->has_video = 0; | |
7458ccbb RS |
631 | } |
632 | ||
01f4895c | 633 | if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
7458ccbb RS |
634 | /* FIXME: we have to have more sensible approach than this one */ |
635 | if (c->has_video) | |
01f4895c | 636 | av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames); |
7458ccbb RS |
637 | |
638 | dv_inject_video(c, data, *frame); | |
639 | c->has_video = 1; | |
640 | data_size = 0; | |
96319c39 RS |
641 | if (c->has_audio < 0) |
642 | goto out; | |
7458ccbb RS |
643 | } |
644 | ||
645 | reqasize = 4 * dv_audio_frame_size(c->sys, c->frames); | |
646 | fsize = fifo_size(&c->audio_data, c->audio_data.rptr); | |
01f4895c | 647 | if (st->codec->codec_type == CODEC_TYPE_AUDIO || (c->has_video && fsize >= reqasize)) { |
7458ccbb RS |
648 | if (fsize + data_size >= reqasize && !c->has_audio) { |
649 | if (fsize >= reqasize) { | |
650 | fifo_read(&c->audio_data, &pcm[0], reqasize, &c->audio_data.rptr); | |
651 | } else { | |
652 | fifo_read(&c->audio_data, &pcm[0], fsize, &c->audio_data.rptr); | |
653 | memcpy(&pcm[fsize], &data[0], reqasize - fsize); | |
654 | data += reqasize - fsize; | |
655 | data_size -= reqasize - fsize; | |
656 | } | |
657 | dv_inject_audio(c, &pcm[0], *frame); | |
658 | c->has_audio = 1; | |
659 | } | |
660 | ||
661 | /* FIXME: we have to have more sensible approach than this one */ | |
662 | if (fifo_size(&c->audio_data, c->audio_data.rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) | |
01f4895c | 663 | av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames); |
7458ccbb RS |
664 | fifo_write(&c->audio_data, (uint8_t *)data, data_size, &c->audio_data.wptr); |
665 | } | |
666 | ||
96319c39 | 667 | out: |
7458ccbb RS |
668 | return (c->has_audio && c->has_video) ? c->sys->frame_size : 0; |
669 | } | |
670 | ||
671 | DVMuxContext* dv_init_mux(AVFormatContext* s) | |
672 | { | |
673 | DVMuxContext *c; | |
96319c39 RS |
674 | AVStream *vst = NULL; |
675 | AVStream *ast = NULL; | |
676 | int i; | |
677 | ||
678 | if (s->nb_streams > 2) | |
679 | return NULL; | |
7458ccbb RS |
680 | |
681 | c = av_mallocz(sizeof(DVMuxContext)); | |
682 | if (!c) | |
683 | return NULL; | |
684 | ||
7458ccbb | 685 | /* We have to sort out where audio and where video stream is */ |
96319c39 | 686 | for (i=0; i<s->nb_streams; i++) { |
01f4895c | 687 | switch (s->streams[i]->codec->codec_type) { |
96319c39 RS |
688 | case CODEC_TYPE_VIDEO: |
689 | vst = s->streams[i]; | |
690 | break; | |
691 | case CODEC_TYPE_AUDIO: | |
692 | ast = s->streams[i]; | |
693 | break; | |
694 | default: | |
695 | goto bail_out; | |
696 | } | |
7458ccbb | 697 | } |
96319c39 | 698 | |
7458ccbb | 699 | /* Some checks -- DV format is very picky about its incoming streams */ |
01f4895c | 700 | if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO) |
96319c39 | 701 | goto bail_out; |
01f4895c MN |
702 | if (ast && (ast->codec->codec_id != CODEC_ID_PCM_S16LE || |
703 | ast->codec->sample_rate != 48000 || | |
704 | ast->codec->channels != 2)) | |
96319c39 | 705 | goto bail_out; |
01f4895c | 706 | c->sys = dv_codec_profile(vst->codec); |
7458ccbb RS |
707 | if (!c->sys) |
708 | goto bail_out; | |
709 | ||
710 | /* Ok, everything seems to be in working order */ | |
711 | c->frames = 0; | |
96319c39 RS |
712 | c->has_audio = ast ? 0 : -1; |
713 | c->has_video = 0; | |
4568325a | 714 | c->start_time = (time_t)s->timestamp; |
7458ccbb | 715 | c->aspect = 0; /* 4:3 is the default */ |
01f4895c | 716 | if ((int)(av_q2d(vst->codec->sample_aspect_ratio) * vst->codec->width / vst->codec->height * 10) == 17) /* 16:9 */ |
7458ccbb RS |
717 | c->aspect = 0x07; |
718 | ||
96319c39 | 719 | if (ast && fifo_init(&c->audio_data, 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) |
7458ccbb RS |
720 | goto bail_out; |
721 | ||
722 | dv_format_frame(c, &c->frame_buf[0]); | |
723 | ||
724 | return c; | |
725 | ||
726 | bail_out: | |
727 | av_free(c); | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | void dv_delete_mux(DVMuxContext *c) | |
732 | { | |
733 | fifo_free(&c->audio_data); | |
7458ccbb RS |
734 | } |
735 | ||
ddaae6a9 | 736 | DVDemuxContext* dv_init_demux(AVFormatContext *s) |
7458ccbb RS |
737 | { |
738 | DVDemuxContext *c; | |
739 | ||
740 | c = av_mallocz(sizeof(DVDemuxContext)); | |
741 | if (!c) | |
742 | return NULL; | |
743 | ||
ddaae6a9 | 744 | c->vst = av_new_stream(s, 0); |
50883632 RS |
745 | if (!c->vst) { |
746 | av_free(c); | |
747 | return NULL; | |
748 | } | |
9ee91c2f | 749 | |
ddaae6a9 | 750 | c->fctx = s; |
50883632 | 751 | c->ast[0] = c->ast[1] = NULL; |
ddaae6a9 | 752 | c->ach = 0; |
8066e59f RS |
753 | c->frames = 0; |
754 | c->abytes = 0; | |
664f5836 | 755 | |
01f4895c MN |
756 | c->vst->codec->codec_type = CODEC_TYPE_VIDEO; |
757 | c->vst->codec->codec_id = CODEC_ID_DVVIDEO; | |
758 | c->vst->codec->bit_rate = 25000000; | |
c4e55470 | 759 | c->vst->start_time = 0; |
7458ccbb | 760 | |
7458ccbb | 761 | return c; |
f20dca40 FB |
762 | } |
763 | ||
7458ccbb | 764 | int dv_get_packet(DVDemuxContext *c, AVPacket *pkt) |
f20dca40 | 765 | { |
7458ccbb | 766 | int size = -1; |
ddaae6a9 RS |
767 | int i; |
768 | ||
769 | for (i=0; i<c->ach; i++) { | |
770 | if (c->ast[i] && c->audio_pkt[i].size) { | |
771 | *pkt = c->audio_pkt[i]; | |
772 | c->audio_pkt[i].size = 0; | |
773 | size = pkt->size; | |
774 | break; | |
775 | } | |
425ed6e2 | 776 | } |
7458ccbb RS |
777 | |
778 | return size; | |
779 | } | |
780 | ||
781 | int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, | |
782 | uint8_t* buf, int buf_size) | |
783 | { | |
ddaae6a9 | 784 | int size, i; |
8066e59f | 785 | const DVprofile* sys = dv_frame_profile(buf); |
7458ccbb | 786 | |
8066e59f | 787 | if (buf_size < 4 || buf_size < sys->frame_size) |
7458ccbb RS |
788 | return -1; /* Broken frame, or not enough data */ |
789 | ||
790 | /* Queueing audio packet */ | |
791 | /* FIXME: in case of no audio/bad audio we have to do something */ | |
ddaae6a9 | 792 | size = dv_extract_audio_info(c, buf); |
ddaae6a9 | 793 | for (i=0; i<c->ach; i++) { |
664f5836 | 794 | c->audio_pkt[i].size = size; |
01f4895c | 795 | c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate; |
ddaae6a9 | 796 | } |
664f5836 | 797 | dv_extract_audio(buf, c->audio_buf[0], c->audio_buf[1]); |
8066e59f | 798 | c->abytes += size; |
f20dca40 | 799 | |
7458ccbb | 800 | /* Now it's time to return video packet */ |
ddaae6a9 | 801 | size = dv_extract_video_info(c, buf); |
ec820113 | 802 | av_init_packet(pkt); |
7458ccbb RS |
803 | pkt->data = buf; |
804 | pkt->size = size; | |
e738cee9 | 805 | pkt->flags |= PKT_FLAG_KEY; |
7458ccbb | 806 | pkt->stream_index = c->vst->id; |
eb1bde3e | 807 | pkt->pts = c->frames; |
8066e59f RS |
808 | |
809 | c->frames++; | |
ddaae6a9 | 810 | |
7458ccbb RS |
811 | return size; |
812 | } | |
813 | ||
83582de4 MN |
814 | static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c, |
815 | int64_t timestamp, int flags) | |
8066e59f | 816 | { |
0bf4e2b8 | 817 | // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk) |
01f4895c | 818 | const DVprofile* sys = dv_codec_profile(c->vst->codec); |
eb1bde3e | 819 | int64_t offset; |
a965c478 | 820 | int64_t size = url_fsize(&s->pb); |
83582de4 | 821 | int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size; |
0bf4e2b8 | 822 | |
eb1bde3e | 823 | offset = sys->frame_size * timestamp; |
83582de4 MN |
824 | |
825 | if (offset > max_offset) offset = max_offset; | |
826 | else if (offset < 0) offset = 0; | |
8066e59f | 827 | |
0bf4e2b8 | 828 | return offset; |
8066e59f RS |
829 | } |
830 | ||
664f5836 RS |
831 | void dv_flush_audio_packets(DVDemuxContext *c) |
832 | { | |
833 | c->audio_pkt[0].size = c->audio_pkt[1].size = 0; | |
834 | } | |
835 | ||
7458ccbb RS |
836 | /************************************************************ |
837 | * Implementation of the easiest DV storage of all -- raw DV. | |
838 | ************************************************************/ | |
839 | ||
840 | typedef struct RawDVContext { | |
ddaae6a9 RS |
841 | uint8_t buf[144000]; |
842 | DVDemuxContext* dv_demux; | |
7458ccbb RS |
843 | } RawDVContext; |
844 | ||
845 | static int dv_read_header(AVFormatContext *s, | |
846 | AVFormatParameters *ap) | |
847 | { | |
848 | RawDVContext *c = s->priv_data; | |
c4e55470 | 849 | const DVprofile* sys; |
ddaae6a9 | 850 | c->dv_demux = dv_init_demux(s); |
c4e55470 RS |
851 | if (!c->dv_demux) |
852 | return -1; | |
853 | ||
854 | if (get_buffer(&s->pb, c->buf, 4) <= 0 || url_fseek(&s->pb, -4, SEEK_CUR) < 0) | |
855 | return AVERROR_IO; | |
856 | ||
857 | sys = dv_frame_profile(c->buf); | |
858 | s->bit_rate = av_rescale(sys->frame_size * 8, sys->frame_rate, sys->frame_rate_base); | |
7458ccbb | 859 | |
c4e55470 | 860 | return 0; |
7458ccbb RS |
861 | } |
862 | ||
863 | ||
864 | static int dv_read_packet(AVFormatContext *s, AVPacket *pkt) | |
865 | { | |
866 | int size; | |
867 | RawDVContext *c = s->priv_data; | |
868 | ||
869 | size = dv_get_packet(c->dv_demux, pkt); | |
870 | ||
871 | if (size < 0) { | |
872 | if (get_buffer(&s->pb, c->buf, 4) <= 0) | |
0bd586c5 | 873 | return AVERROR_IO; |
7458ccbb RS |
874 | |
875 | size = dv_frame_profile(c->buf)->frame_size; | |
876 | if (get_buffer(&s->pb, c->buf + 4, size - 4) <= 0) | |
0bd586c5 | 877 | return AVERROR_IO; |
7458ccbb RS |
878 | |
879 | size = dv_produce_packet(c->dv_demux, pkt, c->buf, size); | |
880 | } | |
881 | ||
882 | return size; | |
f20dca40 FB |
883 | } |
884 | ||
83582de4 MN |
885 | static int dv_read_seek(AVFormatContext *s, int stream_index, |
886 | int64_t timestamp, int flags) | |
8066e59f | 887 | { |
83582de4 MN |
888 | RawDVContext *r = s->priv_data; |
889 | DVDemuxContext *c = r->dv_demux; | |
83582de4 | 890 | int64_t offset= dv_frame_offset(s, c, timestamp, flags); |
01f4895c | 891 | const DVprofile* sys = dv_codec_profile(c->vst->codec); |
83582de4 MN |
892 | |
893 | c->frames= offset / sys->frame_size; | |
664f5836 RS |
894 | if (c->ach) |
895 | c->abytes= av_rescale(c->frames, | |
01f4895c | 896 | c->ast[0]->codec->bit_rate * (int64_t)sys->frame_rate_base, |
83582de4 | 897 | 8*sys->frame_rate); |
664f5836 RS |
898 | |
899 | dv_flush_audio_packets(c); | |
83582de4 | 900 | return url_fseek(&s->pb, offset, SEEK_SET); |
8066e59f RS |
901 | } |
902 | ||
c18a2692 | 903 | static int dv_read_close(AVFormatContext *s) |
f20dca40 | 904 | { |
7458ccbb RS |
905 | RawDVContext *c = s->priv_data; |
906 | av_free(c->dv_demux); | |
f20dca40 FB |
907 | return 0; |
908 | } | |
909 | ||
7458ccbb | 910 | static int dv_write_header(AVFormatContext *s) |
f20dca40 | 911 | { |
7458ccbb RS |
912 | s->priv_data = dv_init_mux(s); |
913 | if (!s->priv_data) { | |
bc874dae | 914 | av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n" |
118e91f3 RS |
915 | "Make sure that you supply exactly two streams:\n" |
916 | " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"); | |
917 | return -1; | |
918 | } | |
f20dca40 FB |
919 | return 0; |
920 | } | |
921 | ||
e928649b | 922 | static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt) |
f20dca40 | 923 | { |
7458ccbb RS |
924 | uint8_t* frame; |
925 | int fsize; | |
118e91f3 | 926 | |
e928649b MN |
927 | fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index], |
928 | pkt->data, pkt->size, &frame); | |
7458ccbb RS |
929 | if (fsize > 0) { |
930 | put_buffer(&s->pb, frame, fsize); | |
118e91f3 | 931 | put_flush_packet(&s->pb); |
7458ccbb | 932 | } |
ec26a167 | 933 | return 0; |
f20dca40 FB |
934 | } |
935 | ||
118e91f3 RS |
936 | /* |
937 | * We might end up with some extra A/V data without matching counterpart. | |
938 | * E.g. video data without enough audio to write the complete frame. | |
939 | * Currently we simply drop the last frame. I don't know whether this | |
940 | * is the best strategy of all | |
941 | */ | |
7458ccbb | 942 | static int dv_write_trailer(struct AVFormatContext *s) |
f20dca40 | 943 | { |
7458ccbb | 944 | dv_delete_mux((DVMuxContext *)s->priv_data); |
f20dca40 FB |
945 | return 0; |
946 | } | |
947 | ||
118e91f3 RS |
948 | static AVInputFormat dv_iformat = { |
949 | "dv", | |
950 | "DV video format", | |
7458ccbb | 951 | sizeof(RawDVContext), |
118e91f3 RS |
952 | NULL, |
953 | dv_read_header, | |
954 | dv_read_packet, | |
955 | dv_read_close, | |
8066e59f | 956 | dv_read_seek, |
490c735d | 957 | .extensions = "dv,dif", |
118e91f3 RS |
958 | }; |
959 | ||
7458ccbb | 960 | static AVOutputFormat dv_oformat = { |
f20dca40 FB |
961 | "dv", |
962 | "DV video format", | |
963 | NULL, | |
964 | "dv", | |
118e91f3 RS |
965 | sizeof(DVMuxContext), |
966 | CODEC_ID_PCM_S16LE, | |
f20dca40 | 967 | CODEC_ID_DVVIDEO, |
f20dca40 FB |
968 | dv_write_header, |
969 | dv_write_packet, | |
970 | dv_write_trailer, | |
971 | }; | |
f20dca40 | 972 | |
c3775e54 | 973 | int ff_dv_init(void) |
f20dca40 FB |
974 | { |
975 | av_register_input_format(&dv_iformat); | |
3e2937be | 976 | av_register_output_format(&dv_oformat); |
f20dca40 FB |
977 | return 0; |
978 | } |