ffmpeg -i i.dv -i audio_track.mp3 -map 0.0 -map 1.0 \
-vcodec copy -acodec pcm_s16le out.dv
* Preparations for getting rid of DVAUDIO codec, DV stream really
contains PCM audio, so there's no codec needed if we have a
decent demuxer.
* Providing entry points for dv1394 write support.
Originally committed as revision 2174 to svn://svn.ffmpeg.org/ffmpeg/trunk
... | ... |
@@ -13,7 +13,7 @@ PPOBJS= |
13 | 13 |
|
14 | 14 |
# mux and demuxes |
15 | 15 |
OBJS+=mpeg.o mpegts.o mpegtsenc.o ffm.o crc.o img.o raw.o rm.o \ |
16 |
- avienc.o avidec.o wav.o swf.o au.o gif.o mov.o mpjpeg.o dv.o \ |
|
16 |
+ avienc.o avidec.o wav.o swf.o au.o gif.o mov.o mpjpeg.o dvcore.o dv.o \ |
|
17 | 17 |
yuv4mpeg.o 4xm.o flvenc.o flvdec.o movenc.o |
18 | 18 |
|
19 | 19 |
ifeq ($(CONFIG_RISKY),yes) |
... | ... |
@@ -17,13 +17,11 @@ |
17 | 17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | 18 |
*/ |
19 | 19 |
#include "avformat.h" |
20 |
- |
|
21 |
-#define NTSC_FRAME_SIZE 120000 |
|
22 |
-#define PAL_FRAME_SIZE 144000 |
|
20 |
+#include "dvcore.h" |
|
23 | 21 |
|
24 | 22 |
typedef struct DVDemuxContext { |
25 | 23 |
int is_audio; |
26 |
- uint8_t buf[PAL_FRAME_SIZE]; |
|
24 |
+ uint8_t buf[144000]; |
|
27 | 25 |
int size; |
28 | 26 |
} DVDemuxContext; |
29 | 27 |
|
... | ... |
@@ -39,7 +37,7 @@ static int dv_read_header(AVFormatContext *s, |
39 | 39 |
return AVERROR_NOMEM; |
40 | 40 |
vst->codec.codec_type = CODEC_TYPE_VIDEO; |
41 | 41 |
vst->codec.codec_id = CODEC_ID_DVVIDEO; |
42 |
- |
|
42 |
+ vst->codec.bit_rate = 25000000; |
|
43 | 43 |
|
44 | 44 |
ast = av_new_stream(s, 1); |
45 | 45 |
if (!ast) |
... | ... |
@@ -60,18 +58,14 @@ static void __destruct_pkt(struct AVPacket *pkt) |
60 | 60 |
|
61 | 61 |
static int dv_read_packet(AVFormatContext *s, AVPacket *pkt) |
62 | 62 |
{ |
63 |
- int ret, dsf; |
|
63 |
+ int ret; |
|
64 | 64 |
DVDemuxContext *c = s->priv_data; |
65 | 65 |
|
66 | 66 |
if (!c->is_audio) { |
67 | 67 |
ret = get_buffer(&s->pb, c->buf, 4); |
68 | 68 |
if (ret <= 0) |
69 | 69 |
return -EIO; |
70 |
- dsf = c->buf[3] & 0x80; |
|
71 |
- if (!dsf) |
|
72 |
- c->size = NTSC_FRAME_SIZE; |
|
73 |
- else |
|
74 |
- c->size = PAL_FRAME_SIZE; |
|
70 |
+ c->size = dv_frame_profile(&c->buf[0])->frame_size; |
|
75 | 71 |
|
76 | 72 |
ret = get_buffer(&s->pb, c->buf + 4, c->size - 4); |
77 | 73 |
if (ret <= 0) |
... | ... |
@@ -94,20 +88,16 @@ static int dv_read_close(AVFormatContext *s) |
94 | 94 |
return 0; |
95 | 95 |
} |
96 | 96 |
|
97 |
-static AVInputFormat dv_iformat = { |
|
98 |
- "dv", |
|
99 |
- "DV video format", |
|
100 |
- sizeof(DVDemuxContext), |
|
101 |
- NULL, |
|
102 |
- dv_read_header, |
|
103 |
- dv_read_packet, |
|
104 |
- dv_read_close, |
|
105 |
- .extensions = "dv", |
|
106 |
-}; |
|
107 |
- |
|
108 |
- |
|
109 | 97 |
int dv_write_header(struct AVFormatContext *s) |
110 | 98 |
{ |
99 |
+ DVMuxContext *c = s->priv_data; |
|
100 |
+ |
|
101 |
+ if (s->nb_streams != 2 || dv_core_init(c, s->streams) != 0) { |
|
102 |
+ fprintf(stderr, "Can't initialize DV format!\n" |
|
103 |
+ "Make sure that you supply exactly two streams:\n" |
|
104 |
+ " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"); |
|
105 |
+ return -1; |
|
106 |
+ } |
|
111 | 107 |
return 0; |
112 | 108 |
} |
113 | 109 |
|
... | ... |
@@ -115,24 +105,52 @@ int dv_write_packet(struct AVFormatContext *s, |
115 | 115 |
int stream_index, |
116 | 116 |
unsigned char *buf, int size, int force_pts) |
117 | 117 |
{ |
118 |
- put_buffer(&s->pb, buf, size); |
|
119 |
- put_flush_packet(&s->pb); |
|
118 |
+ DVMuxContext *c = s->priv_data; |
|
119 |
+ |
|
120 |
+ if (stream_index == c->vst) |
|
121 |
+ dv_assemble_frame(c, buf, NULL, 0); |
|
122 |
+ else |
|
123 |
+ dv_assemble_frame(c, NULL, buf, size); |
|
124 |
+ |
|
125 |
+ if (c->has_audio && c->has_video) { |
|
126 |
+ put_buffer(&s->pb, &c->frame_buf[0], c->sys->frame_size); |
|
127 |
+ put_flush_packet(&s->pb); |
|
128 |
+ } |
|
129 |
+ |
|
120 | 130 |
return 0; |
121 | 131 |
} |
122 | 132 |
|
133 |
+/* |
|
134 |
+ * We might end up with some extra A/V data without matching counterpart. |
|
135 |
+ * E.g. video data without enough audio to write the complete frame. |
|
136 |
+ * Currently we simply drop the last frame. I don't know whether this |
|
137 |
+ * is the best strategy of all |
|
138 |
+ */ |
|
123 | 139 |
int dv_write_trailer(struct AVFormatContext *s) |
124 | 140 |
{ |
141 |
+ dv_core_delete((DVMuxContext *)s->priv_data); |
|
125 | 142 |
return 0; |
126 | 143 |
} |
127 | 144 |
|
145 |
+static AVInputFormat dv_iformat = { |
|
146 |
+ "dv", |
|
147 |
+ "DV video format", |
|
148 |
+ sizeof(DVDemuxContext), |
|
149 |
+ NULL, |
|
150 |
+ dv_read_header, |
|
151 |
+ dv_read_packet, |
|
152 |
+ dv_read_close, |
|
153 |
+ .extensions = "dv", |
|
154 |
+}; |
|
155 |
+ |
|
128 | 156 |
AVOutputFormat dv_oformat = { |
129 | 157 |
"dv", |
130 | 158 |
"DV video format", |
131 | 159 |
NULL, |
132 | 160 |
"dv", |
133 |
- 0, |
|
161 |
+ sizeof(DVMuxContext), |
|
162 |
+ CODEC_ID_PCM_S16LE, |
|
134 | 163 |
CODEC_ID_DVVIDEO, |
135 |
- CODEC_ID_DVAUDIO, |
|
136 | 164 |
dv_write_header, |
137 | 165 |
dv_write_packet, |
138 | 166 |
dv_write_trailer, |
139 | 167 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,668 @@ |
0 |
+/* |
|
1 |
+ * DV format muxer/demuxer |
|
2 |
+ * Copyright (c) 2003 Roman Shaposhnik |
|
3 |
+ * |
|
4 |
+ * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth |
|
5 |
+ * of DV technical info, and SMPTE 314M specification. |
|
6 |
+ * |
|
7 |
+ * This library is free software; you can redistribute it and/or |
|
8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
9 |
+ * License as published by the Free Software Foundation; either |
|
10 |
+ * version 2 of the License, or (at your option) any later version. |
|
11 |
+ * |
|
12 |
+ * This library is distributed in the hope that it will be useful, |
|
13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
+ * Lesser General Public License for more details. |
|
16 |
+ * |
|
17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
18 |
+ * License along with this library; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
20 |
+ */ |
|
21 |
+#include "avformat.h" |
|
22 |
+#include "dvcore.h" |
|
23 |
+ |
|
24 |
+/* |
|
25 |
+ * The reason why the following three big ugly looking tables are |
|
26 |
+ * here is my lack of DV spec IEC 61834. The tables were basically |
|
27 |
+ * constructed to make code that places packs in SSYB, VAUX and |
|
28 |
+ * AAUX blocks very simple and table-driven. They conform to the |
|
29 |
+ * SMPTE 314M and the output of my personal DV camcorder, neither |
|
30 |
+ * of which is sufficient for a reliable DV stream producing. Thus |
|
31 |
+ * while code is still in development I'll be gathering input from |
|
32 |
+ * people with different DV equipment and modifying the tables to |
|
33 |
+ * accommodate all the quirks. Later on, if possible, some of them |
|
34 |
+ * will be folded into smaller tables and/or switch-if logic. For |
|
35 |
+ * now, my only excuse is -- they don't eat up that much of a space. |
|
36 |
+ */ |
|
37 |
+ |
|
38 |
+static const int dv_ssyb_packs_dist[12][6] = { |
|
39 |
+ { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, |
|
40 |
+ { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, |
|
41 |
+ { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, |
|
42 |
+ { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, |
|
43 |
+ { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, |
|
44 |
+ { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, |
|
45 |
+ { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, |
|
46 |
+ { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, |
|
47 |
+ { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, |
|
48 |
+ { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, |
|
49 |
+ { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, |
|
50 |
+ { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, |
|
51 |
+}; |
|
52 |
+ |
|
53 |
+static const int dv_vaux_packs_dist[12][15] = { |
|
54 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
55 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
56 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
57 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
58 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
59 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
60 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
61 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
62 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
63 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
64 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
65 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
66 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
67 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
68 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
69 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
70 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
71 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
72 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
73 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
74 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
75 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
76 |
+ { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
77 |
+ 0x60, 0x61, 0x62, 0x63, 0xff, 0xff }, |
|
78 |
+}; |
|
79 |
+ |
|
80 |
+static const int dv_aaux_packs_dist[12][9] = { |
|
81 |
+ { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, |
|
82 |
+ { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, |
|
83 |
+ { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, |
|
84 |
+ { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, |
|
85 |
+ { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, |
|
86 |
+ { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, |
|
87 |
+ { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, |
|
88 |
+ { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, |
|
89 |
+ { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, |
|
90 |
+ { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, |
|
91 |
+ { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, |
|
92 |
+ { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, |
|
93 |
+}; |
|
94 |
+ |
|
95 |
+enum dv_section_type { |
|
96 |
+ dv_sect_header = 0x1f, |
|
97 |
+ dv_sect_subcode = 0x3f, |
|
98 |
+ dv_sect_vaux = 0x56, |
|
99 |
+ dv_sect_audio = 0x76, |
|
100 |
+ dv_sect_video = 0x96, |
|
101 |
+}; |
|
102 |
+ |
|
103 |
+enum dv_pack_type { |
|
104 |
+ dv_header525 = 0x3f, /* see dv_write_pack for important details on */ |
|
105 |
+ dv_header625 = 0xbf, /* these two packs */ |
|
106 |
+ dv_timecode = 0x13, |
|
107 |
+ dv_audio_source = 0x50, |
|
108 |
+ dv_audio_control = 0x51, |
|
109 |
+ dv_audio_recdate = 0x52, |
|
110 |
+ dv_audio_rectime = 0x53, |
|
111 |
+ dv_video_source = 0x60, |
|
112 |
+ dv_video_control = 0x61, |
|
113 |
+ dv_viedo_recdate = 0x62, |
|
114 |
+ dv_video_rectime = 0x63, |
|
115 |
+ dv_unknown_pack = 0xff, |
|
116 |
+}; |
|
117 |
+ |
|
118 |
+static const uint16_t dv_audio_shuffle525[10][9] = { |
|
119 |
+ { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */ |
|
120 |
+ { 6, 36, 66, 26, 56, 86, 16, 46, 76 }, |
|
121 |
+ { 12, 42, 72, 2, 32, 62, 22, 52, 82 }, |
|
122 |
+ { 18, 48, 78, 8, 38, 68, 28, 58, 88 }, |
|
123 |
+ { 24, 54, 84, 14, 44, 74, 4, 34, 64 }, |
|
124 |
+ |
|
125 |
+ { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */ |
|
126 |
+ { 7, 37, 67, 27, 57, 87, 17, 47, 77 }, |
|
127 |
+ { 13, 43, 73, 3, 33, 63, 23, 53, 83 }, |
|
128 |
+ { 19, 49, 79, 9, 39, 69, 29, 59, 89 }, |
|
129 |
+ { 25, 55, 85, 15, 45, 75, 5, 35, 65 }, |
|
130 |
+}; |
|
131 |
+ |
|
132 |
+static const uint16_t dv_audio_shuffle625[12][9] = { |
|
133 |
+ { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */ |
|
134 |
+ { 6, 42, 78, 32, 68, 104, 22, 58, 94}, |
|
135 |
+ { 12, 48, 84, 2, 38, 74, 28, 64, 100}, |
|
136 |
+ { 18, 54, 90, 8, 44, 80, 34, 70, 106}, |
|
137 |
+ { 24, 60, 96, 14, 50, 86, 4, 40, 76}, |
|
138 |
+ { 30, 66, 102, 20, 56, 92, 10, 46, 82}, |
|
139 |
+ |
|
140 |
+ { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */ |
|
141 |
+ { 7, 43, 79, 33, 69, 105, 23, 59, 95}, |
|
142 |
+ { 13, 49, 85, 3, 39, 75, 29, 65, 101}, |
|
143 |
+ { 19, 55, 91, 9, 45, 81, 35, 71, 107}, |
|
144 |
+ { 25, 61, 97, 15, 51, 87, 5, 41, 77}, |
|
145 |
+ { 31, 67, 103, 21, 57, 93, 11, 47, 83}, |
|
146 |
+}; |
|
147 |
+ |
|
148 |
+static const int dv_audio_frequency[3] = { |
|
149 |
+ 48000, 44100, 32000, |
|
150 |
+}; |
|
151 |
+ |
|
152 |
+const DVprofile dv_profiles[2] = { |
|
153 |
+ { .dsf = 0, |
|
154 |
+ .frame_size = 120000, /* 525/60 system (NTSC) */ |
|
155 |
+ .difseg_size = 10, |
|
156 |
+ .frame_rate = 30000, |
|
157 |
+ .ltc_divisor = 30, |
|
158 |
+ .frame_rate_base = 1001, |
|
159 |
+ .height = 480, |
|
160 |
+/* .video_place = dv_place_411, */ |
|
161 |
+ .audio_stride = 90, |
|
162 |
+ .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32Khz */ |
|
163 |
+ .audio_samples_dist = { 1602, 1601, 1602, 1601, 1602 }, |
|
164 |
+ .audio_shuffle = dv_audio_shuffle525, |
|
165 |
+ }, |
|
166 |
+ { .dsf = 1, |
|
167 |
+ .frame_size = 144000, /* 625/50 system (PAL) */ |
|
168 |
+ .difseg_size = 12, |
|
169 |
+ .frame_rate = 25, |
|
170 |
+ .frame_rate_base = 1, |
|
171 |
+ .ltc_divisor = 25, |
|
172 |
+ .height = 576, |
|
173 |
+/* .video_place = dv_place_420, */ |
|
174 |
+ .audio_stride = 108, |
|
175 |
+ .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32Khz */ |
|
176 |
+ .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 }, |
|
177 |
+ .audio_shuffle = dv_audio_shuffle525, |
|
178 |
+ } |
|
179 |
+}; |
|
180 |
+ |
|
181 |
+static inline uint16_t dv_audio_12to16(uint16_t sample) |
|
182 |
+{ |
|
183 |
+ uint16_t shift, result; |
|
184 |
+ |
|
185 |
+ sample = (sample < 0x800) ? sample : sample | 0xf000; |
|
186 |
+ shift = (sample & 0xf00) >> 8; |
|
187 |
+ |
|
188 |
+ if (shift < 0x2 || shift > 0xd) { |
|
189 |
+ result = sample; |
|
190 |
+ } else if (shift < 0x8) { |
|
191 |
+ shift--; |
|
192 |
+ result = (sample - (256 * shift)) << shift; |
|
193 |
+ } else { |
|
194 |
+ shift = 0xe - shift; |
|
195 |
+ result = ((sample + ((256 * shift) + 1)) << shift) - 1; |
|
196 |
+ } |
|
197 |
+ |
|
198 |
+ return result; |
|
199 |
+} |
|
200 |
+ |
|
201 |
+static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf) |
|
202 |
+{ |
|
203 |
+ struct tm tc; |
|
204 |
+ time_t ct; |
|
205 |
+ int ltc_frame; |
|
206 |
+ |
|
207 |
+ buf[0] = (uint8_t)pack_id; |
|
208 |
+ switch (pack_id) { |
|
209 |
+ case dv_header525: /* I can't imagine why these two weren't defined as real */ |
|
210 |
+ case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */ |
|
211 |
+ buf[1] = 0xf8 | /* reserved -- always 1 */ |
|
212 |
+ (0 & 0x07); /* APT: Track application ID */ |
|
213 |
+ buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */ |
|
214 |
+ (0x0f << 3) | /* reserved -- always 1 */ |
|
215 |
+ (0 & 0x07); /* AP1: Audio application ID */ |
|
216 |
+ buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */ |
|
217 |
+ (0x0f << 3) | /* reserved -- always 1 */ |
|
218 |
+ (0 & 0x07); /* AP2: Video application ID */ |
|
219 |
+ buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */ |
|
220 |
+ (0x0f << 3) | /* reserved -- always 1 */ |
|
221 |
+ (0 & 0x07); /* AP3: Subcode application ID */ |
|
222 |
+ break; |
|
223 |
+ case dv_timecode: |
|
224 |
+ ct = (time_t)(c->frames / ((float)c->sys->frame_rate / |
|
225 |
+ (float)c->sys->frame_rate_base)); |
|
226 |
+ localtime_r(&ct, &tc); |
|
227 |
+ /* |
|
228 |
+ * LTC drop-frame frame counter drops two frames (0 and 1) every |
|
229 |
+ * minute, unless it is exactly divisible by 10 |
|
230 |
+ */ |
|
231 |
+ ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor; |
|
232 |
+ buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */ |
|
233 |
+ (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */ |
|
234 |
+ ((ltc_frame / 10) << 4) | /* Tens of frames */ |
|
235 |
+ (ltc_frame % 10); /* Units of frames */ |
|
236 |
+ buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */ |
|
237 |
+ ((tc.tm_sec / 10) << 4) | /* Tens of seconds */ |
|
238 |
+ (tc.tm_sec % 10); /* Units of seconds */ |
|
239 |
+ buf[3] = (1 << 7) | /* Binary group flag BGF0 */ |
|
240 |
+ ((tc.tm_min / 10) << 4) | /* Tens of minutes */ |
|
241 |
+ (tc.tm_min % 10); /* Units of minutes */ |
|
242 |
+ buf[4] = (1 << 7) | /* Binary group flag BGF2 */ |
|
243 |
+ (1 << 6) | /* Binary group flag BGF1 */ |
|
244 |
+ ((tc.tm_hour / 10) << 4) | /* Tens of hours */ |
|
245 |
+ (tc.tm_hour % 10); /* Units of hours */ |
|
246 |
+ break; |
|
247 |
+ case dv_audio_source: /* AAUX source pack */ |
|
248 |
+ buf[1] = (0 << 7) | /* locked mode */ |
|
249 |
+ (1 << 6) | /* reserved -- always 1 */ |
|
250 |
+ (dv_audio_frame_size(c->sys, c->frames) - |
|
251 |
+ c->sys->audio_min_samples[0]); |
|
252 |
+ /* # of samples */ |
|
253 |
+ buf[2] = (0 << 7) | /* multi-stereo */ |
|
254 |
+ (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */ |
|
255 |
+ (0 << 4) | /* pair bit: 0 -- one pair of channels */ |
|
256 |
+ 0; /* audio mode */ |
|
257 |
+ buf[3] = (1 << 7) | /* res */ |
|
258 |
+ (1 << 6) | /* multi-language flag */ |
|
259 |
+ (c->sys->dsf << 5) | /* system: 60fields/50fields */ |
|
260 |
+ 0; /* definition: 0 -- SD (525/625) */ |
|
261 |
+ buf[4] = (1 << 7) | /* emphasis: 1 -- off */ |
|
262 |
+ (0 << 6) | /* emphasis time constant: 0 -- reserved */ |
|
263 |
+ (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */ |
|
264 |
+ 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */ |
|
265 |
+ break; |
|
266 |
+ case dv_audio_control: |
|
267 |
+ buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ |
|
268 |
+ (1 << 4) | /* input source: 1 -- digital input */ |
|
269 |
+ (3 << 2) | /* compression: 3 -- no information */ |
|
270 |
+ 0; /* misc. info/SMPTE emphasis off */ |
|
271 |
+ buf[2] = (1 << 7) | /* recording start point: 1 -- no */ |
|
272 |
+ (1 << 6) | /* recording end point: 1 -- no */ |
|
273 |
+ (1 << 3) | /* recording mode: 1 -- original */ |
|
274 |
+ 7; |
|
275 |
+ buf[3] = (1 << 7) | /* direction: 1 -- forward */ |
|
276 |
+ 0x20; /* speed */ |
|
277 |
+ buf[4] = (1 << 7) | /* reserved -- always 1 */ |
|
278 |
+ 0x7f; /* genre category */ |
|
279 |
+ break; |
|
280 |
+ case dv_audio_recdate: |
|
281 |
+ case dv_viedo_recdate: /* VAUX recording date */ |
|
282 |
+ ct = c->start_time + (time_t)(c->frames / |
|
283 |
+ ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base)); |
|
284 |
+ localtime_r(&ct, &tc); |
|
285 |
+ buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */ |
|
286 |
+ /* 0xff is very likely to be "unknown" */ |
|
287 |
+ buf[2] = (3 << 6) | /* reserved -- always 1 */ |
|
288 |
+ ((tc.tm_mday / 10) << 4) | /* Tens of day */ |
|
289 |
+ (tc.tm_mday % 10); /* Units of day */ |
|
290 |
+ buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */ |
|
291 |
+ (tc.tm_mon % 10); /* Units of month */ |
|
292 |
+ buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */ |
|
293 |
+ (tc.tm_year % 10); /* Units of year */ |
|
294 |
+ break; |
|
295 |
+ case dv_audio_rectime: /* AAUX recording time */ |
|
296 |
+ case dv_video_rectime: /* VAUX recording time */ |
|
297 |
+ ct = c->start_time + (time_t)(c->frames / |
|
298 |
+ ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base)); |
|
299 |
+ localtime_r(&ct, &tc); |
|
300 |
+ buf[1] = (3 << 6) | /* reserved -- always 1 */ |
|
301 |
+ 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */ |
|
302 |
+ buf[2] = (1 << 7) | /* reserved -- always 1 */ |
|
303 |
+ ((tc.tm_sec / 10) << 4) | /* Tens of seconds */ |
|
304 |
+ (tc.tm_sec % 10); /* Units of seconds */ |
|
305 |
+ buf[3] = (1 << 7) | /* reserved -- always 1 */ |
|
306 |
+ ((tc.tm_min / 10) << 4) | /* Tens of minutes */ |
|
307 |
+ (tc.tm_min % 10); /* Units of minutes */ |
|
308 |
+ buf[4] = (3 << 6) | /* reserved -- always 1 */ |
|
309 |
+ ((tc.tm_hour / 10) << 4) | /* Tens of hours */ |
|
310 |
+ (tc.tm_hour % 10); /* Units of hours */ |
|
311 |
+ break; |
|
312 |
+ case dv_video_source: |
|
313 |
+ buf[1] = 0xff; /* reserved -- always 1 */ |
|
314 |
+ buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */ |
|
315 |
+ (1 << 6) | /* following CLF is valid - 0, invalid - 1 */ |
|
316 |
+ (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */ |
|
317 |
+ 0xf; /* reserved -- always 1 */ |
|
318 |
+ buf[3] = (3 << 6) | /* reserved -- always 1 */ |
|
319 |
+ (c->sys->dsf << 5) | /* system: 60fields/50fields */ |
|
320 |
+ 0; /* signal type video compression */ |
|
321 |
+ buf[4] = 0xff; /* VISC: 0xff -- no information */ |
|
322 |
+ break; |
|
323 |
+ case dv_video_control: |
|
324 |
+ buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */ |
|
325 |
+ 0x3f; /* reserved -- always 1 */ |
|
326 |
+ buf[2] = 0xc8 | /* reserved -- always b11001xxx */ |
|
327 |
+ c->aspect; |
|
328 |
+ buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */ |
|
329 |
+ (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */ |
|
330 |
+ (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */ |
|
331 |
+ (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */ |
|
332 |
+ 0xc; /* reserved -- always b1100 */ |
|
333 |
+ buf[4] = 0xff; /* reserved -- always 1 */ |
|
334 |
+ break; |
|
335 |
+ default: |
|
336 |
+ buf[1] = buf[2] = buf[3] = buf[4] = 0xff; |
|
337 |
+ } |
|
338 |
+ return 5; |
|
339 |
+} |
|
340 |
+ |
|
341 |
+static inline int dv_write_dif_id(enum dv_section_type t, uint8_t seq_num, |
|
342 |
+ uint8_t dif_num, uint8_t* buf) |
|
343 |
+{ |
|
344 |
+ buf[0] = (uint8_t)t; /* Section type */ |
|
345 |
+ buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */ |
|
346 |
+ (0 << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */ |
|
347 |
+ 7; /* reserved -- always 1 */ |
|
348 |
+ buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */ |
|
349 |
+ return 3; |
|
350 |
+} |
|
351 |
+ |
|
352 |
+static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf) |
|
353 |
+{ |
|
354 |
+ if (syb_num == 0 || syb_num == 6) { |
|
355 |
+ buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */ |
|
356 |
+ (0<<4) | /* AP3 (Subcode application ID) */ |
|
357 |
+ 0x0f; /* reserved -- always 1 */ |
|
358 |
+ } |
|
359 |
+ else if (syb_num == 11) { |
|
360 |
+ buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */ |
|
361 |
+ 0x7f; /* reserved -- always 1 */ |
|
362 |
+ } |
|
363 |
+ else { |
|
364 |
+ buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */ |
|
365 |
+ (0<<4) | /* APT (Track application ID) */ |
|
366 |
+ 0x0f; /* reserved -- always 1 */ |
|
367 |
+ } |
|
368 |
+ buf[1] = 0xf0 | /* reserved -- always 1 */ |
|
369 |
+ (syb_num & 0x0f); /* SSYB number 0 - 11 */ |
|
370 |
+ buf[2] = 0xff; /* reserved -- always 1 */ |
|
371 |
+ return 3; |
|
372 |
+} |
|
373 |
+ |
|
374 |
+void dv_format_frame(DVMuxContext *c, uint8_t* buf) |
|
375 |
+{ |
|
376 |
+ int i, j, k; |
|
377 |
+ |
|
378 |
+ for (i = 0; i < c->sys->difseg_size; i++) { |
|
379 |
+ memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */ |
|
380 |
+ |
|
381 |
+ /* DV header: 1DIF */ |
|
382 |
+ buf += dv_write_dif_id(dv_sect_header, i, 0, buf); |
|
383 |
+ buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf); |
|
384 |
+ buf += 72; /* unused bytes */ |
|
385 |
+ |
|
386 |
+ /* DV subcode: 2DIFs */ |
|
387 |
+ for (j = 0; j < 2; j++) { |
|
388 |
+ buf += dv_write_dif_id( dv_sect_subcode, i, j, buf); |
|
389 |
+ for (k = 0; k < 6; k++) { |
|
390 |
+ buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf); |
|
391 |
+ buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf); |
|
392 |
+ } |
|
393 |
+ buf += 29; /* unused bytes */ |
|
394 |
+ } |
|
395 |
+ |
|
396 |
+ /* DV VAUX: 3DIFs */ |
|
397 |
+ for (j = 0; j < 3; j++) { |
|
398 |
+ buf += dv_write_dif_id(dv_sect_vaux, i, j, buf); |
|
399 |
+ for (k = 0; k < 15 ; k++) |
|
400 |
+ buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf); |
|
401 |
+ buf += 2; /* unused bytes */ |
|
402 |
+ } |
|
403 |
+ |
|
404 |
+ /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */ |
|
405 |
+ for (j = 0; j < 135; j++) { |
|
406 |
+ if (j%15 == 0) { |
|
407 |
+ buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf); |
|
408 |
+ buf += dv_write_pack(dv_aaux_packs_dist[i][j/15], c, buf); |
|
409 |
+ buf += 72; /* shuffled PCM audio */ |
|
410 |
+ } |
|
411 |
+ buf += dv_write_dif_id(dv_sect_video, i, j, buf); |
|
412 |
+ buf += 77; /* 1 video macro block: 1 bytes control |
|
413 |
+ 4 * 14 bytes Y 8x8 data |
|
414 |
+ 10 bytes Cr 8x8 data |
|
415 |
+ 10 bytes Cb 8x8 data */ |
|
416 |
+ } |
|
417 |
+ } |
|
418 |
+} |
|
419 |
+ |
|
420 |
+void dv_inject_audio(DVMuxContext *c, uint8_t* pcm, uint8_t* frame_ptr) |
|
421 |
+{ |
|
422 |
+ int i, j, d, of; |
|
423 |
+ for (i = 0; i < c->sys->difseg_size; i++) { |
|
424 |
+ frame_ptr += 6 * 80; /* skip DIF segment header */ |
|
425 |
+ for (j = 0; j < 9; j++) { |
|
426 |
+ for (d = 8; d < 80; d+=2) { |
|
427 |
+ of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride; |
|
428 |
+ frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit |
|
429 |
+ frame_ptr[d+1] = pcm[of*2]; // that DV is a big endian PCM |
|
430 |
+ } |
|
431 |
+ frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ |
|
432 |
+ } |
|
433 |
+ } |
|
434 |
+} |
|
435 |
+ |
|
436 |
+void dv_inject_video(DVMuxContext *c, uint8_t* video_data, uint8_t* frame_ptr) |
|
437 |
+{ |
|
438 |
+ int i, j; |
|
439 |
+ int ptr = 0; |
|
440 |
+ |
|
441 |
+ for (i = 0; i < c->sys->difseg_size; i++) { |
|
442 |
+ ptr += 6 * 80; /* skip DIF segment header */ |
|
443 |
+ for (j = 0; j < 135; j++) { |
|
444 |
+ if (j%15 == 0) |
|
445 |
+ ptr += 80; /* skip Audio DIF */ |
|
446 |
+ ptr += 3; |
|
447 |
+ memcpy(frame_ptr + ptr, video_data + ptr, 77); |
|
448 |
+ ptr += 77; |
|
449 |
+ } |
|
450 |
+ } |
|
451 |
+} |
|
452 |
+ |
|
453 |
+int dv_audio_frame_size(const DVprofile* sys, int frame) |
|
454 |
+{ |
|
455 |
+ return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/ |
|
456 |
+ sizeof(sys->audio_samples_dist[0]))]; |
|
457 |
+} |
|
458 |
+ |
|
459 |
+const DVprofile* dv_frame_profile(uint8_t* frame) |
|
460 |
+{ |
|
461 |
+ return &dv_profiles[!!(frame[3] & 0x80)]; /* Header, DSF flag */ |
|
462 |
+} |
|
463 |
+ |
|
464 |
+/* |
|
465 |
+ * This is the dumbest implementation of all -- it simply looks at |
|
466 |
+ * a fixed offset and if pack isn't there -- fails. We might want |
|
467 |
+ * to have a fallback mechanism for complete search of missing packs. |
|
468 |
+ */ |
|
469 |
+const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t) |
|
470 |
+{ |
|
471 |
+ int offs; |
|
472 |
+ |
|
473 |
+ switch (t) { |
|
474 |
+ case dv_audio_source: |
|
475 |
+ offs = (80*6 + 80*16*3 + 3); |
|
476 |
+ break; |
|
477 |
+ case dv_audio_control: |
|
478 |
+ offs = (80*6 + 80*16*4 + 3); |
|
479 |
+ break; |
|
480 |
+ case dv_video_control: |
|
481 |
+ offs = (80*5 + 48 + 5); |
|
482 |
+ break; |
|
483 |
+ default: |
|
484 |
+ return NULL; |
|
485 |
+ } |
|
486 |
+ |
|
487 |
+ return (frame[offs] == t ? &frame[offs] : NULL); |
|
488 |
+} |
|
489 |
+ |
|
490 |
+/* |
|
491 |
+ * There's a couple of assumptions being made here: |
|
492 |
+ * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples. |
|
493 |
+ * We can pass them upwards when ffmpeg will be ready to deal with them. |
|
494 |
+ * 2. We don't do software emphasis. |
|
495 |
+ * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples |
|
496 |
+ * are converted into 16bit linear ones. |
|
497 |
+ */ |
|
498 |
+int dv_extract_audio(uint8_t* frame, uint8_t* pcm, AVCodecContext* avctx) |
|
499 |
+{ |
|
500 |
+ int size, i, j, d, of, smpls, freq, quant; |
|
501 |
+ uint16_t lc, rc; |
|
502 |
+ const DVprofile* sys; |
|
503 |
+ const uint8_t* as_pack; |
|
504 |
+ |
|
505 |
+ as_pack = dv_extract_pack(frame, dv_audio_source); |
|
506 |
+ if (!as_pack) /* No audio ? */ |
|
507 |
+ return 0; |
|
508 |
+ |
|
509 |
+ sys = dv_frame_profile(frame); |
|
510 |
+ smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ |
|
511 |
+ freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ |
|
512 |
+ quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ |
|
513 |
+ |
|
514 |
+ if (quant > 1) |
|
515 |
+ return -1; /* Unsupported quantization */ |
|
516 |
+ |
|
517 |
+ avctx->sample_rate = dv_audio_frequency[freq]; |
|
518 |
+ avctx->channels = 2; |
|
519 |
+ avctx->bit_rate = avctx->channels * avctx->sample_rate * 16; |
|
520 |
+ // What about: |
|
521 |
+ // avctx->frame_size = |
|
522 |
+ |
|
523 |
+ size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */ |
|
524 |
+ |
|
525 |
+ /* for each DIF segment */ |
|
526 |
+ for (i = 0; i < sys->difseg_size; i++) { |
|
527 |
+ frame += 6 * 80; /* skip DIF segment header */ |
|
528 |
+ for (j = 0; j < 9; j++) { |
|
529 |
+ for (d = 8; d < 80; d += 2) { |
|
530 |
+ if (quant == 0) { /* 16bit quantization */ |
|
531 |
+ of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride; |
|
532 |
+ pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit |
|
533 |
+ pcm[of*2+1] = frame[d]; // that DV is a big endian PCM |
|
534 |
+ if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00) |
|
535 |
+ pcm[of*2+1] = 0; |
|
536 |
+ } else { /* 12bit quantization */ |
|
537 |
+ if (i >= sys->difseg_size/2) |
|
538 |
+ goto out; /* We're not doing 4ch at this time */ |
|
539 |
+ |
|
540 |
+ lc = ((uint16_t)frame[d] << 4) | |
|
541 |
+ ((uint16_t)frame[d+2] >> 4); |
|
542 |
+ rc = ((uint16_t)frame[d+1] << 4) | |
|
543 |
+ ((uint16_t)frame[d+2] & 0x0f); |
|
544 |
+ lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); |
|
545 |
+ rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); |
|
546 |
+ |
|
547 |
+ of = sys->audio_shuffle[i][j] + (d - 8)/3 * sys->audio_stride; |
|
548 |
+ pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit |
|
549 |
+ pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM |
|
550 |
+ of = sys->audio_shuffle[i+sys->difseg_size/2][j] + |
|
551 |
+ (d - 8)/3 * sys->audio_stride; |
|
552 |
+ pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit |
|
553 |
+ pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM |
|
554 |
+ ++d; |
|
555 |
+ } |
|
556 |
+ } |
|
557 |
+ |
|
558 |
+ frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ |
|
559 |
+ } |
|
560 |
+ } |
|
561 |
+ |
|
562 |
+out: |
|
563 |
+ return size; |
|
564 |
+} |
|
565 |
+ |
|
566 |
+/* FIXME: The following three functions could be underengineered ;-) */ |
|
567 |
+void dv_assemble_frame(DVMuxContext *c, uint8_t* video, uint8_t* audio, int asize) |
|
568 |
+{ |
|
569 |
+ uint8_t pcm[8192]; |
|
570 |
+ uint8_t* frame = &c->frame_buf[0]; |
|
571 |
+ int fsize, reqasize; |
|
572 |
+ |
|
573 |
+ if (c->has_audio && c->has_video) { /* must be a stale frame */ |
|
574 |
+ dv_format_frame(c, frame); |
|
575 |
+ c->frames++; |
|
576 |
+ c->has_audio = c->has_video = 0; |
|
577 |
+ } |
|
578 |
+ |
|
579 |
+ if (video) { |
|
580 |
+ /* FIXME: we have to have more sensible approach than this one */ |
|
581 |
+ if (c->has_video) |
|
582 |
+ fprintf(stderr, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames); |
|
583 |
+ |
|
584 |
+ dv_inject_video(c, video, frame); |
|
585 |
+ c->has_video = 1; |
|
586 |
+ } |
|
587 |
+ if (audio) { |
|
588 |
+ reqasize = 4 * dv_audio_frame_size(c->sys, c->frames); |
|
589 |
+ fsize = fifo_size(&c->audio_data, c->audio_data.rptr); |
|
590 |
+ if (fsize + asize >= reqasize) { |
|
591 |
+ if (fsize >= reqasize) { |
|
592 |
+ fifo_read(&c->audio_data, &pcm[0], reqasize, &c->audio_data.rptr); |
|
593 |
+ } else { |
|
594 |
+ fifo_read(&c->audio_data, &pcm[0], fsize, &c->audio_data.rptr); |
|
595 |
+ memcpy(&pcm[fsize], &audio[0], reqasize - fsize); |
|
596 |
+ audio += reqasize - fsize; |
|
597 |
+ asize -= reqasize - fsize; |
|
598 |
+ } |
|
599 |
+ dv_inject_audio(c, &pcm[0], frame); |
|
600 |
+ c->has_audio = 1; |
|
601 |
+ } |
|
602 |
+ |
|
603 |
+ /* FIXME: we have to have more sensible approach than this one */ |
|
604 |
+ if (fifo_size(&c->audio_data, c->audio_data.rptr) + asize >= AVCODEC_MAX_AUDIO_FRAME_SIZE) |
|
605 |
+ fprintf(stderr, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames); |
|
606 |
+ fifo_write(&c->audio_data, audio, asize, &c->audio_data.wptr); |
|
607 |
+ } |
|
608 |
+} |
|
609 |
+ |
|
610 |
+int dv_core_init(DVMuxContext *c, AVStream *streams[]) |
|
611 |
+{ |
|
612 |
+ /* We have to sort out where audio and where video stream is */ |
|
613 |
+ if (streams[0]->codec.codec_type == CODEC_TYPE_VIDEO && |
|
614 |
+ streams[1]->codec.codec_type == CODEC_TYPE_AUDIO) { |
|
615 |
+ c->vst = 0; |
|
616 |
+ c->ast = 1; |
|
617 |
+ } |
|
618 |
+ else if (streams[1]->codec.codec_type == CODEC_TYPE_VIDEO && |
|
619 |
+ streams[0]->codec.codec_type == CODEC_TYPE_AUDIO) { |
|
620 |
+ c->vst = 1; |
|
621 |
+ c->ast = 0; |
|
622 |
+ } else |
|
623 |
+ goto bail_out; |
|
624 |
+ |
|
625 |
+ /* Some checks -- DV format is very picky about its incoming streams */ |
|
626 |
+ if (streams[c->vst]->codec.codec_id != CODEC_ID_DVVIDEO || |
|
627 |
+ streams[c->ast]->codec.codec_id != CODEC_ID_PCM_S16LE) |
|
628 |
+ goto bail_out; |
|
629 |
+ if (streams[c->ast]->codec.sample_rate != 48000 || |
|
630 |
+ streams[c->ast]->codec.channels != 2) |
|
631 |
+ goto bail_out; |
|
632 |
+ |
|
633 |
+ if (streams[c->vst]->codec.frame_rate == 25 && |
|
634 |
+ streams[c->vst]->codec.frame_rate_base == 1) { |
|
635 |
+ /* May be we have to pick sys for every frame */ |
|
636 |
+ c->sys = &dv_profiles[1]; |
|
637 |
+ } |
|
638 |
+ else if (streams[c->vst]->codec.frame_rate == 30000 && |
|
639 |
+ streams[c->vst]->codec.frame_rate_base == 1001) { |
|
640 |
+ /* May be we have to pick sys for every frame */ |
|
641 |
+ c->sys = &dv_profiles[0]; |
|
642 |
+ } else |
|
643 |
+ goto bail_out; |
|
644 |
+ |
|
645 |
+ /* Ok, everything seems to be in working order */ |
|
646 |
+ c->frames = 0; |
|
647 |
+ c->has_audio = c->has_video = 0; |
|
648 |
+ c->start_time = time(NULL); |
|
649 |
+ c->aspect = 0; /* 4:3 is the default */ |
|
650 |
+ if (streams[c->vst]->codec.aspect_ratio == 16.0 / 9.0) |
|
651 |
+ c->aspect = 0x07; |
|
652 |
+ |
|
653 |
+ if (fifo_init(&c->audio_data, AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) |
|
654 |
+ goto bail_out; |
|
655 |
+ |
|
656 |
+ dv_format_frame(c, &c->frame_buf[0]); |
|
657 |
+ |
|
658 |
+ return 0; |
|
659 |
+ |
|
660 |
+bail_out: |
|
661 |
+ return -1; |
|
662 |
+} |
|
663 |
+ |
|
664 |
+void dv_core_delete(DVMuxContext *c) |
|
665 |
+{ |
|
666 |
+ fifo_free(&c->audio_data); |
|
667 |
+} |
0 | 668 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,73 @@ |
0 |
+/* |
|
1 |
+ * DV format muxer/demuxer |
|
2 |
+ * Copyright (c) 2003 Roman Shaposhnik |
|
3 |
+ * |
|
4 |
+ * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth |
|
5 |
+ * of DV technical info, and SMPTE 314M specification. |
|
6 |
+ * |
|
7 |
+ * This library is free software; you can redistribute it and/or |
|
8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
9 |
+ * License as published by the Free Software Foundation; either |
|
10 |
+ * version 2 of the License, or (at your option) any later version. |
|
11 |
+ * |
|
12 |
+ * This library is distributed in the hope that it will be useful, |
|
13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
+ * Lesser General Public License for more details. |
|
16 |
+ * |
|
17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
18 |
+ * License along with this library; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
20 |
+ */ |
|
21 |
+#include <time.h> |
|
22 |
+#include "avformat.h" |
|
23 |
+ |
|
24 |
+/* |
|
25 |
+ * DVprofile is used to express the differences between various |
|
26 |
+ * DV flavors. For now it's primarily used for differentiating |
|
27 |
+ * 525/60 and 625/50, but the plans are to use it for various |
|
28 |
+ * DV specs as well (e.g. SMPTE314M vs. IEC 61834). |
|
29 |
+ */ |
|
30 |
+typedef struct DVprofile { |
|
31 |
+ int dsf; /* value of the dsf in the DV header */ |
|
32 |
+ int frame_size; /* total size of one frame in bytes */ |
|
33 |
+ int difseg_size; /* number of DIF segments */ |
|
34 |
+ int frame_rate; |
|
35 |
+ int frame_rate_base; |
|
36 |
+ int ltc_divisor; /* FPS from the LTS standpoint */ |
|
37 |
+ int height; /* picture height in pixels */ |
|
38 |
+ uint16_t *video_place; /* positions of all DV macro blocks */ |
|
39 |
+ |
|
40 |
+ int audio_stride; /* size of audio_shuffle table */ |
|
41 |
+ int audio_min_samples[3]; /* min ammount of audio samples */ |
|
42 |
+ /* for 48Khz, 44.1Khz and 32Khz */ |
|
43 |
+ int audio_samples_dist[5];/* how many samples are supposed to be */ |
|
44 |
+ /* in each frame in a 5 frames window */ |
|
45 |
+ const uint16_t (*audio_shuffle)[9]; /* PCM shuffling table */ |
|
46 |
+} DVprofile; |
|
47 |
+ |
|
48 |
+typedef struct DVMuxContext { |
|
49 |
+ const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */ |
|
50 |
+ uint8_t frame_buf[144000]; /* frame under contruction */ |
|
51 |
+ FifoBuffer audio_data; /* Fifo for storing excessive amounts of PCM */ |
|
52 |
+ int frames; /* Number of a current frame */ |
|
53 |
+ time_t start_time; /* Start time of recording */ |
|
54 |
+ uint8_t aspect; /* Aspect ID 0 - 4:3, 7 - 16:9 */ |
|
55 |
+ int ast, vst; /* Audio and Video stream indecies */ |
|
56 |
+ int has_audio; /* frame under contruction has audio */ |
|
57 |
+ int has_video; /* frame under contruction has video */ |
|
58 |
+} DVMuxContext; |
|
59 |
+ |
|
60 |
+void dv_format_frame(DVMuxContext *, uint8_t*); |
|
61 |
+void dv_inject_audio(DVMuxContext *, uint8_t*, uint8_t*); |
|
62 |
+void dv_inject_video(DVMuxContext *, uint8_t*, uint8_t*); |
|
63 |
+ |
|
64 |
+int dv_extract_audio(uint8_t*, uint8_t*, AVCodecContext*); |
|
65 |
+ |
|
66 |
+int dv_audio_frame_size(const DVprofile*, int); |
|
67 |
+ |
|
68 |
+void dv_assemble_frame(DVMuxContext *, uint8_t*, uint8_t*, int); |
|
69 |
+int dv_core_init(DVMuxContext *, AVStream*[]); |
|
70 |
+void dv_core_delete(DVMuxContext *); |
|
71 |
+ |
|
72 |
+const DVprofile* dv_frame_profile(uint8_t*); |