2 * Grabbing algorithm is from dvgrab
5 #include "colormodels.h"
21 static int dv_initted
= 0;
22 static pthread_mutex_t dv_lock
;
26 dv_t
*dv
= 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
,
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
);
111 if(data
[0] != 0x1f) return 1;
113 pitches
[0] = DV_WIDTH
* 2;
119 dv_parse_header(dv
->decoder
, data
);
123 //printf("dv_read_video 1\n");
124 pixels
[0] = output_rows
[0];
125 dv_decode_full_frame(dv
->decoder
,
130 //printf("dv_read_video 2\n");
134 unsigned char *temp_rows
[DV_HEIGHT
];
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
,
150 //printf("dv_read_video 4\n");
152 cmodel_transfer(output_rows
,
174 dv
->decoder
->prev_frame_decoded
= 1;
175 pthread_mutex_unlock(&dv_lock
);
184 int dv_read_audio(dv_t
*dv
,
185 unsigned char *samples
,
191 long current_position
;
195 short *samples_int16
= (short*)samples
;
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
);
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;
248 // Encodes BC_YUV422 only
250 void dv_write_video(dv_t
*dv
,
252 unsigned char **input_rows
,
256 dv_color_space_t encode_dv_colormodel
= 0;
260 dv
->encoder
= dv_encoder_new(
266 switch( color_model
)
269 encode_dv_colormodel
= e_dv_color_yuv
;
272 encode_dv_colormodel
= e_dv_color_rgb
;
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
,
286 encode_dv_colormodel
,
292 int dv_write_audio(dv_t
*dv
,
294 unsigned char *input_samples
,
305 dv
->encoder
= dv_encoder_new(
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
);
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
,