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
->decoder
->quality
= dv
->decoder
->video
->quality
;
38 dv
->decoder
->prev_frame_decoded
= 0;
44 int dv_delete(dv_t
*dv
)
49 dv_decoder_free( dv
->decoder
);
57 for(i
= 0; i
< 4; i
++)
58 free(dv
->temp_audio
[i
]);
63 dv_encoder_free( dv
->encoder
);
70 // Decodes BC_YUV422 only
74 int dv_read_video(dv_t
*dv
,
75 unsigned char **output_rows
,
94 int use_temp
= color_model
!= BC_YUV422
;
95 unsigned char *pixels
[3];
97 //printf("dv_read_video 1 %d\n", color_model);
98 pthread_mutex_lock(&dv_lock
);
110 if(data
[0] != 0x1f) return 1;
112 pitches
[0] = DV_WIDTH
* 2;
118 dv_parse_header(dv
->decoder
, data
);
122 //printf("dv_read_video 1\n");
123 pixels
[0] = output_rows
[0];
124 dv_decode_full_frame(dv
->decoder
,
129 //printf("dv_read_video 2\n");
133 unsigned char *temp_rows
[DV_HEIGHT
];
135 dv
->temp_video
= calloc(1, DV_WIDTH
* DV_HEIGHT
* 2);
137 for(i
= 0; i
< DV_HEIGHT
; i
++)
139 temp_rows
[i
] = dv
->temp_video
+ i
* DV_WIDTH
* 2;
142 pixels
[0] = dv
->temp_video
;
143 //printf("dv_read_video 3 %p\n", data);
144 dv_decode_full_frame(dv
->decoder
,
149 //printf("dv_read_video 4\n");
151 cmodel_transfer(output_rows
,
173 dv
->decoder
->prev_frame_decoded
= 1;
174 pthread_mutex_unlock(&dv_lock
);
183 int dv_read_audio(dv_t
*dv
,
184 unsigned char *samples
,
190 long current_position
;
194 short *samples_int16
= (short*)samples
;
196 if(channels
> 4) channels
= 4;
198 // For some reason someone had problems with libdv's maxmimum audio samples
199 #define MAX_AUDIO_SAMPLES 2048
200 if(!dv
->temp_audio
[0])
202 for(i
= 0; i
< 4; i
++)
203 dv
->temp_audio
[i
] = calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES
);
219 if(data
[0] != 0x1f) return 0;
221 dv_parse_header(dv
->decoder
, data
);
222 dv_decode_full_audio(dv
->decoder
, data
, dv
->temp_audio
);
223 samples_read
= dv
->decoder
->audio
->samples_this_frame
;
225 for(i
= 0; i
< channels
; i
++)
227 for(j
= 0; j
< samples_read
; j
++)
229 samples_int16
[i
+ j
* channels
] = dv
->temp_audio
[i
][j
];
230 if(samples_int16
[i
+ j
* channels
] == -0x8000)
231 samples_int16
[i
+ j
* channels
] = 0;
247 // Encodes BC_YUV422 only
249 void dv_write_video(dv_t
*dv
,
251 unsigned char **input_rows
,
255 dv_color_space_t encode_dv_colormodel
= 0;
259 dv
->encoder
= dv_encoder_new(
265 switch( color_model
)
268 encode_dv_colormodel
= e_dv_color_yuv
;
271 encode_dv_colormodel
= e_dv_color_rgb
;
277 dv
->encoder
->is16x9
= 0;
278 dv
->encoder
->vlc_encode_passes
= 3;
279 dv
->encoder
->static_qno
= 0;
280 dv
->encoder
->force_dct
= DV_DCT_AUTO
;
281 dv
->encoder
->isPAL
= (norm
== DV_PAL
);
283 dv_encode_full_frame( dv
->encoder
,
285 encode_dv_colormodel
,
291 int dv_write_audio(dv_t
*dv
,
293 unsigned char *input_samples
,
304 dv
->encoder
= dv_encoder_new(
309 dv
->encoder
->isPAL
= (norm
== DV_PAL
);
312 // Get sample count from a libdv function
313 int samples
= dv_calculate_samples(dv
->encoder
, rate
, dv
->audio_frames
);
316 if(!dv
->temp_audio
[0])
318 for(i
= 0; i
< 4; i
++)
319 dv
->temp_audio
[i
] = calloc(1, sizeof(int16_t) * MAX_AUDIO_SAMPLES
);
322 for(i
= 0; i
< channels
; i
++)
324 short *temp_audio
= dv
->temp_audio
[i
];
325 short *input_channel
= (short*)input_samples
+ i
;
326 for(j
= 0; j
< samples
; j
++)
328 temp_audio
[j
] = input_channel
[j
* channels
];
333 dv_encode_full_audio(dv
->encoder
,