Advertisement
Guest User

Untitled

a guest
Dec 5th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. #include "RawVideoReader.h"
  2. #include <time.h>
  3. #include <cstdio>
  4. #define AV_OUTPUT_FORMAT "mpegts"
  5. #define AV_OUTPUT_CODEC  "libx264"
  6.  
  7. #define AV_OUTPUT_BITRATE 40000000
  8. #define AV_OUTPUT_THREADS 16
  9. #define AV_OUTPUT_THREAD_TYPE FF_THREAD_SLICE
  10.  
  11. extern "C"{
  12. #include <libavcodec/avcodec.h>
  13. #include <libavformat/avformat.h>
  14. #include <libswscale/swscale.h>
  15. #include <libavutil/avassert.h>
  16. }
  17. typedef struct {
  18.     AVFormatContext *formatCtx;
  19.     AVCodecContext  *codecCtx;
  20.     AVCodec         *encoder;
  21.     AVStream        *outStream;
  22.     AVFrame         *frame;
  23.     AVProgram       *program;
  24.     AVPacket        packet;
  25. } ff_output_t;
  26.  
  27. ff_output_t ff_output;
  28.  
  29. int BytesPerFrame;
  30.  
  31. AVDictionary    *codecOptions = NULL;
  32. AVInputFormat mpegts_mux;
  33. uint8_t * picture_buf;
  34.  
  35.  
  36. void convertFRame(Frame *f, AVFrame * ff_frame) {  
  37.     int size =(int) f->height * f->width;
  38.     printf("Alocou\n");
  39.  
  40.     ff_frame->data[0] =(uint8_t*) f->data;
  41.     ff_frame->data[1] = ff_frame->data[0] + size;
  42.     ff_frame->data[2] = ff_frame->data[1] + size / 4;
  43.  
  44.     ff_frame->linesize[0] = f->width;
  45.     ff_frame->linesize[1] = f->width / 2;
  46.     ff_frame->linesize[2] = f->width / 2;
  47.  
  48.     ff_frame->height = (int) f->height;
  49.     ff_frame->width = (int) f->width;
  50.     ff_frame->format = AV_PIX_FMT_YUV420P;
  51. }
  52.  
  53. void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb){
  54.     if (pkt->pts != AV_NOPTS_VALUE)
  55.         pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
  56.     if (pkt->dts != AV_NOPTS_VALUE)
  57.         pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
  58.     if (pkt->duration > 0)
  59.         pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
  60.     if (pkt->convergence_duration > 0)
  61.         pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
  62. }
  63.  
  64. int main()
  65. {
  66.  
  67.     char * videoOutputName = "1.ts";
  68.     int width = 4096, heigth = 2304;
  69.     //RawVideoReader reader(4096, 2304, 4, "CineMag005-70frames.rgba"); //RGBA32 - 4BPP
  70.     //RawVideoReader reader(4096, 2304, 1.5, "CineMag005-26frames.nv12"); //NV12 - 1.5BPP
  71.     RawVideoReader reader(4096, 2304, 1.5, "Cinemag005.yuv420p"); //YUV420P - 1.5BPP
  72.     BytesPerFrame =  avpicture_get_size(AV_PIX_FMT_YUV420P, 4096, 2304);
  73.    
  74.     av_register_all();
  75.     avformat_network_init();
  76.  
  77.     //Open OUTPUT
  78.    
  79.     if (avformat_alloc_output_context2(&ff_output.formatCtx, NULL, AV_OUTPUT_FORMAT, videoOutputName) < 0) {
  80.         printf("could not create output context\n");
  81.         return -1;
  82.     }
  83.     ff_output.encoder = avcodec_find_encoder_by_name(AV_OUTPUT_CODEC);
  84.    
  85.     if (ff_output.encoder == NULL) {
  86.         printf("Codec %s not found..\n", AV_OUTPUT_CODEC);
  87.         return -1;
  88.     }
  89.    
  90.     ff_output.outStream = avformat_new_stream(ff_output.formatCtx, ff_output.encoder);
  91.     if (ff_output.outStream == NULL) {
  92.         printf("Could not create output stream\n");
  93.         return -1;
  94.     }
  95.    
  96.     ff_output.outStream->id = ff_output.formatCtx->nb_streams - 1;
  97.    
  98.    
  99.     ff_output.codecCtx = avcodec_alloc_context3(ff_output.encoder);
  100.    
  101.     ff_output.codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  102.     ff_output.codecCtx->height = 2304;
  103.     ff_output.codecCtx->width = 4096;
  104.     ff_output.codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  105.    
  106.     //Set custom BIT RATE and THREADs
  107.     ff_output.codecCtx->bit_rate = AV_OUTPUT_BITRATE;
  108.     ff_output.codecCtx->thread_count = AV_OUTPUT_THREADS;
  109.     ff_output.codecCtx->thread_type = AV_OUTPUT_THREAD_TYPE;
  110.     ff_output.codecCtx->bit_rate = AV_OUTPUT_BITRATE;
  111.    
  112.     ff_output.formatCtx->bit_rate = AV_OUTPUT_BITRATE;
  113.     ff_output.codecCtx->bit_rate_tolerance = 0;
  114.     ff_output.codecCtx->rc_max_rate = 0;
  115.     ff_output.codecCtx->rc_buffer_size = 0;
  116.     ff_output.codecCtx->gop_size = 40;
  117.     ff_output.codecCtx->max_b_frames = 3;
  118.     ff_output.codecCtx->b_frame_strategy = 1;
  119.     ff_output.codecCtx->coder_type = 1;
  120.     ff_output.codecCtx->me_cmp = 1;
  121.     ff_output.codecCtx->me_range = 16;
  122.     ff_output.codecCtx->qmin = 10;
  123.     ff_output.codecCtx->qmax = 51;
  124.     ff_output.codecCtx->scenechange_threshold = 40;
  125.     ff_output.codecCtx->flags |= CODEC_FLAG_LOOP_FILTER;
  126.     ff_output.codecCtx->me_method = ME_HEX;
  127.     ff_output.codecCtx->me_subpel_quality = 5;
  128.     ff_output.codecCtx->i_quant_factor = 0.71;
  129.     ff_output.codecCtx->qcompress = 0.6;
  130.     ff_output.codecCtx->max_qdiff = 4;
  131.    
  132.     //Set custo timebase for codec and streams
  133.     ff_output.codecCtx->time_base.num = 1;
  134.     ff_output.codecCtx->time_base.den = 24;
  135.    
  136.     ff_output.formatCtx->streams[0]->codec->width = ff_output.codecCtx->width;
  137.     ff_output.formatCtx->streams[0]->codec->height = ff_output.codecCtx->height;
  138.     ff_output.formatCtx->streams[0]->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  139.     ff_output.formatCtx->streams[0]->codec->bit_rate = AV_OUTPUT_BITRATE;
  140.    
  141.     ff_output.outStream->avg_frame_rate = {24,1};
  142.    
  143.    
  144.     if (avcodec_open2(ff_output.codecCtx, ff_output.encoder, &codecOptions)) {
  145.         printf("Could not open output codec...\n");
  146.         return -1;
  147.     }
  148.     ff_output.formatCtx->iformat = &mpegts_mux;
  149.     av_dump_format(ff_output.formatCtx, 0, videoOutputName, 0);
  150.    
  151.     if (avio_open(&ff_output.formatCtx->pb, videoOutputName, AVIO_FLAG_WRITE)) {
  152.         printf("avio_open failed %s\n", videoOutputName);
  153.         return -1;
  154.     }
  155.     int inc = 0, gotPacket;
  156.    
  157.     Frame* f;
  158.     int valor = 0;
  159.    
  160.     while (1) {
  161.    
  162.         ff_output.frame = av_frame_alloc();
  163.         f = reader.getFrame(++valor);
  164.    
  165.         convertFRame(f, ff_output.frame);      
  166.    
  167.         ff_output.frame->pts = inc++;
  168.         ff_output.packet.data = NULL;
  169.         ff_output.packet.size = 0;
  170.         av_init_packet(&ff_output.packet);
  171.    
  172.         int ret = avcodec_encode_video2(ff_output.codecCtx, &ff_output.packet, ff_output.frame, &gotPacket);
  173.    
  174.         if (gotPacket) {
  175.    
  176.             ff_output.packet.stream_index = 0;
  177.             av_packet_rescale_ts(&ff_output.packet,
  178.                                  ff_output.codecCtx->time_base,
  179.                                  ff_output.outStream->time_base);
  180.    
  181.             if (av_interleaved_write_frame(ff_output.formatCtx, &ff_output.packet) < 0) {
  182.                 printf("Unable to write to output stream..\n");
  183.                 return 0;
  184.             }
  185.    
  186.         }
  187.         av_frame_free(&ff_output.frame);
  188.     }
  189.    
  190.     return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement