You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

718 lines
20KB

  1. /*
  2. * Basic user interface for ffmpeg system
  3. * Copyright (c) 2000 Gerard Lantau
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <netinet/in.h>
  22. #include <linux/videodev.h>
  23. #include <linux/soundcard.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/mman.h>
  28. #include <errno.h>
  29. #include <sys/time.h>
  30. #include <getopt.h>
  31. #include "mpegenc.h"
  32. #include "mpegvideo.h"
  33. static AVFormat *file_format;
  34. static int frame_width = 160;
  35. static int frame_height = 128;
  36. static int frame_rate = 25;
  37. static int bit_rate = 200000;
  38. static int video_disable = 0;
  39. static const char *video_filename, *audio_filename;
  40. static float recording_time = 10.0;
  41. static int nb_frames;
  42. static int gop_size = 12;
  43. static int intra_only = 0;
  44. static int audio_freq = 44100;
  45. static int audio_bit_rate = 64000;
  46. static int audio_disable = 0;
  47. static int audio_channels = 1;
  48. static long long time_start;
  49. static int file_read_picture(AVEncodeContext *s,
  50. UINT8 *picture[3],
  51. int width, int height,
  52. int picture_number)
  53. {
  54. FILE *f;
  55. char buf[1024];
  56. static int init = 0;
  57. static UINT8 *pict[3];
  58. if (!init) {
  59. pict[0] = malloc(width * height);
  60. pict[1] = malloc(width * height / 4);
  61. pict[2] = malloc(width * height / 4);
  62. init = 1;
  63. }
  64. picture[0] = pict[0];
  65. picture[1] = pict[1];
  66. picture[2] = pict[2];
  67. sprintf(buf, "%s%d.Y", video_filename, picture_number);
  68. f=fopen(buf, "r");
  69. if (!f) {
  70. return -1;
  71. }
  72. fread(picture[0], 1, width * height, f);
  73. fclose(f);
  74. sprintf(buf, "%s%d.U", video_filename, picture_number);
  75. f=fopen(buf, "r");
  76. if (!f) {
  77. perror(buf);
  78. exit(1);
  79. }
  80. fread(picture[1], 1, width * height / 4, f);
  81. fclose(f);
  82. sprintf(buf, "%s%d.V", video_filename, picture_number);
  83. f=fopen(buf, "r");
  84. if (!f) {
  85. perror(buf);
  86. exit(1);
  87. }
  88. fread(picture[2], 1, width * height / 4, f);
  89. fclose(f);
  90. return 0;
  91. }
  92. static void display_stats(AVEncodeContext *video_ctx,
  93. AVEncodeContext *audio_ctx,
  94. int batch_mode, int the_end)
  95. {
  96. if (video_ctx &&
  97. ((video_ctx->frame_number % video_ctx->rate) == 0 ||
  98. the_end)) {
  99. float ti;
  100. if (batch_mode) {
  101. ti = (float)video_ctx->frame_number / video_ctx->rate;
  102. } else {
  103. ti = (gettime() - time_start) / 1000000.0;
  104. if (ti < 0.1)
  105. ti = 0.1;
  106. }
  107. fprintf(stderr,
  108. "frame=%5d size=%8dkB time=%0.1f fps=%4.1f bitrate=%6.1fkbits/s q=%2d\r",
  109. video_ctx->frame_number,
  110. data_out_size / 1024,
  111. ti,
  112. video_ctx->frame_number / ti,
  113. (data_out_size * 8 / ti / 1000),
  114. ((MpegEncContext *)video_ctx->priv_data)->qscale);
  115. if (the_end) {
  116. fprintf(stderr,"\n");
  117. }
  118. fflush(stderr);
  119. }
  120. #if 0
  121. if (the_end && batch_mode && audio_ctx) {
  122. duration = (gettime() - ti) / 1000000.0;
  123. factor = 0;
  124. if (ti > 0) {
  125. factor = (float)nb_samples / s->sample_rate / duration;
  126. }
  127. fprintf(stderr, "%0.1f seconds compressed in %0.1f seconds (speed factor: %0.1f)\n",
  128. (float)nb_samples / s->sample_rate,
  129. duration,
  130. factor);
  131. }
  132. #endif
  133. }
  134. void raw_write_data(void *opaque,
  135. unsigned char *buf, int size)
  136. {
  137. FILE *outfile = opaque;
  138. fwrite(buf, 1, size, outfile);
  139. data_out_size += size;
  140. }
  141. int raw_seek(void *opaque, long long offset, int whence)
  142. {
  143. FILE *outfile = opaque;
  144. fseek(outfile, offset, whence);
  145. return 0;
  146. }
  147. static void av_encode(AVFormatContext *ctx,
  148. const char *video_filename,
  149. const char *audio_filename)
  150. {
  151. UINT8 audio_buffer[4096];
  152. UINT8 video_buffer[128*1024];
  153. char buf[256];
  154. short *samples;
  155. int ret;
  156. int audio_fd;
  157. FILE *infile;
  158. int sample_count;
  159. int batch_mode;
  160. AVEncodeContext *audio_enc, *video_enc;
  161. int frame_size, frame_bytes;
  162. AVEncoder *audio_encoder, *video_encoder;
  163. UINT8 *picture[3];
  164. /* audio */
  165. audio_enc = ctx->audio_enc;
  166. sample_count = 0;
  167. infile = NULL;
  168. frame_size = 0;
  169. samples = NULL;
  170. audio_fd = -1;
  171. frame_bytes = 0;
  172. batch_mode = 0;
  173. if (audio_filename ||
  174. video_filename)
  175. batch_mode = 1;
  176. if (audio_enc) {
  177. if (batch_mode) {
  178. if (!audio_filename) {
  179. fprintf(stderr, "Must give audio input file\n");
  180. exit(1);
  181. }
  182. infile = fopen(audio_filename, "r");
  183. if (!infile) {
  184. fprintf(stderr, "Could not open '%s'\n", audio_filename);
  185. exit(1);
  186. }
  187. audio_fd = -1;
  188. } else {
  189. audio_fd = audio_open(audio_enc->rate, audio_enc->channels);
  190. if (audio_fd < 0) {
  191. fprintf(stderr, "Could not open audio device\n");
  192. exit(1);
  193. }
  194. }
  195. audio_encoder = avencoder_find(ctx->format->audio_codec);
  196. if (avencoder_open(audio_enc, audio_encoder) < 0) {
  197. fprintf(stderr, "Audio encoder: incorrect audio frequency or bitrate\n");
  198. exit(1);
  199. }
  200. avencoder_string(buf, sizeof(buf), audio_enc);
  201. fprintf(stderr, " %s\n", buf);
  202. frame_size = audio_enc->frame_size;
  203. frame_bytes = frame_size * 2 * audio_enc->channels;
  204. samples = malloc(frame_bytes);
  205. }
  206. /* video */
  207. video_enc = ctx->video_enc;
  208. if (video_enc) {
  209. if (batch_mode) {
  210. if (!video_filename) {
  211. fprintf(stderr, "Must give video input file\n");
  212. exit(1);
  213. }
  214. } else {
  215. ret = v4l_init(video_enc->rate, video_enc->width, video_enc->height);
  216. if (ret < 0) {
  217. fprintf(stderr,"Could not init video 4 linux capture\n");
  218. exit(1);
  219. }
  220. }
  221. video_encoder = avencoder_find(ctx->format->video_codec);
  222. if (avencoder_open(video_enc, video_encoder) < 0) {
  223. fprintf(stderr, "Error while initializing video codec\n");
  224. exit(1);
  225. }
  226. avencoder_string(buf, sizeof(buf), video_enc);
  227. fprintf(stderr, " %s\n", buf);
  228. }
  229. ctx->format->write_header(ctx);
  230. time_start = gettime();
  231. for(;;) {
  232. /* read & compression audio frames */
  233. if (audio_enc) {
  234. if (!batch_mode) {
  235. for(;;) {
  236. ret = read(audio_fd, samples, frame_bytes);
  237. if (ret != frame_bytes)
  238. break;
  239. ret = avencoder_encode(audio_enc,
  240. audio_buffer, sizeof(audio_buffer), samples);
  241. ctx->format->write_audio_frame(ctx, audio_buffer, ret);
  242. }
  243. } else {
  244. if (video_enc)
  245. sample_count += audio_enc->rate / video_enc->rate;
  246. else
  247. sample_count += frame_size;
  248. while (sample_count > frame_size) {
  249. if (fread(samples, 1, frame_bytes, infile) == 0)
  250. goto the_end;
  251. ret = avencoder_encode(audio_enc,
  252. audio_buffer, sizeof(audio_buffer), samples);
  253. ctx->format->write_audio_frame(ctx, audio_buffer, ret);
  254. sample_count -= frame_size;
  255. }
  256. }
  257. }
  258. if (video_enc) {
  259. /* read video image */
  260. if (batch_mode) {
  261. ret = file_read_picture (video_enc, picture,
  262. video_enc->width, video_enc->height,
  263. video_enc->frame_number);
  264. } else {
  265. ret = v4l_read_picture (picture,
  266. video_enc->width, video_enc->height,
  267. video_enc->frame_number);
  268. }
  269. if (ret < 0)
  270. break;
  271. ret = avencoder_encode(video_enc, video_buffer, sizeof(video_buffer), picture);
  272. ctx->format->write_video_picture(ctx, video_buffer, ret);
  273. }
  274. display_stats(video_enc, NULL, batch_mode, 0);
  275. if (video_enc && video_enc->frame_number >= nb_frames)
  276. break;
  277. }
  278. the_end:
  279. display_stats(video_enc, NULL, batch_mode, 1);
  280. if (video_enc)
  281. avencoder_close(video_enc);
  282. if (audio_enc)
  283. avencoder_close(audio_enc);
  284. ctx->format->write_trailer(ctx);
  285. if (!infile) {
  286. close(audio_fd);
  287. } else {
  288. fclose(infile);
  289. }
  290. }
  291. typedef struct {
  292. const char *str;
  293. int width, height;
  294. } SizeEntry;
  295. SizeEntry sizes[] = {
  296. { "sqcif", 128, 96 },
  297. { "qcif", 176, 144 },
  298. { "cif", 352, 288 },
  299. { "4cif", 704, 576 },
  300. };
  301. enum {
  302. OPT_AR=256,
  303. OPT_AB,
  304. OPT_AN,
  305. OPT_AC,
  306. OPT_VN,
  307. };
  308. struct option long_options[] =
  309. {
  310. { "ar", required_argument, NULL, OPT_AR },
  311. { "ab", required_argument, NULL, OPT_AB },
  312. { "an", no_argument, NULL, OPT_AN },
  313. { "ac", required_argument, NULL, OPT_AC },
  314. { "vn", no_argument, NULL, OPT_VN },
  315. };
  316. enum {
  317. OUT_FILE,
  318. OUT_PIPE,
  319. OUT_UDP,
  320. };
  321. void help(void)
  322. {
  323. AVFormat *f;
  324. printf("ffmpeg version 1.0, Copyright (c) 2000 Gerard Lantau\n"
  325. "usage: ffmpeg [options] outfile [video_infile] [audio_infile]\n"
  326. "Hyper fast MPEG1 video/H263/RV and AC3/MPEG audio layer 2 encoder\n"
  327. "\n"
  328. "Main options are:\n"
  329. "\n"
  330. "-L print the LICENSE\n"
  331. "-s size set frame size [%dx%d]\n"
  332. "-f format set encoding format [%s]\n"
  333. "-r fps set frame rate [%d]\n"
  334. "-b bitrate set the total bitrate in kbit/s [%d]\n"
  335. "-t time set recording time in seconds [%0.1f]\n"
  336. "-ar freq set the audio sampling freq [%d]\n"
  337. "-ab bitrate set the audio bitrate in kbit/s [%d]\n"
  338. "-ac channels set the number of audio channels [%d]\n"
  339. "-an disable audio recording [%s]\n"
  340. "-vn disable video recording [%s]\n"
  341. "\n"
  342. "Frame sizes abbreviations: sqcif qcif cif 4cif\n",
  343. frame_width, frame_height,
  344. file_format->name,
  345. frame_rate,
  346. bit_rate / 1000,
  347. recording_time,
  348. audio_freq,
  349. audio_bit_rate / 1000,
  350. audio_channels,
  351. audio_disable ? "yes" : "no",
  352. video_disable ? "yes" : "no");
  353. printf("Encoding video formats:");
  354. for(f = first_format; f != NULL; f = f->next)
  355. printf(" %s", f->name);
  356. printf("\n");
  357. printf("outfile can be a file name, - (pipe) or 'udp:host:port'\n"
  358. "\n"
  359. "Advanced options are:\n"
  360. "-d device set video4linux device name\n"
  361. "-g gop_size set the group of picture size [%d]\n"
  362. "-i use only intra frames [%s]\n"
  363. "-c comment set the comment string\n"
  364. "\n",
  365. gop_size,
  366. intra_only ? "yes" : "no");
  367. }
  368. void licence(void)
  369. {
  370. printf(
  371. "ffmpeg version 1.0\n"
  372. "Copyright (c) 2000 Gerard Lantau\n"
  373. "This program is free software; you can redistribute it and/or modify\n"
  374. "it under the terms of the GNU General Public License as published by\n"
  375. "the Free Software Foundation; either version 2 of the License, or\n"
  376. "(at your option) any later version.\n"
  377. "\n"
  378. "This program is distributed in the hope that it will be useful,\n"
  379. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  380. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  381. "GNU General Public License for more details.\n"
  382. "\n"
  383. "You should have received a copy of the GNU General Public License\n"
  384. "along with this program; if not, write to the Free Software\n"
  385. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  386. );
  387. }
  388. static unsigned char output_buffer[32768];
  389. int main(int argc, char **argv)
  390. {
  391. AVEncodeContext video_enc1, *video_enc = &video_enc1;
  392. AVEncodeContext audio_enc1, *audio_enc = &audio_enc1;
  393. UDPContext udp_ctx1, *udp_ctx = &udp_ctx1;
  394. AVFormatContext av_ctx1, *av_ctx = &av_ctx1;
  395. FILE *outfile;
  396. int i, c;
  397. char *filename;
  398. int output_type;
  399. int use_video, use_audio;
  400. register_avencoder(&ac3_encoder);
  401. register_avencoder(&mp2_encoder);
  402. register_avencoder(&mpeg1video_encoder);
  403. register_avencoder(&h263_encoder);
  404. register_avencoder(&rv10_encoder);
  405. register_avencoder(&mjpeg_encoder);
  406. register_avformat(&mp2_format);
  407. register_avformat(&ac3_format);
  408. register_avformat(&mpeg1video_format);
  409. register_avformat(&h263_format);
  410. register_avformat(&mpeg_mux_format);
  411. register_avformat(&ra_format);
  412. register_avformat(&rm_format);
  413. register_avformat(&asf_format);
  414. register_avformat(&mpjpeg_format);
  415. register_avformat(&swf_format);
  416. file_format = NULL;
  417. for(;;) {
  418. c = getopt_long_only(argc, argv, "s:f:r:b:t:hd:g:ic:L",
  419. long_options, NULL);
  420. if (c == -1)
  421. break;
  422. switch(c) {
  423. case 'L':
  424. licence();
  425. exit(1);
  426. case 'h':
  427. help();
  428. exit(1);
  429. case 's':
  430. {
  431. int n = sizeof(sizes) / sizeof(SizeEntry);
  432. const char *p;
  433. for(i=0;i<n;i++) {
  434. if (!strcmp(sizes[i].str, optarg)) {
  435. frame_width = sizes[i].width;
  436. frame_height = sizes[i].height;
  437. break;
  438. }
  439. }
  440. if (i == n) {
  441. p = optarg;
  442. frame_width = strtol(p, (char **)&p, 10);
  443. if (*p)
  444. p++;
  445. frame_height = strtol(p, (char **)&p, 10);
  446. }
  447. }
  448. break;
  449. case 'f':
  450. {
  451. AVFormat *f;
  452. f = first_format;
  453. while (f != NULL && strcmp(f->name, optarg) != 0) f = f->next;
  454. if (f == NULL) {
  455. fprintf(stderr, "Invalid format: %s\n", optarg);
  456. exit(1);
  457. }
  458. file_format = f;
  459. }
  460. break;
  461. case 'r':
  462. {
  463. frame_rate = atoi(optarg);
  464. }
  465. break;
  466. case 'b':
  467. {
  468. bit_rate = atoi(optarg) * 1000;
  469. }
  470. break;
  471. case 't':
  472. {
  473. recording_time = atof(optarg);
  474. break;
  475. }
  476. /* audio specific */
  477. case OPT_AR:
  478. {
  479. audio_freq = atoi(optarg);
  480. break;
  481. }
  482. case OPT_AB:
  483. {
  484. audio_bit_rate = atoi(optarg) * 1000;
  485. break;
  486. }
  487. case OPT_AN:
  488. audio_disable = 1;
  489. break;
  490. case OPT_VN:
  491. video_disable = 1;
  492. break;
  493. case OPT_AC:
  494. {
  495. audio_channels = atoi(optarg);
  496. if (audio_channels != 1 &&
  497. audio_channels != 2) {
  498. fprintf(stderr, "Incorrect number of channels: %d\n", audio_channels);
  499. exit(1);
  500. }
  501. }
  502. break;
  503. /* advanced options */
  504. case 'd':
  505. v4l_device = optarg;
  506. break;
  507. case 'g':
  508. gop_size = atoi(optarg);
  509. break;
  510. case 'i':
  511. intra_only = 1;
  512. break;
  513. case 'c':
  514. comment_string = optarg;
  515. break;
  516. default:
  517. exit(2);
  518. }
  519. }
  520. if (optind >= argc) {
  521. help();
  522. exit(1);
  523. }
  524. filename = argv[optind++];
  525. video_filename = NULL;
  526. audio_filename = NULL;
  527. /* auto detect format */
  528. if (file_format == NULL)
  529. file_format = guess_format(NULL, filename, NULL);
  530. if (file_format == NULL)
  531. file_format = &mpeg_mux_format;
  532. /* check parameters */
  533. if (frame_width <= 0 || frame_height <= 0) {
  534. fprintf(stderr, "Incorrect frame size\n");
  535. exit(1);
  536. }
  537. if ((frame_width % 16) != 0 || (frame_height % 16) != 0) {
  538. fprintf(stderr, "Frame size must be a multiple of 16\n");
  539. exit(1);
  540. }
  541. if (bit_rate < 5000 || bit_rate >= 10000000) {
  542. fprintf(stderr, "Invalid bit rate\n");
  543. exit(1);
  544. }
  545. if (frame_rate < 1 || frame_rate >= 60) {
  546. fprintf(stderr, "Invalid frame rate\n");
  547. exit(1);
  548. }
  549. nb_frames = (int)(recording_time * frame_rate);
  550. if (nb_frames < 1) {
  551. fprintf(stderr, "Invalid recording time\n");
  552. exit(1);
  553. }
  554. use_video = file_format->video_codec != CODEC_ID_NONE;
  555. use_audio = file_format->audio_codec != CODEC_ID_NONE;
  556. if (audio_disable) {
  557. use_audio = 0;
  558. }
  559. if (video_disable) {
  560. use_video = 0;
  561. }
  562. if (use_video == 0 && use_audio == 0) {
  563. fprintf(stderr, "No audio or video selected\n");
  564. exit(1);
  565. }
  566. fprintf(stderr, "Recording: %s, %0.1f seconds\n",
  567. file_format->name,
  568. recording_time);
  569. /* open output media */
  570. if (strstart(filename, "udp:", NULL)) {
  571. output_type = OUT_UDP;
  572. outfile = NULL;
  573. memset(udp_ctx, 0, sizeof(*udp_ctx));
  574. if (udp_tx_open(udp_ctx, filename, 0) < 0) {
  575. fprintf(stderr, "Could not open UDP socket\n");
  576. exit(1);
  577. }
  578. } else if (!strcmp(filename, "-")) {
  579. output_type = OUT_PIPE;
  580. outfile = stdout;
  581. } else {
  582. output_type = OUT_FILE;
  583. outfile = fopen(filename, "w");
  584. if (!outfile) {
  585. perror(filename);
  586. exit(1);
  587. }
  588. }
  589. av_ctx->video_enc = NULL;
  590. av_ctx->audio_enc = NULL;
  591. if (output_type == OUT_UDP) {
  592. init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
  593. udp_ctx, udp_write_data, NULL);
  594. } else {
  595. init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
  596. outfile, raw_write_data, raw_seek);
  597. }
  598. if (use_video) {
  599. if (optind < argc) {
  600. video_filename = argv[optind++];
  601. }
  602. /* init mpeg video encoding context */
  603. memset(video_enc, 0, sizeof(*video_enc));
  604. video_enc->bit_rate = bit_rate;
  605. video_enc->rate = frame_rate;
  606. video_enc->width = frame_width;
  607. video_enc->height = frame_height;
  608. if (!intra_only)
  609. video_enc->gop_size = gop_size;
  610. else
  611. video_enc->gop_size = 0;
  612. av_ctx->video_enc = video_enc;
  613. av_ctx->format = file_format;
  614. }
  615. if (use_audio) {
  616. if (optind < argc) {
  617. audio_filename = argv[optind++];
  618. }
  619. audio_enc->bit_rate = audio_bit_rate;
  620. audio_enc->rate = audio_freq;
  621. audio_enc->channels = audio_channels;
  622. av_ctx->audio_enc = audio_enc;
  623. }
  624. av_ctx->format = file_format;
  625. av_ctx->is_streamed = 0;
  626. av_encode(av_ctx, video_filename, audio_filename);
  627. /* close output media */
  628. switch(output_type) {
  629. case OUT_FILE:
  630. fclose(outfile);
  631. break;
  632. case OUT_PIPE:
  633. break;
  634. case OUT_UDP:
  635. udp_tx_close(udp_ctx);
  636. break;
  637. }
  638. fprintf(stderr, "\n");
  639. return 0;
  640. }