encode_video.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2001 Fabrice Bellard
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * video encoding with libavcodec API example
  25. *
  26. * @example encode_video.c
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <libavcodec/avcodec.h>
  32. #include <libavutil/opt.h>
  33. #include <libavutil/imgutils.h>
  34. static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
  35. FILE *outfile)
  36. {
  37. int ret;
  38. /* send the frame to the encoder */
  39. if (frame)
  40. printf("Send frame %3"PRId64"\n", frame->pts);
  41. ret = avcodec_send_frame(enc_ctx, frame);
  42. if (ret < 0) {
  43. fprintf(stderr, "Error sending a frame for encoding\n");
  44. exit(1);
  45. }
  46. while (ret >= 0) {
  47. ret = avcodec_receive_packet(enc_ctx, pkt);
  48. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  49. return;
  50. else if (ret < 0) {
  51. fprintf(stderr, "Error during encoding\n");
  52. exit(1);
  53. }
  54. printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
  55. fwrite(pkt->data, 1, pkt->size, outfile);
  56. av_packet_unref(pkt);
  57. }
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. const char *filename, *codec_name;
  62. const AVCodec *codec;
  63. AVCodecContext *c= NULL;
  64. int i, ret, x, y;
  65. FILE *f;
  66. AVFrame *frame;
  67. AVPacket *pkt;
  68. uint8_t endcode[] = { 0, 0, 1, 0xb7 };
  69. if (argc <= 2) {
  70. fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
  71. exit(0);
  72. }
  73. filename = argv[1];
  74. codec_name = argv[2];
  75. /* find the mpeg1video encoder */
  76. codec = avcodec_find_encoder_by_name(codec_name);
  77. if (!codec) {
  78. fprintf(stderr, "Codec '%s' not found\n", codec_name);
  79. exit(1);
  80. }
  81. c = avcodec_alloc_context3(codec);
  82. if (!c) {
  83. fprintf(stderr, "Could not allocate video codec context\n");
  84. exit(1);
  85. }
  86. pkt = av_packet_alloc();
  87. if (!pkt)
  88. exit(1);
  89. /* put sample parameters */
  90. c->bit_rate = 400000;
  91. /* resolution must be a multiple of two */
  92. c->width = 352;
  93. c->height = 288;
  94. /* frames per second */
  95. c->time_base = (AVRational){1, 25};
  96. c->framerate = (AVRational){25, 1};
  97. /* emit one intra frame every ten frames
  98. * check frame pict_type before passing frame
  99. * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
  100. * then gop_size is ignored and the output of encoder
  101. * will always be I frame irrespective to gop_size
  102. */
  103. c->gop_size = 10;
  104. c->max_b_frames = 1;
  105. c->pix_fmt = AV_PIX_FMT_YUV420P;
  106. if (codec->id == AV_CODEC_ID_H264)
  107. av_opt_set(c->priv_data, "preset", "slow", 0);
  108. /* open it */
  109. ret = avcodec_open2(c, codec, NULL);
  110. if (ret < 0) {
  111. fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
  112. exit(1);
  113. }
  114. f = fopen(filename, "wb");
  115. if (!f) {
  116. fprintf(stderr, "Could not open %s\n", filename);
  117. exit(1);
  118. }
  119. frame = av_frame_alloc();
  120. if (!frame) {
  121. fprintf(stderr, "Could not allocate video frame\n");
  122. exit(1);
  123. }
  124. frame->format = c->pix_fmt;
  125. frame->width = c->width;
  126. frame->height = c->height;
  127. ret = av_frame_get_buffer(frame, 0);
  128. if (ret < 0) {
  129. fprintf(stderr, "Could not allocate the video frame data\n");
  130. exit(1);
  131. }
  132. /* encode 1 second of video */
  133. for (i = 0; i < 25; i++) {
  134. fflush(stdout);
  135. /* make sure the frame data is writable */
  136. ret = av_frame_make_writable(frame);
  137. if (ret < 0)
  138. exit(1);
  139. /* prepare a dummy image */
  140. /* Y */
  141. for (y = 0; y < c->height; y++) {
  142. for (x = 0; x < c->width; x++) {
  143. frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
  144. }
  145. }
  146. /* Cb and Cr */
  147. for (y = 0; y < c->height/2; y++) {
  148. for (x = 0; x < c->width/2; x++) {
  149. frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
  150. frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
  151. }
  152. }
  153. frame->pts = i;
  154. /* encode the image */
  155. encode(c, frame, pkt, f);
  156. }
  157. /* flush the encoder */
  158. encode(c, NULL, pkt, f);
  159. /* add sequence end code to have a real MPEG file */
  160. if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
  161. fwrite(endcode, 1, sizeof(endcode), f);
  162. fclose(f);
  163. avcodec_free_context(&c);
  164. av_frame_free(&frame);
  165. av_packet_free(&pkt);
  166. return 0;
  167. }