Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / quicktime / libdv.c
blob7449c57217d12865ceac09da75cc75cc820095ec
1 /*
2 * Grabbing algorithm is from dvgrab
3 */
5 #include "colormodels.h"
6 #include "libdv.h"
8 #include <pthread.h>
9 #include <stdio.h>
10 #include <string.h>
12 #ifdef USE_MMX
13 #include "mmx.h"
14 #endif
17 #define DV_WIDTH 720
18 #define DV_HEIGHT 576
21 static int dv_initted = 0;
22 static pthread_mutex_t dv_lock;
24 dv_t* dv_new()
26 dv_t *dv = calloc(1, sizeof(dv_t));
27 if(!dv_initted)
29 pthread_mutexattr_t attr;
30 dv_initted = 1;
31 // dv_init();
32 pthread_mutexattr_init(&attr);
33 pthread_mutex_init(&dv_lock, &attr);
36 dv->decoder = dv_decoder_new(0, 0, 0);
37 dv_set_error_log (dv->decoder, 0);
38 dv->decoder->quality = DV_QUALITY_BEST;
39 dv->decoder->prev_frame_decoded = 0;
40 dv->use_mmx = 1;
41 return dv;
45 int dv_delete(dv_t *dv)
47 int i;
48 if(dv->decoder)
50 dv_decoder_free( dv->decoder );
53 if(dv->temp_video)
54 free(dv->temp_video);
56 if(dv->temp_audio[0])
58 for(i = 0; i < 4; i++)
59 free(dv->temp_audio[i]);
62 if(dv->encoder)
64 dv_encoder_free( dv->encoder );
67 free(dv);
68 return 0;
71 // Decodes BC_YUV422 only
75 int dv_read_video(dv_t *dv,
76 unsigned char **output_rows,
77 unsigned char *data,
78 long bytes,
79 int color_model)
81 int dif = 0;
82 int lost_coeffs = 0;
83 long offset = 0;
84 int isPAL = 0;
85 int is61834 = 0;
86 int numDIFseq;
87 int ds;
88 int i, v, b, m;
89 dv_block_t *bl;
90 long mb_offset;
91 dv_sample_t sampling;
92 dv_macroblock_t *mb;
93 int pixel_size;
94 int pitches[3];
95 int use_temp = color_model != BC_YUV422;
96 unsigned char *pixels[3];
98 //printf("dv_read_video 1 %d\n", color_model);
99 pthread_mutex_lock(&dv_lock);
100 switch(bytes)
102 case DV_PAL_SIZE:
103 break;
104 case DV_NTSC_SIZE:
105 break;
106 default:
107 return 1;
108 break;
111 if(data[0] != 0x1f) return 1;
113 pitches[0] = DV_WIDTH * 2;
114 pitches[1] = 0;
115 pitches[2] = 0;
116 pixels[1] = 0;
117 pixels[2] = 0;
119 dv_parse_header(dv->decoder, data);
121 if(!use_temp)
123 //printf("dv_read_video 1\n");
124 pixels[0] = output_rows[0];
125 dv_decode_full_frame(dv->decoder,
126 data,
127 e_dv_color_yuv,
128 output_rows,
129 pitches);
130 //printf("dv_read_video 2\n");
132 else
134 unsigned char *temp_rows[DV_HEIGHT];
135 if(!dv->temp_video)
136 dv->temp_video = calloc(1, DV_WIDTH * DV_HEIGHT * 2);
138 for(i = 0; i < DV_HEIGHT; i++)
140 temp_rows[i] = dv->temp_video + i * DV_WIDTH * 2;
143 pixels[0] = dv->temp_video;
144 //printf("dv_read_video 3 %p\n", data);
145 dv_decode_full_frame(dv->decoder,
146 data,
147 e_dv_color_yuv,
148 pixels,
149 pitches);
150 //printf("dv_read_video 4\n");
152 cmodel_transfer(output_rows,
153 temp_rows,
154 output_rows[0],
155 output_rows[1],
156 output_rows[2],
162 DV_WIDTH,
163 dv->decoder->height,
166 DV_WIDTH,
167 dv->decoder->height,
168 BC_YUV422,
169 color_model,
171 DV_WIDTH,
172 DV_WIDTH);
174 dv->decoder->prev_frame_decoded = 1;
175 pthread_mutex_unlock(&dv_lock);
176 return 0;
184 int dv_read_audio(dv_t *dv,
185 unsigned char *samples,
186 unsigned char *data,
187 long size,
188 int channels,
189 int bits)
191 long current_position;
192 int norm;
193 int i, j;
194 int audio_bytes;
195 short *samples_int16 = (short*)samples;
196 int samples_read;
197 if(channels > 4) channels = 4;
199 // For some reason someone had problems with libdv's maxmimum audio samples
200 #define MAX_AUDIO_SAMPLES 2048
201 if(!dv->temp_audio[0])
203 for(i = 0; i < 4; i++)
204 dv->temp_audio[i] = calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES);
207 switch(size)
209 case DV_PAL_SIZE:
210 norm = DV_PAL;
211 break;
212 case DV_NTSC_SIZE:
213 norm = DV_NTSC;
214 break;
215 default:
216 return 0;
217 break;
220 if(data[0] != 0x1f) return 0;
222 dv_parse_header(dv->decoder, data);
223 dv_decode_full_audio(dv->decoder, data, dv->temp_audio);
224 samples_read = dv->decoder->audio->samples_this_frame;
226 for(i = 0; i < channels; i++)
228 for(j = 0; j < samples_read; j++)
230 samples_int16[i + j * channels] = dv->temp_audio[i][j];
231 if(samples_int16[i + j * channels] == -0x8000)
232 samples_int16[i + j * channels] = 0;
241 return samples_read;
248 // Encodes BC_YUV422 only
250 void dv_write_video(dv_t *dv,
251 unsigned char *data,
252 unsigned char **input_rows,
253 int color_model,
254 int norm)
256 dv_color_space_t encode_dv_colormodel = 0;
258 if(!dv->encoder)
260 dv->encoder = dv_encoder_new(
266 switch( color_model )
268 case BC_YUV422:
269 encode_dv_colormodel = e_dv_color_yuv;
270 break;
271 case BC_RGB888:
272 encode_dv_colormodel = e_dv_color_rgb;
273 break;
274 default:
275 return;
276 break;
278 dv->encoder->is16x9 = 0;
279 dv->encoder->vlc_encode_passes = 3;
280 dv->encoder->static_qno = 0;
281 dv->encoder->force_dct = DV_DCT_AUTO;
282 dv->encoder->isPAL = (norm == DV_PAL);
284 dv_encode_full_frame( dv->encoder,
285 input_rows,
286 encode_dv_colormodel,
287 data );
292 int dv_write_audio(dv_t *dv,
293 unsigned char *data,
294 unsigned char *input_samples,
295 int max_samples,
296 int channels,
297 int bits,
298 int rate,
299 int norm)
301 int i, j;
303 if(!dv->encoder)
305 dv->encoder = dv_encoder_new(
308 0 );
310 dv->encoder->isPAL = (norm == DV_PAL);
313 // Get sample count from a libdv function
314 int samples = dv_calculate_samples(dv->encoder, rate, dv->audio_frames);
315 dv->audio_frames++;
317 if(!dv->temp_audio[0])
319 for(i = 0; i < 4; i++)
320 dv->temp_audio[i] = calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES);
323 for(i = 0; i < channels; i++)
325 short *temp_audio = dv->temp_audio[i];
326 short *input_channel = (short*)input_samples + i;
327 for(j = 0; j < samples; j++)
329 temp_audio[j] = input_channel[j * channels];
334 dv_encode_full_audio(dv->encoder,
335 dv->temp_audio,
336 channels,
337 rate,
338 data);
339 return samples;