关键词搜索

源码搜索 ×
×

FFmpeg音视频编解码示例

发布2018-01-11浏览3069次

详情内容

FFmpeg示例程序集:https://www3.nd.edu/~sjiang1/docio/ffmpeg_doc/globals_eval.html

FFmpeg音视频编解码示例:https://www3.nd.edu/~sjiang1/docio/ffmpeg_doc/decoding_encoding_8c-example.html#a19

视频编码

  1. /*
  2. * Video encoding example
  3. */
  4. static void video_encode_example(const char *filename, int codec_id)
  5. {
  6. AVCodec *codec;
  7. AVCodecContext *c= NULL;
  8. int i, ret, x, y, got_output;
  9. FILE *f;
  10. AVFrame *frame;
  11. AVPacket pkt;
  12. uint8_t endcode[] = { 0, 0, 1, 0xb7 };
  13. printf("Encode video file %s\n", filename);
  14. /* find the mpeg1 video encoder */
  15. codec = avcodec_find_encoder(codec_id);
  16. if (!codec) {
  17. fprintf(stderr, "Codec not found\n");
  18. exit(1);
  19. }
  20. c = avcodec_alloc_context3(codec);
  21. if (!c) {
  22. fprintf(stderr, "Could not allocate video codec context\n");
  23. exit(1);
  24. }
  25. /* put sample parameters */
  26. c->bit_rate = 400000;
  27. /* resolution must be a multiple of two */
  28. c->width = 352;
  29. c->height = 288;
  30. /* frames per second */
  31. c->time_base = (AVRational){1,25};
  32. /* emit one intra frame every ten frames
  33. * check frame pict_type before passing frame
  34. * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
  35. * then gop_size is ignored and the output of encoder
  36. * will always be I frame irrespective to gop_size
  37. */
  38. c->gop_size = 10;
  39. c->max_b_frames = 1;
  40. c->pix_fmt = AV_PIX_FMT_YUV420P;
  41. if (codec_id == AV_CODEC_ID_H264)
  42. av_opt_set(c->priv_data, "preset", "slow", 0);
  43. /* open it */
  44. if (avcodec_open2(c, codec, NULL) < 0) {
  45. fprintf(stderr, "Could not open codec\n");
  46. exit(1);
  47. }
  48. f = fopen(filename, "wb");
  49. if (!f) {
  50. fprintf(stderr, "Could not open %s\n", filename);
  51. exit(1);
  52. }
  53. frame = av_frame_alloc();
  54. if (!frame) {
  55. fprintf(stderr, "Could not allocate video frame\n");
  56. exit(1);
  57. }
  58. frame->format = c->pix_fmt;
  59. frame->width = c->width;
  60. frame->height = c->height;
  61. /* the image can be allocated by any means and av_image_alloc() is
  62. * just the most convenient way if av_malloc() is to be used */
  63. ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
  64. c->pix_fmt, 32);
  65. if (ret < 0) {
  66. fprintf(stderr, "Could not allocate raw picture buffer\n");
  67. exit(1);
  68. }
  69. /* encode 1 second of video */
  70. for (i = 0; i < 25; i++) {
  71. av_init_packet(&pkt);
  72. pkt.data = NULL; // packet data will be allocated by the encoder
  73. pkt.size = 0;
  74. fflush(stdout);
  75. /* prepare a dummy image */
  76. /* Y */
  77. for (y = 0; y < c->height; y++) {
  78. for (x = 0; x < c->width; x++) {
  79. frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
  80. }
  81. }
  82. /* Cb and Cr */
  83. for (y = 0; y < c->height/2; y++) {
  84. for (x = 0; x < c->width/2; x++) {
  85. frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
  86. frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
  87. }
  88. }
  89. frame->pts = i;
  90. /* encode the image */
  91. ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
  92. if (ret < 0) {
  93. fprintf(stderr, "Error encoding frame\n");
  94. exit(1);
  95. }
  96. if (got_output) {
  97. printf("Write frame %3d (size=%5d)\n", i, pkt.size);
  98. fwrite(pkt.data, 1, pkt.size, f);
  99. av_packet_unref(&pkt);
  100. }
  101. }
  102. /* get the delayed frames */
  103. for (got_output = 1; got_output; i++) {
  104. fflush(stdout);
  105. ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
  106. if (ret < 0) {
  107. fprintf(stderr, "Error encoding frame\n");
  108. exit(1);
  109. }
  110. if (got_output) {
  111. printf("Write frame %3d (size=%5d)\n", i, pkt.size);
  112. fwrite(pkt.data, 1, pkt.size, f);
  113. av_packet_unref(&pkt);
  114. }
  115. }
  116. /* add sequence end code to have a real mpeg file */
  117. fwrite(endcode, 1, sizeof(endcode), f);
  118. fclose(f);
  119. avcodec_close(c);
  120. av_free(c);
  121. av_freep(&frame->data[0]);
  122. av_frame_free(&frame);
  123. printf("\n");
  124. }


