comparison dv.c @ 4008:8af7a177b17d libavformat

prettyprinting cosmetics
author diego
date Fri, 24 Oct 2008 21:41:27 +0000
parents d5eb96f97305
children dde5fb2506b6
comparison
equal deleted inserted replaced
4007:d5eb96f97305 4008:8af7a177b17d
33 #include "libavcodec/dvdata.h" 33 #include "libavcodec/dvdata.h"
34 #include "dv.h" 34 #include "dv.h"
35 35
36 struct DVDemuxContext { 36 struct DVDemuxContext {
37 const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */ 37 const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
38 AVFormatContext* fctx; 38 AVFormatContext* fctx;
39 AVStream* vst; 39 AVStream* vst;
40 AVStream* ast[4]; 40 AVStream* ast[4];
41 AVPacket audio_pkt[4]; 41 AVPacket audio_pkt[4];
42 uint8_t audio_buf[4][8192]; 42 uint8_t audio_buf[4][8192];
43 int ach; 43 int ach;
44 int frames; 44 int frames;
45 uint64_t abytes; 45 uint64_t abytes;
46 }; 46 };
47 47
48 static inline uint16_t dv_audio_12to16(uint16_t sample) 48 static inline uint16_t dv_audio_12to16(uint16_t sample)
49 { 49 {
50 uint16_t shift, result; 50 uint16_t shift, result;
51 51
52 sample = (sample < 0x800) ? sample : sample | 0xf000; 52 sample = (sample < 0x800) ? sample : sample | 0xf000;
53 shift = (sample & 0xf00) >> 8; 53 shift = (sample & 0xf00) >> 8;
54 54
55 if (shift < 0x2 || shift > 0xd) { 55 if (shift < 0x2 || shift > 0xd) {
56 result = sample; 56 result = sample;
57 } else if (shift < 0x8) { 57 } else if (shift < 0x8) {
58 shift--; 58 shift--;
74 { 74 {
75 int offs; 75 int offs;
76 76
77 switch (t) { 77 switch (t) {
78 case dv_audio_source: 78 case dv_audio_source:
79 offs = (80*6 + 80*16*3 + 3); 79 offs = (80*6 + 80*16*3 + 3);
80 break; 80 break;
81 case dv_audio_control: 81 case dv_audio_control:
82 offs = (80*6 + 80*16*4 + 3); 82 offs = (80*6 + 80*16*4 + 3);
83 break; 83 break;
84 case dv_video_control: 84 case dv_video_control:
85 offs = (80*5 + 48 + 5); 85 offs = (80*5 + 48 + 5);
86 break; 86 break;
87 default: 87 default:
88 return NULL; 88 return NULL;
89 } 89 }
90 90
91 return frame[offs] == t ? &frame[offs] : NULL; 91 return frame[offs] == t ? &frame[offs] : NULL;
92 } 92 }
93 93
109 109
110 as_pack = dv_extract_pack(frame, dv_audio_source); 110 as_pack = dv_extract_pack(frame, dv_audio_source);
111 if (!as_pack) /* No audio ? */ 111 if (!as_pack) /* No audio ? */
112 return 0; 112 return 0;
113 113
114 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ 114 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
115 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */ 115 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
116 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ 116 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
117 117
118 if (quant > 1) 118 if (quant > 1)
119 return -1; /* unsupported quantization */ 119 return -1; /* unsupported quantization */
120 120
121 size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */ 121 size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
122 half_ch = sys->difseg_size/2; 122 half_ch = sys->difseg_size / 2;
123 123
124 /* We work with 720p frames split in half, thus even frames have 124 /* We work with 720p frames split in half, thus even frames have
125 * channels 0,1 and odd 2,3 */ 125 * channels 0,1 and odd 2,3. */
126 ipcm = (sys->height == 720 && !(frame[1]&0x0C))?2:0; 126 ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
127 pcm = ppcm[ipcm++]; 127 pcm = ppcm[ipcm++];
128 128
129 /* for each DIF channel */ 129 /* for each DIF channel */
130 for (chan = 0; chan < sys->n_difchan; chan++) { 130 for (chan = 0; chan < sys->n_difchan; chan++) {
131 /* for each DIF segment */ 131 /* for each DIF segment */
132 for (i = 0; i < sys->difseg_size; i++) { 132 for (i = 0; i < sys->difseg_size; i++) {
140 140
141 /* for each AV sequence */ 141 /* for each AV sequence */
142 for (j = 0; j < 9; j++) { 142 for (j = 0; j < 9; j++) {
143 for (d = 8; d < 80; d += 2) { 143 for (d = 8; d < 80; d += 2) {
144 if (quant == 0) { /* 16bit quantization */ 144 if (quant == 0) { /* 16bit quantization */
145 of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride; 145 of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
146 if (of*2 >= size) 146 if (of*2 >= size)
147 continue; 147 continue;
148 148
149 pcm[of*2] = frame[d+1]; // FIXME: maybe we have to admit 149 pcm[of*2] = frame[d+1]; // FIXME: maybe we have to admit
150 pcm[of*2+1] = frame[d]; // that DV is a big-endian PCM 150 pcm[of*2+1] = frame[d]; // that DV is a big-endian PCM
151 if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00) 151 if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
152 pcm[of*2+1] = 0; 152 pcm[of*2+1] = 0;
153 } else { /* 12bit quantization */ 153 } else { /* 12bit quantization */
154 lc = ((uint16_t)frame[d] << 4) | 154 lc = ((uint16_t)frame[d] << 4) |
155 ((uint16_t)frame[d+2] >> 4); 155 ((uint16_t)frame[d+2] >> 4);
156 rc = ((uint16_t)frame[d+1] << 4) | 156 rc = ((uint16_t)frame[d+1] << 4) |
157 ((uint16_t)frame[d+2] & 0x0f); 157 ((uint16_t)frame[d+2] & 0x0f);
158 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); 158 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
159 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); 159 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
160 160
161 of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride; 161 of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
162 if (of*2 >= size) 162 if (of*2 >= size)
163 continue; 163 continue;
164 164
165 pcm[of*2] = lc & 0xff; // FIXME: maybe we have to admit 165 pcm[of*2] = lc & 0xff; // FIXME: maybe we have to admit
166 pcm[of*2+1] = lc >> 8; // that DV is a big-endian PCM 166 pcm[of*2+1] = lc >> 8; // that DV is a big-endian PCM
167 of = sys->audio_shuffle[i%half_ch+half_ch][j] + 167 of = sys->audio_shuffle[i%half_ch+half_ch][j] +
168 (d - 8)/3 * sys->audio_stride; 168 (d - 8) / 3 * sys->audio_stride;
169 pcm[of*2] = rc & 0xff; // FIXME: maybe we have to admit 169 pcm[of*2] = rc & 0xff; // FIXME: maybe we have to admit
170 pcm[of*2+1] = rc >> 8; // that DV is a big-endian PCM 170 pcm[of*2+1] = rc >> 8; // that DV is a big-endian PCM
171 ++d; 171 ++d;
172 } 172 }
173 } 173 }
194 if (!as_pack || !c->sys) { /* No audio ? */ 194 if (!as_pack || !c->sys) { /* No audio ? */
195 c->ach = 0; 195 c->ach = 0;
196 return 0; 196 return 0;
197 } 197 }
198 198
199 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ 199 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
200 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */ 200 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
201 stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH, 3 - 8CH */ 201 stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
202 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ 202 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
203 203
204 /* note: ach counts PAIRS of channels (i.e. stereo channels) */ 204 /* note: ach counts PAIRS of channels (i.e. stereo channels) */
205 ach = ((int[4]){ 1, 0, 2, 4})[stype]; 205 ach = ((int[4]){ 1, 0, 2, 4})[stype];
206 if (ach == 1 && quant && freq == 2) 206 if (ach == 1 && quant && freq == 2)
207 ach = 2; 207 ach = 2;
208 208
209 /* Dynamic handling of the audio streams in DV */ 209 /* Dynamic handling of the audio streams in DV */
210 for (i=0; i<ach; i++) { 210 for (i = 0; i < ach; i++) {
211 if (!c->ast[i]) { 211 if (!c->ast[i]) {
212 c->ast[i] = av_new_stream(c->fctx, 0); 212 c->ast[i] = av_new_stream(c->fctx, 0);
213 if (!c->ast[i]) 213 if (!c->ast[i])
214 break; 214 break;
215 av_set_pts_info(c->ast[i], 64, 1, 30000); 215 av_set_pts_info(c->ast[i], 64, 1, 30000);
216 c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO; 216 c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
217 c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE; 217 c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
218 218
219 av_init_packet(&c->audio_pkt[i]); 219 av_init_packet(&c->audio_pkt[i]);
220 c->audio_pkt[i].size = 0; 220 c->audio_pkt[i].size = 0;
221 c->audio_pkt[i].data = c->audio_buf[i]; 221 c->audio_pkt[i].data = c->audio_buf[i];
222 c->audio_pkt[i].stream_index = c->ast[i]->index; 222 c->audio_pkt[i].stream_index = c->ast[i]->index;
223 c->audio_pkt[i].flags |= PKT_FLAG_KEY; 223 c->audio_pkt[i].flags |= PKT_FLAG_KEY;
224 } 224 }
225 c->ast[i]->codec->sample_rate = dv_audio_frequency[freq]; 225 c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
226 c->ast[i]->codec->channels = 2; 226 c->ast[i]->codec->channels = 2;
227 c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16; 227 c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
228 c->ast[i]->start_time = 0; 228 c->ast[i]->start_time = 0;
229 } 229 }
230 c->ach = i; 230 c->ach = i;
231 231
232 return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */; 232 return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
233 } 233 }
240 int size = 0; 240 int size = 0;
241 241
242 if (c->sys) { 242 if (c->sys) {
243 avctx = c->vst->codec; 243 avctx = c->vst->codec;
244 244
245 av_set_pts_info(c->vst, 64, c->sys->time_base.num, c->sys->time_base.den); 245 av_set_pts_info(c->vst, 64, c->sys->time_base.num,
246 c->sys->time_base.den);
246 avctx->time_base= c->sys->time_base; 247 avctx->time_base= c->sys->time_base;
247 if(!avctx->width){ 248 if (!avctx->width){
248 avctx->width = c->sys->width; 249 avctx->width = c->sys->width;
249 avctx->height = c->sys->height; 250 avctx->height = c->sys->height;
250 } 251 }
251 avctx->pix_fmt = c->sys->pix_fmt; 252 avctx->pix_fmt = c->sys->pix_fmt;
252 253
253 /* finding out SAR is a little bit messy */ 254 /* finding out SAR is a little bit messy */
254 vsc_pack = dv_extract_pack(frame, dv_video_control); 255 vsc_pack = dv_extract_pack(frame, dv_video_control);
255 apt = frame[4] & 0x07; 256 apt = frame[4] & 0x07;
256 is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || 257 is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
257 (!apt && (vsc_pack[2] & 0x07) == 0x07))); 258 (!apt && (vsc_pack[2] & 0x07) == 0x07)));
258 c->vst->sample_aspect_ratio = c->sys->sar[is16_9]; 259 c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
259 avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1}, 260 avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
260 c->sys->time_base); 261 c->sys->time_base);
261 size = c->sys->frame_size; 262 size = c->sys->frame_size;
262 } 263 }
279 if (!c->vst) { 280 if (!c->vst) {
280 av_free(c); 281 av_free(c);
281 return NULL; 282 return NULL;
282 } 283 }
283 284
284 c->sys = NULL; 285 c->sys = NULL;
285 c->fctx = s; 286 c->fctx = s;
286 memset(c->ast, 0, sizeof(c->ast)); 287 memset(c->ast, 0, sizeof(c->ast));
287 c->ach = 0; 288 c->ach = 0;
288 c->frames = 0; 289 c->frames = 0;
289 c->abytes = 0; 290 c->abytes = 0;
290 291
291 c->vst->codec->codec_type = CODEC_TYPE_VIDEO; 292 c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
292 c->vst->codec->codec_id = CODEC_ID_DVVIDEO; 293 c->vst->codec->codec_id = CODEC_ID_DVVIDEO;
293 c->vst->codec->bit_rate = 25000000; 294 c->vst->codec->bit_rate = 25000000;
294 c->vst->start_time = 0; 295 c->vst->start_time = 0;
295 296
296 return c; 297 return c;
297 } 298 }
298 299
299 int dv_get_packet(DVDemuxContext *c, AVPacket *pkt) 300 int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
300 { 301 {
301 int size = -1; 302 int size = -1;
302 int i; 303 int i;
303 304
304 for (i=0; i<c->ach; i++) { 305 for (i = 0; i < c->ach; i++) {
305 if (c->ast[i] && c->audio_pkt[i].size) { 306 if (c->ast[i] && c->audio_pkt[i].size) {
306 *pkt = c->audio_pkt[i]; 307 *pkt = c->audio_pkt[i];
307 c->audio_pkt[i].size = 0; 308 c->audio_pkt[i].size = 0;
308 size = pkt->size; 309 size = pkt->size;
309 break; 310 break;
326 } 327 }
327 328
328 /* Queueing audio packet */ 329 /* Queueing audio packet */
329 /* FIXME: in case of no audio/bad audio we have to do something */ 330 /* FIXME: in case of no audio/bad audio we have to do something */
330 size = dv_extract_audio_info(c, buf); 331 size = dv_extract_audio_info(c, buf);
331 for (i=0; i<c->ach; i++) { 332 for (i = 0; i < c->ach; i++) {
332 c->audio_pkt[i].size = size; 333 c->audio_pkt[i].size = size;
333 c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate; 334 c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
334 ppcm[i] = c->audio_buf[i]; 335 ppcm[i] = c->audio_buf[i];
335 } 336 }
336 dv_extract_audio(buf, ppcm, c->sys); 337 dv_extract_audio(buf, ppcm, c->sys);
337 c->abytes += size; 338 c->abytes += size;
338 339
339 /* We work with 720p frames split in half, thus even frames have 340 /* We work with 720p frames split in half, thus even frames have
340 * channels 0,1 and odd 2,3. */ 341 * channels 0,1 and odd 2,3. */
341 if (c->sys->height == 720) { 342 if (c->sys->height == 720) {
342 if (buf[1]&0x0C) 343 if (buf[1] & 0x0C)
343 c->audio_pkt[2].size = c->audio_pkt[3].size = 0; 344 c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
344 else 345 else
345 c->audio_pkt[0].size = c->audio_pkt[1].size = 0; 346 c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
346 } 347 }
347 348
348 /* Now it's time to return video packet */ 349 /* Now it's time to return video packet */
349 size = dv_extract_video_info(c, buf); 350 size = dv_extract_video_info(c, buf);
350 av_init_packet(pkt); 351 av_init_packet(pkt);
351 pkt->data = buf; 352 pkt->data = buf;
352 pkt->size = size; 353 pkt->size = size;
353 pkt->flags |= PKT_FLAG_KEY; 354 pkt->flags |= PKT_FLAG_KEY;
354 pkt->stream_index = c->vst->id; 355 pkt->stream_index = c->vst->id;
355 pkt->pts = c->frames; 356 pkt->pts = c->frames;
356 357
357 c->frames++; 358 c->frames++;
358 359
359 return size; 360 return size;
360 } 361 }
440 } 441 }
441 442
442 static int dv_read_seek(AVFormatContext *s, int stream_index, 443 static int dv_read_seek(AVFormatContext *s, int stream_index,
443 int64_t timestamp, int flags) 444 int64_t timestamp, int flags)
444 { 445 {
445 RawDVContext *r = s->priv_data; 446 RawDVContext *r = s->priv_data;
446 DVDemuxContext *c = r->dv_demux; 447 DVDemuxContext *c = r->dv_demux;
447 int64_t offset= dv_frame_offset(s, c, timestamp, flags); 448 int64_t offset = dv_frame_offset(s, c, timestamp, flags);
448 449
449 dv_offset_reset(c, offset / c->sys->frame_size); 450 dv_offset_reset(c, offset / c->sys->frame_size);
450 451
451 offset = url_fseek(s->pb, offset, SEEK_SET); 452 offset = url_fseek(s->pb, offset, SEEK_SET);
452 return (offset < 0)?offset:0; 453 return (offset < 0) ? offset : 0;
453 } 454 }
454 455
455 static int dv_read_close(AVFormatContext *s) 456 static int dv_read_close(AVFormatContext *s)
456 { 457 {
457 RawDVContext *c = s->priv_data; 458 RawDVContext *c = s->priv_data;