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.

176 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. static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
  33. FILE *outfile)
  34. {
  35. int ret;
  36. /* send the frame to the encoder */
  37. ret = avcodec_send_frame(enc_ctx, frame);
  38. if (ret < 0) {
  39. fprintf(stderr, "error sending a frame for encoding\n");
  40. exit(1);
  41. }
  42. while (ret >= 0) {
  43. ret = avcodec_receive_packet(enc_ctx, pkt);
  44. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  45. return;
  46. else if (ret < 0) {
  47. fprintf(stderr, "error during encoding\n");
  48. exit(1);
  49. }
  50. printf("encoded frame %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
  51. fwrite(pkt->data, 1, pkt->size, outfile);
  52. av_packet_unref(pkt);
  53. }
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. const char *filename;
  58. const AVCodec *codec;
  59. AVCodecContext *c= NULL;
  60. int i, ret, x, y;
  61. FILE *f;
  62. AVFrame *picture;
  63. AVPacket *pkt;
  64. uint8_t endcode[] = { 0, 0, 1, 0xb7 };
  65. if (argc <= 1) {
  66. fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
  67. exit(0);
  68. }
  69. filename = argv[1];
  70. avcodec_register_all();
  71. /* find the mpeg1video encoder */
  72. codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
  73. if (!codec) {
  74. fprintf(stderr, "codec not found\n");
  75. exit(1);
  76. }
  77. c = avcodec_alloc_context3(codec);
  78. picture = av_frame_alloc();
  79. pkt = av_packet_alloc();
  80. if (!pkt)
  81. exit(1);
  82. /* put sample parameters */
  83. c->bit_rate = 400000;
  84. /* resolution must be a multiple of two */
  85. c->width = 352;
  86. c->height = 288;
  87. /* frames per second */
  88. c->time_base = (AVRational){1, 25};
  89. c->framerate = (AVRational){25, 1};
  90. c->gop_size = 10; /* emit one intra frame every ten frames */
  91. c->max_b_frames=1;
  92. c->pix_fmt = AV_PIX_FMT_YUV420P;
  93. /* open it */
  94. if (avcodec_open2(c, codec, NULL) < 0) {
  95. fprintf(stderr, "could not open codec\n");
  96. exit(1);
  97. }
  98. f = fopen(filename, "wb");
  99. if (!f) {
  100. fprintf(stderr, "could not open %s\n", filename);
  101. exit(1);
  102. }
  103. picture->format = c->pix_fmt;
  104. picture->width = c->width;
  105. picture->height = c->height;
  106. ret = av_frame_get_buffer(picture, 32);
  107. if (ret < 0) {
  108. fprintf(stderr, "could not alloc the frame data\n");
  109. exit(1);
  110. }
  111. /* encode 1 second of video */
  112. for(i=0;i<25;i++) {
  113. fflush(stdout);
  114. /* make sure the frame data is writable */
  115. ret = av_frame_make_writable(picture);
  116. if (ret < 0)
  117. exit(1);
  118. /* prepare a dummy image */
  119. /* Y */
  120. for(y=0;y<c->height;y++) {
  121. for(x=0;x<c->width;x++) {
  122. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  123. }
  124. }
  125. /* Cb and Cr */
  126. for(y=0;y<c->height/2;y++) {
  127. for(x=0;x<c->width/2;x++) {
  128. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  129. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  130. }
  131. }
  132. picture->pts = i;
  133. /* encode the image */
  134. encode(c, picture, pkt, f);
  135. }
  136. /* flush the encoder */
  137. encode(c, NULL, pkt, f);
  138. /* add sequence end code to have a real MPEG file */
  139. fwrite(endcode, 1, sizeof(endcode), f);
  140. fclose(f);
  141. avcodec_free_context(&c);
  142. av_frame_free(&picture);
  143. av_packet_free(&pkt);
  144. return 0;
  145. }