视频解码

  1. /*
  2. * Video decoding example
  3. */
  4. static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
  5. char *filename)
  6. {
  7. FILE *f;
  8. int i;
  9. f = fopen(filename,"w");
  10. fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
  11. for (i = 0; i < ysize; i++)
  12. fwrite(buf + i * wrap, 1, xsize, f);
  13. fclose(f);
  14. }
  15. static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
  16. AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
  17. {
  18. int len, got_frame;
  19. char buf[1024];
  20. len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
  21. if (len < 0) {
  22. fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
  23. return len;
  24. }
  25. if (got_frame) {
  26. printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
  27. fflush(stdout);
  28. /* the picture is allocated by the decoder, no need to free it */
  29. snprintf(buf, sizeof(buf), outfilename, *frame_count);
  30. pgm_save(frame->data[0], frame->linesize[0],
  31. frame->width, frame->height, buf);
  32. (*frame_count)++;
  33. }
  34. if (pkt->data) {
  35. pkt->size -= len;
  36. pkt->data += len;
  37. }
  38. return 0;
  39. }
  40. static void video_decode_example(const char *outfilename, const char *filename)
  41. {
  42. AVCodec *codec;
  43. AVCodecContext *c= NULL;
  44. int frame_count;
  45. FILE *f;
  46. AVFrame *frame;
  47. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  48. AVPacket avpkt;
  49. av_init_packet(&avpkt);
  50. /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
  51. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  52. printf("Decode video file %s to %s\n", filename, outfilename);
  53. /* find the mpeg1 video decoder */
  54. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  55. if (!codec) {
  56. fprintf(stderr, "Codec not found\n");
  57. exit(1);
  58. }
  59. c = avcodec_alloc_context3(codec);
  60. if (!c) {
  61. fprintf(stderr, "Could not allocate video codec context\n");
  62. exit(1);
  63. }
  64. if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
  65. c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
  66. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  67. MUST be initialized there because this information is not
  68. available in the bitstream. */
  69. /* open it */
  70. if (avcodec_open2(c, codec, NULL) < 0) {
  71. fprintf(stderr, "Could not open codec\n");
  72. exit(1);
  73. }
  74. f = fopen(filename, "rb");
  75. if (!f) {
  76. fprintf(stderr, "Could not open %s\n", filename);
  77. exit(1);
  78. }
  79. frame = av_frame_alloc();
  80. if (!frame) {
  81. fprintf(stderr, "Could not allocate video frame\n");
  82. exit(1);
  83. }
  84. frame_count = 0;
  85. for (;;) {
  86. avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
  87. if (avpkt.size == 0)
  88. break;
  89. /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
  90. and this is the only method to use them because you cannot
  91. know the compressed data size before analysing it.
  92. BUT some other codecs (msmpeg4, mpeg4) are inherently frame
  93. based, so you must call them with all the data for one
  94. frame exactly. You must also initialize 'width' and
  95. 'height' before initializing them. */
  96. /* NOTE2: some codecs allow the raw parameters (frame size,
  97. sample rate) to be changed at any frame. We handle this, so
  98. you should also take care of it */
  99. /* here, we use a stream based decoder (mpeg1video), so we
  100. feed decoder and see if it could decode a frame */
  101. avpkt.data = inbuf;
  102. while (avpkt.size > 0)
  103. if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
  104. exit(1);
  105. }
  106. /* some codecs, such as MPEG, transmit the I and P frame with a
  107. latency of one frame. You must do the following to have a
  108. chance to get the last frame of the video */
  109. avpkt.data = NULL;
  110. avpkt.size = 0;
  111. decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
  112. fclose(f);
  113. avcodec_close(c);
  114. av_free(c);
  115. av_frame_free(&frame);
  116. printf("\n");
  117. }


