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.

172 lines
4.5KB

  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. picture->format = c->pix_fmt;
  78. picture->width = c->width;
  79. picture->height = c->height;
  80. ret = av_frame_get_buffer(picture, 32);
  81. if (ret < 0) {
  82. fprintf(stderr, "could not alloc the frame data\n");
  83. exit(1);
  84. }
  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. /* make sure the frame data is writable */
  92. ret = av_frame_make_writable(picture);
  93. if (ret < 0)
  94. exit(1);
  95. /* prepare a dummy image */
  96. /* Y */
  97. for(y=0;y<c->height;y++) {
  98. for(x=0;x<c->width;x++) {
  99. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  100. }
  101. }
  102. /* Cb and Cr */
  103. for(y=0;y<c->height/2;y++) {
  104. for(x=0;x<c->width/2;x++) {
  105. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  106. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  107. }
  108. }
  109. picture->pts = i;
  110. /* encode the image */
  111. ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
  112. if (ret < 0) {
  113. fprintf(stderr, "error encoding frame\n");
  114. exit(1);
  115. }
  116. if (got_output) {
  117. printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
  118. fwrite(pkt.data, 1, pkt.size, f);
  119. av_packet_unref(&pkt);
  120. }
  121. }
  122. /* get the delayed frames */
  123. for (got_output = 1; got_output; i++) {
  124. fflush(stdout);
  125. ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
  126. if (ret < 0) {
  127. fprintf(stderr, "error encoding frame\n");
  128. exit(1);
  129. }
  130. if (got_output) {
  131. printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
  132. fwrite(pkt.data, 1, pkt.size, f);
  133. av_packet_unref(&pkt);
  134. }
  135. }
  136. /* add sequence end code to have a real MPEG file */
  137. fwrite(endcode, 1, sizeof(endcode), f);
  138. fclose(f);
  139. avcodec_free_context(&c);
  140. av_frame_free(&picture);
  141. return 0;
  142. }