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.

196 lines
5.3KB

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