音频编码

  1. /*
  2. * Audio encoding example
  3. */
  4. static void audio_encode_example(const char *filename)
  5. {
  6. AVCodec *codec;
  7. AVCodecContext *c= NULL;
  8. AVFrame *frame;
  9. AVPacket pkt;
  10. int i, j, k, ret, got_output;
  11. int buffer_size;
  12. FILE *f;
  13. uint16_t *samples;
  14. float t, tincr;
  15. printf("Encode audio file %s\n", filename);
  16. /* find the MP2 encoder */
  17. codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
  18. if (!codec) {
  19. fprintf(stderr, "Codec not found\n");
  20. exit(1);
  21. }
  22. c = avcodec_alloc_context3(codec);
  23. if (!c) {
  24. fprintf(stderr, "Could not allocate audio codec context\n");
  25. exit(1);
  26. }
  27. /* put sample parameters */
  28. c->bit_rate = 64000;
  29. /* check that the encoder supports s16 pcm input */
  30. c->sample_fmt = AV_SAMPLE_FMT_S16;
  31. if (!check_sample_fmt(codec, c->sample_fmt)) {
  32. fprintf(stderr, "Encoder does not support sample format %s",
  33. av_get_sample_fmt_name(c->sample_fmt));
  34. exit(1);
  35. }
  36. /* select other audio parameters supported by the encoder */
  37. c->sample_rate = select_sample_rate(codec);
  38. c->channel_layout = select_channel_layout(codec);
  39. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  40. /* open it */
  41. if (avcodec_open2(c, codec, NULL) < 0) {
  42. fprintf(stderr, "Could not open codec\n");
  43. exit(1);
  44. }
  45. f = fopen(filename, "wb");
  46. if (!f) {
  47. fprintf(stderr, "Could not open %s\n", filename);
  48. exit(1);
  49. }
  50. /* frame containing input raw audio */
  51. frame = av_frame_alloc();
  52. if (!frame) {
  53. fprintf(stderr, "Could not allocate audio frame\n");
  54. exit(1);
  55. }
  56. frame->nb_samples = c->frame_size;
  57. frame->format = c->sample_fmt;
  58. frame->channel_layout = c->channel_layout;
  59. /* the codec gives us the frame size, in samples,
  60. * we calculate the size of the samples buffer in bytes */
  61. buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
  62. c->sample_fmt, 0);
  63. if (buffer_size < 0) {
  64. fprintf(stderr, "Could not get sample buffer size\n");
  65. exit(1);
  66. }
  67. samples = av_malloc(buffer_size);
  68. if (!samples) {
  69. fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
  70. buffer_size);
  71. exit(1);
  72. }
  73. /* setup the data pointers in the AVFrame */
  74. ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
  75. (const uint8_t*)samples, buffer_size, 0);
  76. if (ret < 0) {
  77. fprintf(stderr, "Could not setup audio frame\n");
  78. exit(1);
  79. }
  80. /* encode a single tone sound */
  81. t = 0;
  82. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  83. for (i = 0; i < 200; i++) {
  84. av_init_packet(&pkt);
  85. pkt.data = NULL; // packet data will be allocated by the encoder
  86. pkt.size = 0;
  87. for (j = 0; j < c->frame_size; j++) {
  88. samples[2*j] = (int)(sin(t) * 10000);
  89. for (k = 1; k < c->channels; k++)
  90. samples[2*j + k] = samples[2*j];
  91. t += tincr;
  92. }
  93. /* encode the samples */
  94. ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
  95. if (ret < 0) {
  96. fprintf(stderr, "Error encoding audio frame\n");
  97. exit(1);
  98. }
  99. if (got_output) {
  100. fwrite(pkt.data, 1, pkt.size, f);
  101. av_packet_unref(&pkt);
  102. }
  103. }
  104. /* get the delayed frames */
  105. for (got_output = 1; got_output; i++) {
  106. ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
  107. if (ret < 0) {
  108. fprintf(stderr, "Error encoding frame\n");
  109. exit(1);
  110. }
  111. if (got_output) {
  112. fwrite(pkt.data, 1, pkt.size, f);
  113. av_packet_unref(&pkt);
  114. }
  115. }
  116. fclose(f);
  117. av_freep(&samples);
  118. av_frame_free(&frame);
  119. avcodec_close(c);
  120. av_free(c);
  121. }


