Advertisement
Guest User

Untitled

a guest
Jun 5th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1.  
  2.  
  3. void AviWriter::openFile(QString name, unsigned int width, unsigned int height, float fps)
  4. {
  5. LOG_INFO("Opening file %s, properties: [width: %d, height: %d, fps: %d]",
  6. name.toAscii().data(), width, height, fps);
  7.  
  8. av_log_set_level(AV_LOG_DEBUG);
  9.  
  10. // presentation timestamp
  11. m_avpkt.pts = 0;
  12. // decompresion timestamp
  13. m_avpkt.dts = 0;
  14. // position in stream, -1 -> unknown
  15. m_avpkt.pos = -1;
  16. // duration of this packet in AVStream->time_base units
  17. // 0 -> unknown
  18. m_avpkt.duration = 0;
  19. // Time diff in av stream units between pts of this packet and point at which
  20. // output from the decoder has converged independent from availability of previous frame.
  21. m_avpkt.convergence_duration = 0;
  22. // AV_PKT_FLAG
  23. m_avpkt.flags = 0;
  24. m_avpkt.stream_index = 0;
  25. m_avpkt.destruct = NULL;
  26.  
  27. av_register_all();
  28. avcodec_register_all();
  29.  
  30. m_avOutputFormat = av_guess_format("avi", NULL, NULL);
  31. if( !m_avOutputFormat )
  32. {
  33. std::string msg("Cannot find suitable output format");
  34. LOG_ERROR(msg.c_str());
  35. throw AviException(msg);
  36. }
  37.  
  38. m_codecp = avcodec_find_decoder(CODEC_ID_MPEG4);
  39. if(m_codecp == NULL)
  40. {
  41. std::string msg("avcodec_find_decoder failed");
  42. LOG_ERROR(msg.c_str());
  43. throw AviException(msg);
  44. }
  45.  
  46. m_avFormatCtx = avformat_alloc_context();
  47. if(m_avFormatCtx == NULL)
  48. {
  49. std::string msg("Couldn't allocate memory for output format");
  50. LOG_ERROR(msg.c_str());
  51. throw AviException(msg);
  52. }
  53. m_avFormatCtx->oformat = m_avOutputFormat;
  54.  
  55. LOG_INFO("Setting filename to av output format");
  56. //qsnprintf(m_avFormatCtx->filename, sizeof(m_avFormatCtx->filename), "%s",name.toAscii().data());
  57. m_avFormatCtx->max_delay = (int)(0.7 * AV_TIME_BASE);
  58. m_avCodecCtx = avcodec_alloc_context3(m_codecp);
  59.  
  60. if( !m_avCodecCtx )
  61. LOG_INFO( "Can not create codec context" );
  62.  
  63. m_picture = avcodec_alloc_frame();
  64. m_avCodecCtx->width = width;
  65. m_avCodecCtx->height = height;
  66. m_avCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  67. m_avCodecCtx->pix_fmt = PIX_FMT_YUV420P;
  68.  
  69. /*
  70. * Convert FPS float value to AVRational (time_base value)
  71. * A bit of explanation:
  72.  
  73. * time_base field is something opposite to framerate. The FPS value tell how much frames are rendered in one second.
  74. * Time_base (timebase ? time-base ? time base ? How to spell that crap ?) is a measure that tells you how much time it takes
  75. * to render one frame, hence time_base = 1/FPS.
  76. *
  77. * AVCodec doc says we should use 1/FPS for fixed framerate, or just time base as it is.
  78. *
  79. * Here i convert float fps value to AVRational struct (fractions made of integers, clever) then i reverse denominator and nominator.
  80. */
  81.  
  82. AVRational one = av_d2q(fps, std::numeric_limits<int>::max() );
  83. m_avCodecCtx->time_base.num = one.den;
  84. m_avCodecCtx->time_base.den = one.num;
  85. m_avCodecCtx->has_b_frames = 1;
  86. m_avCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  87.  
  88. if (avcodec_open2(m_avCodecCtx, m_codecp, NULL) < 0)
  89. {
  90. std::string msg("avcodec_open failed !");
  91. LOG_ERROR(msg.c_str());
  92. throw AviException(msg);
  93. }
  94.  
  95. m_avVideo = avformat_new_stream(m_avFormatCtx, m_codecp);
  96. if(!m_avVideo)
  97. {
  98. std::string msg("av_new_stream failed: Could not alloc video stream");
  99. LOG_ERROR(msg.c_str());
  100. throw AviException(msg);
  101. }
  102. m_avVideo->codec = m_avCodecCtx;
  103.  
  104. if(nRitaConfig.debug())
  105. {
  106. LOG_DEBUG("Dump format");
  107. av_dump_format(m_avFormatCtx, 0, name.toAscii().data(), 1);
  108. }
  109.  
  110. if (!(m_avOutputFormat->flags & AVFMT_NOFILE))
  111. {
  112. LOG_INFO("url_fopen");
  113.  
  114. if (avio_open(&m_avFormatCtx->pb, name.toAscii().data(), AVIO_FLAG_WRITE) < 0)
  115. {
  116. std::string msg("Could not open file, avio_open failed");
  117. LOG_ERROR(msg.c_str());
  118. throw AviException(msg);
  119. }
  120. }
  121.  
  122. writeHeader();
  123. }
  124.  
  125. void AviWriter::writeHeader()
  126. {
  127. LOG_INFO("Writing header");
  128. avformat_write_header(m_avFormatCtx, NULL);
  129. }
  130.  
  131. void AviWriter::close()
  132. {
  133. LOG_INFO("Closing %s file", m_aviFile.fileName().toAscii().data());
  134. av_write_trailer(m_avFormatCtx);
  135. av_freep(&m_avFormatCtx->streams[0]->codec);
  136. av_freep(&m_avFormatCtx->streams[0]);
  137.  
  138. if (!(m_avOutputFormat->flags & AVFMT_NOFILE))
  139. {
  140. // close the output file
  141. avio_close(m_avFormatCtx->pb);
  142. }
  143. av_free(m_avFormatCtx);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement