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.

165 lines
4.4KB

  1. /*
  2. * copyright (c) 2001 Fabrice Bellard
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * video encoding with libavcodec API example
  23. *
  24. * @example encode_video.c
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "libavcodec/avcodec.h"
  30. #include "libavutil/frame.h"
  31. #include "libavutil/imgutils.h"
  32. int main(int argc, char **argv)
  33. {
  34. const char *filename;
  35. const AVCodec *codec;
  36. AVCodecContext *c= NULL;
  37. int i, ret, x, y, got_output;
  38. FILE *f;
  39. AVFrame *picture;
  40. AVPacket pkt;
  41. uint8_t endcode[] = { 0, 0, 1, 0xb7 };
  42. if (argc <= 1) {
  43. fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
  44. exit(0);
  45. }
  46. filename = argv[1];
  47. avcodec_register_all();
  48. /* find the mpeg1video encoder */
  49. codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
  50. if (!codec) {
  51. fprintf(stderr, "codec not found\n");
  52. exit(1);
  53. }
  54. c = avcodec_alloc_context3(codec);
  55. picture = av_frame_alloc();
  56. /* put sample parameters */
  57. c->bit_rate = 400000;
  58. /* resolution must be a multiple of two */
  59. c->width = 352;
  60. c->height = 288;
  61. /* frames per second */
  62. c->time_base= (AVRational){1,25};
  63. c->gop_size = 10; /* emit one intra frame every ten frames */
  64. c->max_b_frames=1;
  65. c->pix_fmt = AV_PIX_FMT_YUV420P;
  66. /* open it */
  67. if (avcodec_open2(c, codec, NULL) < 0) {
  68. fprintf(stderr, "could not open codec\n");
  69. exit(1);
  70. }
  71. f = fopen(filename, "wb");
  72. if (!f) {
  73. fprintf(stderr, "could not open %s\n", filename);
  74. exit(1);
  75. }
  76. ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
  77. c->pix_fmt, 32);
  78. if (ret < 0) {
  79. fprintf(stderr, "could not alloc raw picture buffer\n");
  80. exit(1);
  81. }
  82. picture->format = c->pix_fmt;
  83. picture->width = c->width;
  84. picture->height = c->height;
  85. /* encode 1 second of video */
  86. for(i=0;i<25;i++) {
  87. av_init_packet(&pkt);
  88. pkt.data = NULL; // packet data will be allocated by the encoder
  89. pkt.size = 0;
  90. fflush(stdout);
  91. /* prepare a dummy image */
  92. /* Y */
  93. for(y=0;y<c->height;y++) {
  94. for(x=0;x<c->width;x++) {
  95. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  96. }
  97. }
  98. /* Cb and Cr */
  99. for(y=0;y<c->height/2;y++) {
  100. for(x=0;x<c->width/2;x++) {
  101. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  102. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  103. }
  104. }
  105. picture->pts = i;
  106. /* encode the image */
  107. ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
  108. if (ret < 0) {
  109. fprintf(stderr, "error encoding frame\n");
  110. exit(1);
  111. }
  112. if (got_output) {
  113. printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
  114. fwrite(pkt.data, 1, pkt.size, f);
  115. av_packet_unref(&pkt);
  116. }
  117. }
  118. /* get the delayed frames */
  119. for (got_output = 1; got_output; i++) {
  120. fflush(stdout);
  121. ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
  122. if (ret < 0) {
  123. fprintf(stderr, "error encoding frame\n");
  124. exit(1);
  125. }
  126. if (got_output) {
  127. printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
  128. fwrite(pkt.data, 1, pkt.size, f);
  129. av_packet_unref(&pkt);
  130. }
  131. }
  132. /* add sequence end code to have a real MPEG file */
  133. fwrite(endcode, 1, sizeof(endcode), f);
  134. fclose(f);
  135. avcodec_free_context(&c);
  136. av_freep(&picture->data[0]);
  137. av_frame_free(&picture);
  138. return 0;
  139. }