音频解码

  1. /*
  2. * Audio decoding.
  3. */
  4. static void audio_decode_example(const char *outfilename, const char *filename)
  5. {
  6. AVCodec *codec;
  7. AVCodecContext *c= NULL;
  8. int len;
  9. FILE *f, *outfile;
  10. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  11. AVPacket avpkt;
  12. AVFrame *decoded_frame = NULL;
  13. av_init_packet(&avpkt);
  14. printf("Decode audio file %s to %s\n", filename, outfilename);
  15. /* find the mpeg audio decoder */
  16. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  17. if (!codec) {
  18. fprintf(stderr, "Codec not found\n");
  19. exit(1);
  20. }
  21. c = avcodec_alloc_context3(codec);
  22. if (!c) {
  23. fprintf(stderr, "Could not allocate audio codec context\n");
  24. exit(1);
  25. }
  26. /* open it */
  27. if (avcodec_open2(c, codec, NULL) < 0) {
  28. fprintf(stderr, "Could not open codec\n");
  29. exit(1);
  30. }
  31. f = fopen(filename, "rb");
  32. if (!f) {
  33. fprintf(stderr, "Could not open %s\n", filename);
  34. exit(1);
  35. }
  36. outfile = fopen(outfilename, "wb");
  37. if (!outfile) {
  38. av_free(c);
  39. exit(1);
  40. }
  41. /* decode until eof */
  42. avpkt.data = inbuf;
  43. avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  44. while (avpkt.size > 0) {
  45. int i, ch;
  46. int got_frame = 0;
  47. if (!decoded_frame) {
  48. if (!(decoded_frame = av_frame_alloc())) {
  49. fprintf(stderr, "Could not allocate audio frame\n");
  50. exit(1);
  51. }
  52. }
  53. len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
  54. if (len < 0) {
  55. fprintf(stderr, "Error while decoding\n");
  56. exit(1);
  57. }
  58. if (got_frame) {
  59. /* if a frame has been decoded, output it */
  60. int data_size = av_get_bytes_per_sample(c->sample_fmt);
  61. if (data_size < 0) {
  62. /* This should not occur, checking just for paranoia */
  63. fprintf(stderr, "Failed to calculate data size\n");
  64. exit(1);
  65. }
  66. for (i=0; i<decoded_frame->nb_samples; i++)
  67. for (ch=0; ch<c->channels; ch++)
  68. fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
  69. }
  70. avpkt.size -= len;
  71. avpkt.data += len;
  72. avpkt.dts =
  73. avpkt.pts = AV_NOPTS_VALUE;
  74. if (avpkt.size < AUDIO_REFILL_THRESH) {
  75. /* Refill the input buffer, to avoid trying to decode
  76. * incomplete frames. Instead of this, one could also use
  77. * a parser, or use a proper container format through
  78. * libavformat. */
  79. memmove(inbuf, avpkt.data, avpkt.size);
  80. avpkt.data = inbuf;
  81. len = fread(avpkt.data + avpkt.size, 1,
  82. AUDIO_INBUF_SIZE - avpkt.size, f);
  83. if (len > 0)
  84. avpkt.size += len;
  85. }
  86. }
  87. fclose(outfile);
  88. fclose(f);
  89. avcodec_close(c);
  90. av_free(c);
  91. av_frame_free(&decoded_frame);
  92. }


相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载