2 * Grabbing algorithm is from dvgrab
23 static int dv_initted = 0;
24 static pthread_mutex_t dv_lock;
28 dv_t *dv = (dv_t *)calloc(1, sizeof(dv_t));
31 pthread_mutexattr_t attr;
34 pthread_mutexattr_init(&attr);
35 pthread_mutex_init(&dv_lock, &attr);
38 dv->decoder = dv_decoder_new(0, 0, 0);
39 dv_set_error_log (dv->decoder, 0);
40 dv->decoder->quality = DV_QUALITY_BEST;
41 dv->decoder->prev_frame_decoded = 0;
47 int dv_delete(dv_t *dv)
52 dv_decoder_free( dv->decoder );
60 for(i = 0; i < 4; i++)
61 free(dv->temp_audio[i]);
66 dv_encoder_free( dv->encoder );
73 // Decodes BC_YUV422 only
77 int dv_read_video(dv_t *dv,
78 unsigned char **output_rows,
85 int use_temp = color_model != BC_YUV422;
86 unsigned char *pixels[3];
88 //printf("dv_read_video 1 %d\n", color_model);
89 pthread_mutex_lock(&dv_lock);
101 if(data[0] != 0x1f) return 1;
103 pitches[0] = DV_WIDTH * 2;
109 dv_parse_header(dv->decoder, data);
113 //printf("dv_read_video 1\n");
114 pixels[0] = output_rows[0];
115 dv_decode_full_frame(dv->decoder,
120 //printf("dv_read_video 2\n");
124 unsigned char *temp_rows[DV_HEIGHT];
126 dv->temp_video = (unsigned char *)calloc(1, DV_WIDTH * DV_HEIGHT * 2);
128 for(i = 0; i < DV_HEIGHT; i++)
130 temp_rows[i] = dv->temp_video + i * DV_WIDTH * 2;
133 pixels[0] = dv->temp_video;
134 //printf("dv_read_video 3 %p\n", data);
135 dv_decode_full_frame(dv->decoder,
140 //printf("dv_read_video 4\n");
142 BC_CModels::transfer(output_rows,
164 dv->decoder->prev_frame_decoded = 1;
165 pthread_mutex_unlock(&dv_lock);
174 int dv_read_audio(dv_t *dv,
175 unsigned char *samples,
182 short *samples_int16 = (short*)samples;
184 if(channels > 4) channels = 4;
186 // For some reason someone had problems with libdv's maxmimum audio samples
187 #define MAX_AUDIO_SAMPLES 2048
188 if(!dv->temp_audio[0])
190 for(i = 0; i < 4; i++)
191 dv->temp_audio[i] = (int16_t*)calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES);
195 case DV_PAL_SIZE: break;
196 case DV_NTSC_SIZE: break;
200 if(data[0] != 0x1f) return 0;
202 dv_parse_header(dv->decoder, data);
203 dv_decode_full_audio(dv->decoder, data, dv->temp_audio);
204 samples_read = dv->decoder->audio->samples_this_frame;
206 for(i = 0; i < channels; i++)
208 for(j = 0; j < samples_read; j++)
210 samples_int16[i + j * channels] = dv->temp_audio[i][j];
211 if(samples_int16[i + j * channels] == -0x8000)
212 samples_int16[i + j * channels] = 0;
228 // Encodes BC_YUV422 only
230 void dv_write_video(dv_t *dv,
232 unsigned char **input_rows,
236 int encode_dv_colormodel = 0;
240 dv->encoder = dv_encoder_new(
246 switch( color_model )
249 encode_dv_colormodel = e_dv_color_yuv;
252 encode_dv_colormodel = e_dv_color_rgb;
258 dv->encoder->is16x9 = 0;
259 dv->encoder->vlc_encode_passes = 3;
260 dv->encoder->static_qno = 0;
261 dv->encoder->force_dct = DV_DCT_AUTO;
262 dv->encoder->isPAL = (norm == DV_PAL);
264 dv_encode_full_frame( dv->encoder,
266 (dv_color_space_t)encode_dv_colormodel,
272 int dv_write_audio(dv_t *dv,
274 unsigned char *input_samples,
285 dv->encoder = dv_encoder_new(
290 dv->encoder->isPAL = (norm == DV_PAL);
293 // Get sample count from a libdv function
294 int samples = dv_calculate_samples(dv->encoder, rate, dv->audio_frames);
297 if(!dv->temp_audio[0])
299 for(i = 0; i < 4; i++)
300 dv->temp_audio[i] = (int16_t*)calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES);
303 for(i = 0; i < channels; i++)
305 short *temp_audio = dv->temp_audio[i];
306 short *input_channel = (short*)input_samples + i;
307 for(j = 0; j < samples; j++)
309 temp_audio[j] = input_channel[j * channels];
314 dv_encode_full_audio(dv->encoder,