Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. static int decode_packet(int *got_frame, int cached)
  2. {
  3. int ret = 0;
  4. int decoded = pkt.size;
  5.  
  6. *got_frame = 0;
  7.  
  8. if (pkt.stream_index == video_stream_idx) {
  9. /* decode video frame */
  10. ret = avcodec_send_packet(video_dec_ctx, &pkt);
  11. if (ret < 0) {
  12. fprintf(stderr, "Error submitting the packet to the decoder\n");
  13. exit(1);
  14. }
  15.  
  16. /* read all the output frames (in general there may be any number of them */
  17. while (ret >= 0) {
  18. ret = avcodec_receive_frame(video_dec_ctx, frame);
  19. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  20. return decoded;
  21. else if (ret < 0) {
  22. fprintf(stderr, "Error during decoding\n");
  23. exit(1);
  24. }
  25.  
  26. if (frame->width != width || frame->height != height ||
  27. frame->format != pix_fmt) {
  28. /* To handle this change, one could call av_image_alloc again and
  29. * decode the following frames into another rawvideo file. */
  30. fprintf(stderr, "Error: Width, height and pixel format have to be "
  31. "constant in a rawvideo file, but the width, height or "
  32. "pixel format of the input video changed:\n"
  33. "old: width = %d, height = %d, format = %s\n"
  34. "new: width = %d, height = %d, format = %s\n",
  35. width, height, av_get_pix_fmt_name(pix_fmt),
  36. frame->width, frame->height,
  37. av_get_pix_fmt_name((AVPixelFormat)frame->format));
  38. return -1;
  39. }
  40.  
  41. printf("video_frame%s n:%d coded_n:%d\n",
  42. cached ? "(cached)" : "",
  43. video_frame_count++, frame->coded_picture_number);
  44.  
  45. /* copy decoded frame to destination buffer:
  46. * this is required since rawvideo expects non aligned data */
  47. av_image_copy(video_dst_data, video_dst_linesize,
  48. (const uint8_t **)(frame->data), frame->linesize,
  49. pix_fmt, width, height);
  50.  
  51. /* write to rawvideo file */
  52. //fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
  53. }
  54. }
  55. else if (pkt.stream_index == audio_stream_idx) {
  56. /* decode audio frame */
  57. ret = avcodec_send_packet(audio_dec_ctx, &pkt);
  58. if (ret < 0) {
  59. fprintf(stderr, "Error submitting the packet to the decoder\n");
  60. exit(1);
  61. }
  62.  
  63. /* read all the output frames (in general there may be any number of them */
  64. while (ret >= 0) {
  65. ret = avcodec_receive_frame(audio_dec_ctx, frame);
  66. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  67. return decoded;
  68. else if (ret < 0) {
  69. fprintf(stderr, "Error during decoding\n");
  70. exit(1);
  71. }
  72.  
  73. size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)frame->format);
  74. /*printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
  75. cached ? "(cached)" : "",
  76. audio_frame_count++, frame->nb_samples,
  77. av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));*/
  78.  
  79. /* Write the raw audio data samples of the first plane. This works
  80. * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
  81. * most audio decoders output planar audio, which uses a separate
  82. * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
  83. * In other words, this code will write only the first audio channel
  84. * in these cases.
  85. * You should use libswresample or libavfilter to convert the frame
  86. * to packed data. */
  87.  
  88. fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
  89.  
  90.  
  91. }
  92. }
  93.  
  94. /* If we use frame reference counting, we own the data and need
  95. * to de-reference it when we don't use it anymore */
  96. if (*got_frame && refcount)
  97. av_frame_unref(frame);
  98.  
  99. return decoded;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement