int lp_libdv_in::init(int audio_buffer_len, int nb_audio_channels) { int i; pv_decoder = dv_decoder_new(TRUE, FALSE, FALSE); if(pv_decoder == 0){ std::cerr << "lp_libdv_in::" << __FUNCTION__ << ": cannot initialise the dv decoder\n"; return -1; } // we init buffers here - make this else later ?? -> Ok for file buffer // File buffer - is the size OK ? pv_file_buf_size = 150000; pv_file_buffer = (uint8_t*)malloc(pv_file_buf_size * sizeof(uint8_t)); if(pv_file_buffer == 0){ std::cerr << "lp_libdv_in::" << __FUNCTION__ << ": cannot allocate memory for file buffer\n"; return -1; } // Audio for(i=0; i<4; i++){ pv_dv_audio_buffers[i] = (int16_t*)malloc(DV_AUDIO_MAX_SAMPLES * sizeof(int16_t)); if(pv_dv_audio_buffers[i] == 0){ std::cerr << "lp_libdv_in::" << __FUNCTION__ << ": cannot allocate memory for dv audio buffers\n"; return -1; } } // Alloc audio ready data buffer for 2 total data length (for storage) pv_audio_ready_buffer = new int16_t[audio_buffer_len * 4 * 2]; if(pv_audio_ready_buffer == 0){ std::cerr << "lp_libdv_in::" << __FUNCTION__ << ": cannot allocate memory for pv_audio_ready_buffer\n"; return -1; } pv_audio_channels = nb_audio_channels; pv_lp_audio_buffer.init(audio_buffer_len * 4 * 4); // Debug - infos if(dv_format_wide(pv_decoder)){ std::cerr << "lp_libdv_in::" << __FUNCTION__ << ": Format video 16/9\n"; } if(dv_format_normal(pv_decoder)){ std::cerr << "lp_libdv_in::" << __FUNCTION__ << ": Format video 4/3\n"; } return 0; }
static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) { GstDVDec *dvdec; guint8 *inframe; guint8 *outframe_ptrs[3]; gint outframe_pitches[3]; GstMapInfo map; GstVideoFrame frame; GstBuffer *outbuf; GstFlowReturn ret = GST_FLOW_OK; guint length; guint64 cstart = GST_CLOCK_TIME_NONE, cstop = GST_CLOCK_TIME_NONE; gboolean PAL, wide; dvdec = GST_DVDEC (parent); gst_buffer_map (buf, &map, GST_MAP_READ); inframe = map.data; /* buffer should be at least the size of one NTSC frame, this should * be enough to decode the header. */ if (G_UNLIKELY (map.size < NTSC_BUFFER)) goto wrong_size; /* preliminary dropping. unref and return if outside of configured segment */ if ((dvdec->segment.format == GST_FORMAT_TIME) && (!(gst_segment_clip (&dvdec->segment, GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf), &cstart, &cstop)))) goto dropping; if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0)) goto parse_header_error; /* get size */ PAL = dv_system_50_fields (dvdec->decoder); wide = dv_format_wide (dvdec->decoder); /* check the buffer is of right size after we know if we are * dealing with PAL or NTSC */ length = (PAL ? PAL_BUFFER : NTSC_BUFFER); if (G_UNLIKELY (map.size < length)) goto wrong_size; dv_parse_packs (dvdec->decoder, inframe); if (dvdec->video_offset % dvdec->drop_factor != 0) goto skip; /* renegotiate on change */ if (PAL != dvdec->PAL || wide != dvdec->wide) { dvdec->src_negotiated = FALSE; dvdec->PAL = PAL; dvdec->wide = wide; } dvdec->height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT); dvdec->interlaced = !dv_is_progressive (dvdec->decoder); /* negotiate if not done yet */ if (!dvdec->src_negotiated) { if (!gst_dvdec_src_negotiate (dvdec)) goto not_negotiated; } if (gst_pad_check_reconfigure (dvdec->srcpad)) { GstCaps *caps; caps = gst_pad_get_current_caps (dvdec->srcpad); if (!caps) goto not_negotiated; gst_dvdec_negotiate_pool (dvdec, caps, &dvdec->vinfo); gst_caps_unref (caps); } if (dvdec->need_segment) { gst_pad_push_event (dvdec->srcpad, gst_event_new_segment (&dvdec->segment)); dvdec->need_segment = FALSE; } ret = gst_buffer_pool_acquire_buffer (dvdec->pool, &outbuf, NULL); if (G_UNLIKELY (ret != GST_FLOW_OK)) goto no_buffer; gst_video_frame_map (&frame, &dvdec->vinfo, outbuf, GST_MAP_WRITE); outframe_ptrs[0] = GST_VIDEO_FRAME_COMP_DATA (&frame, 0); outframe_pitches[0] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0); /* the rest only matters for YUY2 */ if (dvdec->bpp < 3) { outframe_ptrs[1] = GST_VIDEO_FRAME_COMP_DATA (&frame, 1); outframe_ptrs[2] = GST_VIDEO_FRAME_COMP_DATA (&frame, 2); outframe_pitches[1] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1); outframe_pitches[2] = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 2); } GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer"); dv_decode_full_frame (dvdec->decoder, inframe, e_dv_color_yuv, outframe_ptrs, outframe_pitches); gst_video_frame_unmap (&frame); GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf); GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf); /* FIXME : Compute values when using non-TIME segments, * but for the moment make sure we at least don't set bogus values */ if (GST_CLOCK_TIME_IS_VALID (cstart)) { GST_BUFFER_TIMESTAMP (outbuf) = cstart; if (GST_CLOCK_TIME_IS_VALID (cstop)) GST_BUFFER_DURATION (outbuf) = cstop - cstart; } ret = gst_pad_push (dvdec->srcpad, outbuf); skip: dvdec->video_offset++; done: gst_buffer_unmap (buf, &map); gst_buffer_unref (buf); return ret; /* ERRORS */ wrong_size: { GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), ("Input buffer too small")); ret = GST_FLOW_ERROR; goto done; } parse_header_error: { GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), ("Error parsing DV header")); ret = GST_FLOW_ERROR; goto done; } not_negotiated: { GST_DEBUG_OBJECT (dvdec, "could not negotiate output"); ret = GST_FLOW_NOT_NEGOTIATED; goto done; } no_buffer: { GST_DEBUG_OBJECT (dvdec, "could not allocate buffer"); goto done; } dropping: { GST_DEBUG_OBJECT (dvdec, "dropping buffer since it's out of the configured segment"); goto done; } }
/** Get the frame aspect ratio. Indicates whether frame aspect ration is normal (4:3) or wide (16:9). \return true if the frame is wide (16:9), false if unknown or normal. */ bool DVFrame::IsWide( void ) { return dv_format_wide( decoder ) > 0; }
int main( int argc, char **argv) { int infile = 0; unsigned char dv_buffer[144000]; unsigned char video_buffer[720 * 576 * 3]; int16_t *audio_bufs[4]; dv_decoder_t *decoder = NULL; dv_encoder_t *encoder = NULL; int pitches[3]; unsigned char *pixels[3]; int i = 0, j; int isPAL = FALSE; pitches[0] = 720 * 2; pixels[0] = video_buffer; for(i = 0; i < 4; i++) { audio_bufs[i] = malloc(DV_AUDIO_MAX_SAMPLES*sizeof(int16_t)); } /* assume NTSC for now, switch to PAL later if needed */ decoder = dv_decoder_new(FALSE, FALSE, FALSE); encoder = dv_encoder_new(FALSE, FALSE, FALSE); decoder->quality = DV_QUALITY_BEST; encoder->vlc_encode_passes = 3; encoder->static_qno = 0; encoder->force_dct = DV_DCT_AUTO; i = 0; while (read_frame(stdin, dv_buffer, &isPAL)) { dv_parse_header(decoder, dv_buffer); if (isPAL != encoder->isPAL && isPAL == TRUE) { decoder->clamp_luma = FALSE; decoder->clamp_chroma = FALSE; encoder->clamp_luma = FALSE; encoder->clamp_chroma = FALSE; dv_reconfigure(FALSE, FALSE); } else if (isPAL != encoder->isPAL) { decoder->clamp_luma = TRUE; decoder->clamp_chroma = TRUE; decoder->add_ntsc_setup = TRUE; encoder->clamp_luma = TRUE; encoder->clamp_chroma = TRUE; encoder->rem_ntsc_setup = TRUE; dv_reconfigure(TRUE, TRUE); } encoder->isPAL = isPAL; encoder->is16x9 = (dv_format_wide(decoder)>0); dv_decode_full_audio(decoder, dv_buffer, audio_bufs); for (j = 0; j < TIMES; j++) { dv_decode_full_frame(decoder, dv_buffer, e_dv_color_yuv, pixels, pitches); dv_encode_full_frame(encoder, pixels, e_dv_color_yuv, dv_buffer); } dv_encode_full_audio(encoder, audio_bufs, 2, 48000, dv_buffer); fwrite(dv_buffer, 1, (isPAL ? 144000 : 120000), stdout); } close(infile); for(i=0; i < 4; i++) free(audio_bufs[i]); dv_decoder_free(decoder); dv_encoder_free(encoder); return 0; }
static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buf) { GstDVDec *dvdec; guint8 *inframe; guint8 *outframe; guint8 *outframe_ptrs[3]; gint outframe_pitches[3]; GstBuffer *outbuf; GstFlowReturn ret = GST_FLOW_OK; guint length; gint64 cstart, cstop; gboolean PAL, wide; dvdec = GST_DVDEC (gst_pad_get_parent (pad)); inframe = GST_BUFFER_DATA (buf); /* buffer should be at least the size of one NTSC frame, this should * be enough to decode the header. */ if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < NTSC_BUFFER)) goto wrong_size; /* preliminary dropping. unref and return if outside of configured segment */ if ((dvdec->segment->format == GST_FORMAT_TIME) && (!(gst_segment_clip (dvdec->segment, GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf), &cstart, &cstop)))) goto dropping; if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0)) goto parse_header_error; /* get size */ PAL = dv_system_50_fields (dvdec->decoder); wide = dv_format_wide (dvdec->decoder); /* check the buffer is of right size after we know if we are * dealing with PAL or NTSC */ length = (PAL ? PAL_BUFFER : NTSC_BUFFER); if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < length)) goto wrong_size; dv_parse_packs (dvdec->decoder, inframe); if (dvdec->video_offset % dvdec->drop_factor != 0) goto skip; /* renegotiate on change */ if (PAL != dvdec->PAL || wide != dvdec->wide) { dvdec->src_negotiated = FALSE; dvdec->PAL = PAL; dvdec->wide = wide; } dvdec->height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT); /* negotiate if not done yet */ if (!dvdec->src_negotiated) { if (!gst_dvdec_src_negotiate (dvdec)) goto not_negotiated; } ret = gst_pad_alloc_buffer_and_set_caps (dvdec->srcpad, 0, (720 * dvdec->height) * dvdec->bpp, GST_PAD_CAPS (dvdec->srcpad), &outbuf); if (G_UNLIKELY (ret != GST_FLOW_OK)) goto no_buffer; outframe = GST_BUFFER_DATA (outbuf); outframe_ptrs[0] = outframe; outframe_pitches[0] = 720 * dvdec->bpp; /* the rest only matters for YUY2 */ if (dvdec->bpp < 3) { outframe_ptrs[1] = outframe_ptrs[0] + 720 * dvdec->height; outframe_ptrs[2] = outframe_ptrs[1] + 360 * dvdec->height; outframe_pitches[1] = dvdec->height / 2; outframe_pitches[2] = outframe_pitches[1]; } GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer"); dv_decode_full_frame (dvdec->decoder, inframe, e_dv_color_yuv, outframe_ptrs, outframe_pitches); GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf); GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf); GST_BUFFER_TIMESTAMP (outbuf) = cstart; GST_BUFFER_DURATION (outbuf) = cstop - cstart; ret = gst_pad_push (dvdec->srcpad, outbuf); skip: dvdec->video_offset++; done: gst_buffer_unref (buf); gst_object_unref (dvdec); return ret; /* ERRORS */ wrong_size: { GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), ("Input buffer too small")); ret = GST_FLOW_ERROR; goto done; } parse_header_error: { GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), ("Error parsing DV header")); ret = GST_FLOW_ERROR; goto done; } not_negotiated: { GST_DEBUG_OBJECT (dvdec, "could not negotiate output"); ret = GST_FLOW_NOT_NEGOTIATED; goto done; } no_buffer: { GST_DEBUG_OBJECT (dvdec, "could not allocate buffer"); goto done; } dropping: { GST_DEBUG_OBJECT (dvdec, "dropping buffer since it's out of the configured segment"); goto done; } }