2 * Grabbing algorithm is from dvgrab
21 static int dv_initted = 0;
22 static pthread_mutex_t dv_lock;
26 dv_t *dv = (dv_t *)calloc(1, sizeof(dv_t));
29 pthread_mutexattr_t attr;
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;
45 int dv_delete(dv_t *dv)
50 dv_decoder_free( dv->decoder );
58 for(i = 0; i < 4; i++)
59 free(dv->temp_audio[i]);
64 dv_encoder_free( dv->encoder );
71 // Decodes BC_YUV422 only
75 int dv_read_video(dv_t *dv,
76 unsigned char **output_rows,
83 int use_temp = color_model != BC_YUV422;
84 unsigned char *pixels[3];
86 //printf("dv_read_video 1 %d\n", color_model);
87 pthread_mutex_lock(&dv_lock);
99 if(data[0] != 0x1f) return 1;
101 pitches[0] = DV_WIDTH * 2;
107 dv_parse_header(dv->decoder, data);
111 //printf("dv_read_video 1\n");
112 pixels[0] = output_rows[0];
113 dv_decode_full_frame(dv->decoder,
118 //printf("dv_read_video 2\n");
122 unsigned char *temp_rows[DV_HEIGHT];
124 dv->temp_video = (unsigned char *)calloc(1, DV_WIDTH * DV_HEIGHT * 2);
126 for(i = 0; i < DV_HEIGHT; i++)
128 temp_rows[i] = dv->temp_video + i * DV_WIDTH * 2;
131 pixels[0] = dv->temp_video;
132 //printf("dv_read_video 3 %p\n", data);
133 dv_decode_full_frame(dv->decoder,
138 //printf("dv_read_video 4\n");
140 BC_CModels::transfer(output_rows,
162 dv->decoder->prev_frame_decoded = 1;
163 pthread_mutex_unlock(&dv_lock);
172 int dv_read_audio(dv_t *dv,
173 unsigned char *samples,
180 short *samples_int16 = (short*)samples;
182 if(channels > 4) channels = 4;
184 // For some reason someone had problems with libdv's maxmimum audio samples
185 #define MAX_AUDIO_SAMPLES 2048
186 if(!dv->temp_audio[0])
188 for(i = 0; i < 4; i++)
189 dv->temp_audio[i] = (int16_t*)calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES);
193 case DV_PAL_SIZE: break;
194 case DV_NTSC_SIZE: break;
198 if(data[0] != 0x1f) return 0;
200 dv_parse_header(dv->decoder, data);
201 dv_decode_full_audio(dv->decoder, data, dv->temp_audio);
202 samples_read = dv->decoder->audio->samples_this_frame;
204 for(i = 0; i < channels; i++)
206 for(j = 0; j < samples_read; j++)
208 samples_int16[i + j * channels] = dv->temp_audio[i][j];
209 if(samples_int16[i + j * channels] == -0x8000)
210 samples_int16[i + j * channels] = 0;
226 // Encodes BC_YUV422 only
228 void dv_write_video(dv_t *dv,
230 unsigned char **input_rows,
234 int encode_dv_colormodel = 0;
238 dv->encoder = dv_encoder_new(
244 switch( color_model )
247 encode_dv_colormodel = e_dv_color_yuv;
250 encode_dv_colormodel = e_dv_color_rgb;
256 dv->encoder->is16x9 = 0;
257 dv->encoder->vlc_encode_passes = 3;
258 dv->encoder->static_qno = 0;
259 dv->encoder->force_dct = DV_DCT_AUTO;
260 dv->encoder->isPAL = (norm == DV_PAL);
262 dv_encode_full_frame( dv->encoder,
264 (dv_color_space_t)encode_dv_colormodel,
270 int dv_write_audio(dv_t *dv,
272 unsigned char *input_samples,
283 dv->encoder = dv_encoder_new(
288 dv->encoder->isPAL = (norm == DV_PAL);
291 // Get sample count from a libdv function
292 int samples = dv_calculate_samples(dv->encoder, rate, dv->audio_frames);
295 if(!dv->temp_audio[0])
297 for(i = 0; i < 4; i++)
298 dv->temp_audio[i] = (int16_t*)calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES);
301 for(i = 0; i < channels; i++)
303 short *temp_audio = dv->temp_audio[i];
304 short *input_channel = (short*)input_samples + i;
305 for(j = 0; j < samples; j++)
307 temp_audio[j] = input_channel[j * channels];
312 dv_encode_full_audio(dv->encoder,