| 1 | /* |
| 2 | * recode.c |
| 3 | * |
| 4 | * Copyright (C) Dan Dennedy, Peter Schlaile |
| 5 | * |
| 6 | * This file is part of libdv, a free DV (IEC 61834/SMPTE 314M) |
| 7 | * codec. |
| 8 | * |
| 9 | * libdv is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU Lesser Public License as published by |
| 11 | * the Free Software Foundation; either version 2.1, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | * libdv is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser Public License |
| 20 | * along with libdv; see the file COPYING. If not, write to |
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * The libdv homepage is http://libdv.sourceforge.net/. |
| 24 | */ |
| 25 | |
| 26 | #include <libdv/dv.h> |
| 27 | #include <libdv/dv_types.h> |
| 28 | #include <stdio.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #define TIMES 5 |
| 35 | |
| 36 | int read_frame(FILE* in_vid, unsigned char* frame_buf, int * isPAL) |
| 37 | { |
| 38 | if (fread(frame_buf, 1, 120000, in_vid) != 120000) { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | *isPAL = (frame_buf[3] & 0x80); |
| 43 | |
| 44 | if (*isPAL) { |
| 45 | if (fread(frame_buf + 120000, 1, 144000 - 120000, in_vid) != |
| 46 | 144000 - 120000) { |
| 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | int main( int argc, char **argv) |
| 54 | { |
| 55 | int infile = 0; |
| 56 | unsigned char dv_buffer[144000]; |
| 57 | unsigned char video_buffer[720 * 576 * 3]; |
| 58 | int16_t *audio_bufs[4]; |
| 59 | dv_decoder_t *decoder = NULL; |
| 60 | dv_encoder_t *encoder = NULL; |
| 61 | int pitches[3]; |
| 62 | unsigned char *pixels[3]; |
| 63 | int i = 0, j; |
| 64 | int isPAL = FALSE; |
| 65 | |
| 66 | pitches[0] = 720 * 2; |
| 67 | pixels[0] = video_buffer; |
| 68 | |
| 69 | for(i = 0; i < 4; i++) { |
| 70 | audio_bufs[i] = malloc(DV_AUDIO_MAX_SAMPLES*sizeof(int16_t)); |
| 71 | } |
| 72 | |
| 73 | /* assume NTSC for now, switch to PAL later if needed */ |
| 74 | decoder = dv_decoder_new(FALSE, FALSE, FALSE); |
| 75 | encoder = dv_encoder_new(FALSE, FALSE, FALSE); |
| 76 | |
| 77 | decoder->quality = DV_QUALITY_BEST; |
| 78 | encoder->vlc_encode_passes = 3; |
| 79 | encoder->static_qno = 0; |
| 80 | encoder->force_dct = DV_DCT_AUTO; |
| 81 | |
| 82 | i = 0; |
| 83 | while (read_frame(stdin, dv_buffer, &isPAL)) { |
| 84 | dv_parse_header(decoder, dv_buffer); |
| 85 | if (isPAL != encoder->isPAL && isPAL == TRUE) { |
| 86 | decoder->clamp_luma = FALSE; |
| 87 | decoder->clamp_chroma = FALSE; |
| 88 | encoder->clamp_luma = FALSE; |
| 89 | encoder->clamp_chroma = FALSE; |
| 90 | dv_reconfigure(FALSE, FALSE); |
| 91 | } else if (isPAL != encoder->isPAL) { |
| 92 | decoder->clamp_luma = TRUE; |
| 93 | decoder->clamp_chroma = TRUE; |
| 94 | decoder->add_ntsc_setup = TRUE; |
| 95 | encoder->clamp_luma = TRUE; |
| 96 | encoder->clamp_chroma = TRUE; |
| 97 | encoder->rem_ntsc_setup = TRUE; |
| 98 | dv_reconfigure(TRUE, TRUE); |
| 99 | } |
| 100 | encoder->isPAL = isPAL; |
| 101 | encoder->is16x9 = (dv_format_wide(decoder)>0); |
| 102 | dv_decode_full_audio(decoder, dv_buffer, audio_bufs); |
| 103 | for (j = 0; j < TIMES; j++) { |
| 104 | dv_decode_full_frame(decoder, dv_buffer, e_dv_color_yuv, |
| 105 | pixels, pitches); |
| 106 | |
| 107 | dv_encode_full_frame(encoder, pixels, e_dv_color_yuv, dv_buffer); |
| 108 | } |
| 109 | dv_encode_full_audio(encoder, audio_bufs, 2, 48000, dv_buffer); |
| 110 | fwrite(dv_buffer, 1, (isPAL ? 144000 : 120000), stdout); |
| 111 | } |
| 112 | |
| 113 | close(infile); |
| 114 | |
| 115 | for(i=0; i < 4; i++) free(audio_bufs[i]); |
| 116 | dv_decoder_free(decoder); |
| 117 | dv_encoder_free(encoder); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | |
| 123 |