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.

167 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->framerate = (AVRational){25, 1};
  64. c->gop_size = 10; /* emit one intra frame every ten frames */
  65. c->max_b_frames=1;
  66. c->pix_fmt = AV_PIX_FMT_YUV420P;
  67. /* open it */
  68. if (avcodec_open2(c, codec, NULL) < 0) {
  69. fprintf(stderr, "could not open codec\n");
  70. exit(1);
  71. }
  72. f = fopen(filename, "wb");
  73. if (!f) {
  74. fprintf(stderr, "could not open %s\n", filename);
  75. exit(1);
  76. }
  77. ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
  78. c->pix_fmt, 32);
  79. if (ret < 0) {
  80. fprintf(stderr, "could not alloc raw picture buffer\n");
  81. exit(1);
  82. }
  83. picture->format = c->pix_fmt;
  84. picture->width = c->width;
  85. picture->height = c->height;
  86. /* encode 1 second of video */
  87. for(i=0;i<25;i++) {
  88. av_init_packet(&pkt);
  89. pkt.data = NULL; // packet data will be allocated by the encoder
  90. pkt.size = 0;
  91. fflush(stdout);
  92. /* prepare a dummy image */
  93. /* Y */
  94. for(y=0;y<c->height;y++) {
  95. for(x=0;x<c->width;x++) {
  96. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  97. }
  98. }
  99. /* Cb and Cr */
  100. for(y=0;y<c->height/2;y++) {
  101. for(x=0;x<c->width/2;x++) {
  102. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  103. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  104. }
  105. }
  106. picture->pts = i;
  107. /* encode the image */
  108. ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
  109. if (ret < 0) {
  110. fprintf(stderr, "error encoding frame\n");
  111. exit(1);
  112. }
  113. if (got_output) {
  114. printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
  115. fwrite(pkt.data, 1, pkt.size, f);
  116. av_packet_unref(&pkt);
  117. }
  118. }
  119. /* get the delayed frames */
  120. for (got_output = 1; got_output; i++) {
  121. fflush(stdout);
  122. ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
  123. if (ret < 0) {
  124. fprintf(stderr, "error encoding frame\n");
  125. exit(1);
  126. }
  127. if (got_output) {
  128. printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
  129. fwrite(pkt.data, 1, pkt.size, f);
  130. av_packet_unref(&pkt);
  131. }
  132. }
  133. /* add sequence end code to have a real MPEG file */
  134. fwrite(endcode, 1, sizeof(endcode), f);
  135. fclose(f);
  136. avcodec_free_context(&c);
  137. av_freep(&picture->data[0]);
  138. av_frame_free(&picture);
  139. return 0;
  140. }