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.

192 lines
5.4KB

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