ffmpeg.c
上传用户:jxp0626
上传日期:2007-01-08
资源大小:102k
文件大小:23k
- /*
- * Basic user interface for ffmpeg system
- * Copyright (c) 2000,2001 Gerard Lantau
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <linux/videodev.h>
- #include <linux/soundcard.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <errno.h>
- #include <sys/time.h>
- #include <getopt.h>
- #include "mpegenc.h"
- static AVFormat *file_format;
- static int frame_width = 160;
- static int frame_height = 128;
- static int frame_rate = 25;
- static int bit_rate = 200000;
- static int video_disable = 0;
- static const char *video_filename, *audio_filename;
- static float recording_time = 10.0;
- static int nb_frames;
- static int gop_size = 12;
- static int intra_only = 0;
- static int audio_freq = 44100;
- static int audio_bit_rate = 64000;
- static int audio_disable = 0;
- static int audio_channels = 1;
- static int high_quality = 0;
- static long long time_start;
- #define INFMT_YUV 1
- #define INFMT_PGM 2
- static inline int pnm_space(int c)
- {
- return (c==' ' || c=='n' || c=='r' || c=='t');
- }
- static void pnm_get(FILE *f, char *str, int buf_size)
- {
- char *s;
- int c;
-
- do {
- c=fgetc(f);
- if (c=='#') {
- do {
- c=fgetc(f);
- } while (c!='n');
- c=fgetc(f);
- }
- } while (pnm_space(c));
-
- s=str;
- do {
- *s++=c;
- c=fgetc(f);
- } while (!pnm_space(c));
- *s=0;
- }
- static int file_read_picture(AVEncodeContext *s,
- UINT8 *picture[3],
- int width, int height,
- int picture_number)
- {
- FILE *f;
- char buf[1024], buf1[100];
- static int init = 0;
- static int format = 0;
- static UINT8 *pict[3];
- int i;
- if (!init) {
- pict[0] = malloc(width * height);
- pict[1] = malloc(width * height / 4);
- pict[2] = malloc(width * height / 4);
- init = 1;
- }
-
- picture[0] = pict[0];
- picture[1] = pict[1];
- picture[2] = pict[2];
- /* auto detect format */
- if (format == 0) {
- sprintf(buf, "%s%d.Y", video_filename, picture_number);
- f=fopen(buf, "r");
- if (f) {
- fclose(f);
- format = INFMT_YUV;
- goto ok;
- }
- sprintf(buf, "%s%d.pgm", video_filename, picture_number);
- f=fopen(buf, "r");
- if (f) {
- fclose(f);
- format = INFMT_PGM;
- goto ok;
- }
- fprintf(stderr, "Bad file name or unknown input video formatn");
- return -1;
- }
- ok:
- switch(format) {
- case INFMT_YUV:
- sprintf(buf, "%s%d.Y", video_filename, picture_number);
- f=fopen(buf, "r");
- if (!f) {
- return -1;
- }
-
- fread(picture[0], 1, width * height, f);
- fclose(f);
-
- sprintf(buf, "%s%d.U", video_filename, picture_number);
- f=fopen(buf, "r");
- if (!f) {
- perror(buf);
- return -1;
- }
- fread(picture[1], 1, width * height / 4, f);
- fclose(f);
-
- sprintf(buf, "%s%d.V", video_filename, picture_number);
- f=fopen(buf, "r");
- if (!f) {
- perror(buf);
- return -1;
- }
- fread(picture[2], 1, width * height / 4, f);
- fclose(f);
- break;
- case INFMT_PGM:
- sprintf(buf, "%s%d.pgm", video_filename, picture_number);
- f=fopen(buf, "r");
- if (!f) {
- return -1;
- }
- pnm_get(f, buf1, sizeof(buf1));
- if (strcmp(buf1, "P5")) {
- fprintf(stderr, "%s: bad pgm filen", buf);
- fclose(f);
- return -1;
- }
- pnm_get(f, buf1, sizeof(buf1));
- pnm_get(f, buf1, sizeof(buf1));
- pnm_get(f, buf1, sizeof(buf1));
-
- fread(picture[0], 1, width * height, f);
- for(i=0;i<height/2;i++) {
- fread(picture[1] + i * width/2, 1, width/2, f);
- fread(picture[2] + i * width/2, 1, width/2, f);
- }
- fclose(f);
- break;
- }
- return 0;
- }
- static void display_stats(AVEncodeContext *video_ctx,
- AVEncodeContext *audio_ctx,
- int batch_mode, int the_end)
- {
- if (video_ctx &&
- ((video_ctx->frame_number % video_ctx->rate) == 0 ||
- the_end)) {
- float ti;
-
- if (batch_mode) {
- ti = (float)video_ctx->frame_number / video_ctx->rate;
- } else {
- ti = (gettime() - time_start) / 1000000.0;
- if (ti < 0.1)
- ti = 0.1;
- }
-
- fprintf(stderr,
- "frame=%5d size=%8dkB time=%0.1f fps=%4.1f bitrate=%6.1fkbits/s q=%2dr",
- video_ctx->frame_number,
- data_out_size / 1024,
- ti,
- video_ctx->frame_number / ti,
- (data_out_size * 8 / ti / 1000),
- video_ctx->quality);
- if (the_end) {
- fprintf(stderr,"n");
- }
- fflush(stderr);
- }
- #if 0
- if (the_end && batch_mode && audio_ctx) {
- duration = (gettime() - ti) / 1000000.0;
- factor = 0;
- if (ti > 0) {
- factor = (float)nb_samples / s->sample_rate / duration;
- }
- fprintf(stderr, "%0.1f seconds compressed in %0.1f seconds (speed factor: %0.1f)n",
- (float)nb_samples / s->sample_rate,
- duration,
- factor);
- }
- #endif
- }
- void raw_write_data(void *opaque,
- unsigned char *buf, int size)
- {
- FILE *outfile = opaque;
- fwrite(buf, 1, size, outfile);
- data_out_size += size;
- }
- int raw_seek(void *opaque, long long offset, int whence)
- {
- FILE *outfile = opaque;
- fseek(outfile, offset, whence);
- return 0;
- }
- static void av_encode(AVFormatContext *ctx,
- const char *video_filename,
- const char *audio_filename)
- {
- UINT8 audio_buffer[4096];
- UINT8 video_buffer[128*1024];
- char buf[256];
- short *samples;
- int ret;
- int audio_fd;
- FILE *infile;
- int sample_count;
- int batch_mode;
- AVEncodeContext *audio_enc, *video_enc;
- int frame_size, frame_bytes;
- AVEncoder *audio_encoder, *video_encoder;
- UINT8 *picture[3];
- /* audio */
- audio_enc = ctx->audio_enc;
- sample_count = 0;
- infile = NULL;
- frame_size = 0;
- samples = NULL;
- audio_fd = -1;
- frame_bytes = 0;
- batch_mode = 0;
- if (audio_filename ||
- video_filename)
- batch_mode = 1;
-
- if (audio_enc) {
- if (batch_mode) {
- if (!audio_filename) {
- fprintf(stderr, "Must give audio input filen");
- exit(1);
- }
- infile = fopen(audio_filename, "r");
- if (!infile) {
- fprintf(stderr, "Could not open '%s'n", audio_filename);
- exit(1);
- }
- audio_fd = -1;
- } else {
- audio_fd = audio_open(audio_enc->rate, audio_enc->channels);
- if (audio_fd < 0) {
- fprintf(stderr, "Could not open audio devicen");
- exit(1);
- }
- }
-
- audio_encoder = avencoder_find(ctx->format->audio_codec);
- if (avencoder_open(audio_enc, audio_encoder) < 0) {
- fprintf(stderr, "Audio encoder: incorrect audio frequency or bitraten");
- exit(1);
- }
- avencoder_string(buf, sizeof(buf), audio_enc);
- fprintf(stderr, " %sn", buf);
-
- frame_size = audio_enc->frame_size;
-
- frame_bytes = frame_size * 2 * audio_enc->channels;
- samples = malloc(frame_bytes);
- }
- /* video */
- video_enc = ctx->video_enc;
- if (video_enc) {
- if (batch_mode) {
- if (!video_filename) {
- fprintf(stderr, "Must give video input filen");
- exit(1);
- }
- } else {
- ret = v4l_init(video_enc->rate, video_enc->width, video_enc->height);
- if (ret < 0) {
- fprintf(stderr,"Could not init video 4 linux capturen");
- exit(1);
- }
- }
-
- video_encoder = avencoder_find(ctx->format->video_codec);
- if (avencoder_open(video_enc, video_encoder) < 0) {
- fprintf(stderr, "Error while initializing video codecn");
- exit(1);
- }
- avencoder_string(buf, sizeof(buf), video_enc);
- fprintf(stderr, " %sn", buf);
- }
-
- ctx->format->write_header(ctx);
- time_start = gettime();
- for(;;) {
- /* read & compression audio frames */
- if (audio_enc) {
- if (!batch_mode) {
- for(;;) {
- ret = read(audio_fd, samples, frame_bytes);
- if (ret != frame_bytes)
- break;
- ret = avencoder_encode(audio_enc,
- audio_buffer, sizeof(audio_buffer), samples);
- ctx->format->write_audio_frame(ctx, audio_buffer, ret);
- }
- } else {
- if (video_enc)
- sample_count += audio_enc->rate / video_enc->rate;
- else
- sample_count += frame_size;
- while (sample_count > frame_size) {
- if (fread(samples, 1, frame_bytes, infile) == 0)
- goto the_end;
-
- ret = avencoder_encode(audio_enc,
- audio_buffer, sizeof(audio_buffer), samples);
- ctx->format->write_audio_frame(ctx, audio_buffer, ret);
- sample_count -= frame_size;
- }
- }
- }
- if (video_enc) {
- /* read video image */
- if (batch_mode) {
- ret = file_read_picture (video_enc, picture,
- video_enc->width, video_enc->height,
- video_enc->frame_number);
- } else {
- ret = v4l_read_picture (picture,
- video_enc->width, video_enc->height,
- video_enc->frame_number);
- }
- if (ret < 0)
- break;
- ret = avencoder_encode(video_enc, video_buffer, sizeof(video_buffer), picture);
- ctx->format->write_video_picture(ctx, video_buffer, ret);
- }
-
- display_stats(video_enc, NULL, batch_mode, 0);
- if (video_enc && video_enc->frame_number >= nb_frames)
- break;
- }
- the_end:
- display_stats(video_enc, NULL, batch_mode, 1);
- if (video_enc)
- avencoder_close(video_enc);
- if (audio_enc)
- avencoder_close(audio_enc);
-
- ctx->format->write_trailer(ctx);
- if (!infile) {
- close(audio_fd);
- } else {
- fclose(infile);
- }
- }
- typedef struct {
- const char *str;
- int width, height;
- } SizeEntry;
- SizeEntry sizes[] = {
- { "sqcif", 128, 96 },
- { "qcif", 176, 144 },
- { "cif", 352, 288 },
- { "4cif", 704, 576 },
- };
-
- enum {
- OPT_AR=256,
- OPT_AB,
- OPT_AN,
- OPT_AC,
- OPT_VN,
- OPT_AD,
- OPT_HQ,
- };
- struct option long_options[] =
- {
- { "ar", required_argument, NULL, OPT_AR },
- { "ab", required_argument, NULL, OPT_AB },
- { "an", no_argument, NULL, OPT_AN },
- { "ac", required_argument, NULL, OPT_AC },
- { "vn", no_argument, NULL, OPT_VN },
- { "ad", required_argument, NULL, OPT_AD },
- { "hq", no_argument, NULL, OPT_HQ },
- };
- enum {
- OUT_FILE,
- OUT_PIPE,
- OUT_UDP,
- };
- void help(void)
- {
- AVFormat *f;
- printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000,2001 Gerard Lantaun"
- "usage: ffmpeg [options] outfile [video_infile] [audio_infile]n"
- "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio layer 2 encodern"
- "n"
- "Main options are:n"
- "n"
- "-L print the LICENSEn"
- "-s size set frame size [%dx%d]n"
- "-f format set encoding format [guessed]n"
- "-r fps set frame rate [%d]n"
- "-b bitrate set the total bitrate in kbit/s [%d]n"
- "-t time set recording time in seconds [%0.1f]n"
- "-ar freq set the audio sampling freq [%d]n"
- "-ab bitrate set the audio bitrate in kbit/s [%d]n"
- "-ac channels set the number of audio channels [%d]n"
- "-an disable audio recording [%s]n"
- "-vn disable video recording [%s]n"
- "-hq high quality mode (non real time) [%s]n"
- "n"
- "Frame sizes abbreviations: sqcif qcif cif 4cifn",
- frame_width, frame_height,
- frame_rate,
- bit_rate / 1000,
- recording_time,
- audio_freq,
- audio_bit_rate / 1000,
- audio_channels,
- audio_disable ? "yes" : "no",
- video_disable ? "yes" : "no",
- high_quality ? "yes" : "no");
- printf("Encoding video formats:");
- for(f = first_format; f != NULL; f = f->next)
- printf(" %s", f->name);
- printf("n");
- printf("outfile can be a file name, - (pipe) or 'udp:host:port'n"
- "n"
- "Advanced options are:n"
- "-d device set video4linux device name [%s]n"
- "-ad device set audio device name [%s]n"
- "-g gop_size set the group of picture size [%d]n"
- "-i use only intra frames [%s]n"
- "-c comment set the comment stringn"
- "n",
- v4l_device,
- audio_device,
- gop_size,
- intra_only ? "yes" : "no");
- }
- void licence(void)
- {
- printf(
- "ffmpeg version " FFMPEG_VERSION "n"
- "Copyright (c) 2000,2001 Gerard Lantaun"
- "This program is free software; you can redistribute it and/or modifyn"
- "it under the terms of the GNU General Public License as published byn"
- "the Free Software Foundation; either version 2 of the License, orn"
- "(at your option) any later version.n"
- "n"
- "This program is distributed in the hope that it will be useful,n"
- "but WITHOUT ANY WARRANTY; without even the implied warranty ofn"
- "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See then"
- "GNU General Public License for more details.n"
- "n"
- "You should have received a copy of the GNU General Public Licensen"
- "along with this program; if not, write to the Free Softwaren"
- "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.n"
- );
- }
- static unsigned char output_buffer[32768];
- int main(int argc, char **argv)
- {
- AVEncodeContext video_enc1, *video_enc = &video_enc1;
- AVEncodeContext audio_enc1, *audio_enc = &audio_enc1;
- UDPContext udp_ctx1, *udp_ctx = &udp_ctx1;
- AVFormatContext av_ctx1, *av_ctx = &av_ctx1;
- FILE *outfile;
- int i, c;
- char *filename;
- int output_type;
- int use_video, use_audio;
- register_avencoder(&ac3_encoder);
- register_avencoder(&mp2_encoder);
- register_avencoder(&mpeg1video_encoder);
- register_avencoder(&h263_encoder);
- register_avencoder(&rv10_encoder);
- register_avencoder(&mjpeg_encoder);
- register_avencoder(&divx_encoder);
- register_avformat(&mp2_format);
- register_avformat(&ac3_format);
- register_avformat(&mpeg1video_format);
- register_avformat(&h263_format);
- register_avformat(&mpeg_mux_format);
- register_avformat(&ra_format);
- register_avformat(&rm_format);
- register_avformat(&asf_format);
- register_avformat(&avi_format);
- register_avformat(&mpjpeg_format);
- register_avformat(&swf_format);
- file_format = NULL;
-
- for(;;) {
- c = getopt_long_only(argc, argv, "s:f:r:b:t:hd:g:ic:L",
- long_options, NULL);
- if (c == -1)
- break;
- switch(c) {
- case 'L':
- licence();
- exit(1);
- case 'h':
- help();
- exit(1);
- case 's':
- {
- int n = sizeof(sizes) / sizeof(SizeEntry);
- const char *p;
- for(i=0;i<n;i++) {
- if (!strcmp(sizes[i].str, optarg)) {
- frame_width = sizes[i].width;
- frame_height = sizes[i].height;
- break;
- }
- }
- if (i == n) {
- p = optarg;
- frame_width = strtol(p, (char **)&p, 10);
- if (*p)
- p++;
- frame_height = strtol(p, (char **)&p, 10);
- }
- }
- break;
- case 'f':
- {
- AVFormat *f;
- f = first_format;
- while (f != NULL && strcmp(f->name, optarg) != 0) f = f->next;
- if (f == NULL) {
- fprintf(stderr, "Invalid format: %sn", optarg);
- exit(1);
- }
- file_format = f;
- }
- break;
- case 'r':
- {
- frame_rate = atoi(optarg);
- }
- break;
- case 'b':
- {
- bit_rate = atoi(optarg) * 1000;
- }
- break;
- case 't':
- {
- recording_time = atof(optarg);
- break;
- }
- /* audio specific */
- case OPT_AR:
- {
- audio_freq = atoi(optarg);
- break;
- }
- case OPT_AB:
- {
- audio_bit_rate = atoi(optarg) * 1000;
- break;
- }
- case OPT_AN:
- audio_disable = 1;
- break;
- case OPT_VN:
- video_disable = 1;
- break;
- case OPT_AC:
- {
- audio_channels = atoi(optarg);
- if (audio_channels != 1 &&
- audio_channels != 2) {
- fprintf(stderr, "Incorrect number of channels: %dn", audio_channels);
- exit(1);
- }
- }
- break;
- case OPT_HQ:
- high_quality = 1;
- break;
- /* advanced options */
- case 'd':
- v4l_device = optarg;
- break;
- case OPT_AD:
- audio_device = optarg;
- break;
- case 'g':
- gop_size = atoi(optarg);
- break;
- case 'i':
- intra_only = 1;
- break;
- case 'c':
- comment_string = optarg;
- break;
- default:
- exit(2);
- }
- }
- if (optind >= argc) {
- help();
- exit(1);
- }
- filename = argv[optind++];
- video_filename = NULL;
- audio_filename = NULL;
- /* auto detect format */
- if (file_format == NULL)
- file_format = guess_format(NULL, filename, NULL);
- if (file_format == NULL)
- file_format = &mpeg_mux_format;
- /* check parameters */
- if (frame_width <= 0 || frame_height <= 0) {
- fprintf(stderr, "Incorrect frame sizen");
- exit(1);
- }
- if ((frame_width % 16) != 0 || (frame_height % 16) != 0) {
- fprintf(stderr, "Frame size must be a multiple of 16n");
- exit(1);
- }
-
- if (bit_rate < 5000 || bit_rate >= 10000000) {
- fprintf(stderr, "Invalid bit raten");
- exit(1);
- }
- if (frame_rate < 1 || frame_rate >= 60) {
- fprintf(stderr, "Invalid frame raten");
- exit(1);
- }
- nb_frames = (int)(recording_time * frame_rate);
- if (nb_frames < 1) {
- fprintf(stderr, "Invalid recording timen");
- exit(1);
- }
- use_video = file_format->video_codec != CODEC_ID_NONE;
- use_audio = file_format->audio_codec != CODEC_ID_NONE;
- if (audio_disable) {
- use_audio = 0;
- }
- if (video_disable) {
- use_video = 0;
- }
-
- if (use_video == 0 && use_audio == 0) {
- fprintf(stderr, "No audio or video selectedn");
- exit(1);
- }
- fprintf(stderr, "Recording: %s, %0.1f secondsn",
- file_format->name,
- recording_time);
- /* open output media */
- if (strstart(filename, "udp:", NULL)) {
- output_type = OUT_UDP;
- outfile = NULL;
- memset(udp_ctx, 0, sizeof(*udp_ctx));
- if (udp_tx_open(udp_ctx, filename, 0) < 0) {
- fprintf(stderr, "Could not open UDP socketn");
- exit(1);
- }
- } else if (!strcmp(filename, "-")) {
- output_type = OUT_PIPE;
- outfile = stdout;
- } else {
- output_type = OUT_FILE;
- outfile = fopen(filename, "w");
- if (!outfile) {
- perror(filename);
- exit(1);
- }
- }
- av_ctx->video_enc = NULL;
- av_ctx->audio_enc = NULL;
- if (output_type == OUT_UDP) {
- init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
- udp_ctx, udp_write_data, NULL);
- } else {
- init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
- outfile, raw_write_data, raw_seek);
- }
- if (use_video) {
- if (optind < argc) {
- video_filename = argv[optind++];
- }
- /* init mpeg video encoding context */
- memset(video_enc, 0, sizeof(*video_enc));
- video_enc->bit_rate = bit_rate;
- video_enc->rate = frame_rate;
- video_enc->width = frame_width;
- video_enc->height = frame_height;
- if (!intra_only)
- video_enc->gop_size = gop_size;
- else
- video_enc->gop_size = 0;
- if (high_quality)
- video_enc->flags |= CODEC_FLAG_HQ;
- av_ctx->video_enc = video_enc;
- av_ctx->format = file_format;
- }
- if (use_audio) {
- if (optind < argc) {
- audio_filename = argv[optind++];
- }
- audio_enc->bit_rate = audio_bit_rate;
- audio_enc->rate = audio_freq;
- audio_enc->channels = audio_channels;
- av_ctx->audio_enc = audio_enc;
- }
- av_ctx->format = file_format;
- av_ctx->is_streamed = 0;
- av_encode(av_ctx, video_filename, audio_filename);
- /* close output media */
- switch(output_type) {
- case OUT_FILE:
- fclose(outfile);
- break;
- case OUT_PIPE:
- break;
- case OUT_UDP:
- udp_tx_close(udp_ctx);
- break;
- }
- fprintf(stderr, "n");
-
- return 0;
- }
-