Advertisement
Guest User

ffmpeg_basic_transcode.c

a guest
May 25th, 2011
3,953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.33 KB | None | 0 0
  1. /* Decode -encode */
  2.  
  3. #include <libavcodec/avcodec.h>
  4. #include <libavformat/avformat.h>
  5. #include <libswscale/swscale.h>
  6. #include <libavutil/avstring.h>
  7.  
  8. #include <stdio.h>
  9. #include <inttypes.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12.  
  13. AVFormatContext *pFormatCtxIn1;
  14. AVFormatContext *pFormatCtxOut;
  15. const char *filein1, *fileout;
  16. AVOutputFormat *fmt;
  17.  
  18. int i, j;
  19. AVCodecContext *pCodecCtxIn1;
  20. AVCodecContext *pCodecCtxOut;
  21. uint8_t *outbuf;
  22. int outbuf_size = 0;
  23. AVCodec *input_codecs1[MAX_STREAMS];
  24. AVCodec *output_codecs[MAX_STREAMS];
  25. AVFrame *pFrame, *pFrameRGB;
  26. AVPacket packet, pkt;
  27. int got_picture;
  28. int out_size;
  29. int numBytes;
  30. uint8_t *buffer;
  31. int videoStream1;
  32.  
  33. typedef struct AVOutputStream {
  34.     int file_index; /* file index */
  35.     int index; /* stream index in the output file */
  36.     int source_index; /* AVInputStream index */
  37.     AVStream *st; /* stream in the output file */
  38.     int encoding_needed; /* true if encoding needed for this stream */
  39.     int frame_number;
  40.     /* input pts and corresponding output pts
  41.      for A/V sync */
  42.     struct AVInputStream *sync_ist; /* input stream to sync against */
  43.     int64_t sync_opts;
  44.     /* video only */
  45.     struct SwsContext *img_resample_ctx; /* for image resampling */
  46.     int resample_height;
  47.     int resample_width;
  48.     int resample_pix_fmt;
  49.  
  50.     /* full frame size of first frame */
  51.     int original_height;
  52.     int original_width;
  53.  
  54. } AVOutputStream;
  55.  
  56. typedef struct AVInputStream {
  57.     int file_index;
  58.     int index;
  59.     AVStream *st;
  60.     int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  61.     int64_t sample_index; /* current sample */
  62.  
  63.     int64_t next_pts; /* synthetic pts for cases where pkt.pts
  64.      is not defined */
  65.     int64_t pts; /* current pts */
  66.     int is_start; /* is 1 at the start and after a discontinuity */
  67. } AVInputStream;
  68.  
  69. AVOutputStream *ost, **ost_table = NULL;
  70. AVInputStream *ist1, **ist_table1 = NULL;
  71.  
  72. int new_video_stream(AVFormatContext *oc, AVStream *ist) {
  73.     AVStream *st;
  74.     AVCodecContext *video_enc;
  75.     float frame_aspect_ratio = 0;
  76.     AVCodec *codec;
  77.     enum CodecID codec_id;
  78.  
  79.     st = av_new_stream(oc, oc->nb_streams);
  80.     if (!st) {
  81.         fprintf(stderr, "Could not alloc stream\n");
  82.         return -1;
  83.     }
  84.  
  85.     avcodec_get_context_defaults2(st->codec, AVMEDIA_TYPE_VIDEO);
  86.  
  87.     video_enc = st->codec;
  88.  
  89.     codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL,
  90.             AVMEDIA_TYPE_VIDEO);
  91.     codec = avcodec_find_encoder(codec_id);
  92.  
  93.     if (ist->sample_aspect_ratio.num)
  94.         frame_aspect_ratio = av_q2d(ist->sample_aspect_ratio);
  95.     else
  96.         frame_aspect_ratio *= (float) ist->codec->width / ist->codec->height;
  97.  
  98.     video_enc->codec_id = codec_id;
  99.  
  100.     video_enc->codec_type = AVMEDIA_TYPE_VIDEO;
  101.     video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
  102.  
  103.     video_enc->time_base.den = ist->codec->time_base.den;
  104.     video_enc->time_base.num = ist->codec->time_base.num;
  105.  
  106.     video_enc->bit_rate = 512000;
  107.     video_enc->width = ist->codec->width;
  108.     video_enc->height = ist->codec->height;
  109.  
  110.     video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio
  111.             * video_enc->height / video_enc->width, 255);
  112.     video_enc->pix_fmt = ist->codec->pix_fmt;
  113.     video_enc->gop_size = ist->codec->gop_size;
  114.     st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
  115.  
  116.     st->avg_frame_rate.den = ist->avg_frame_rate.den;
  117.     st->avg_frame_rate.num = ist->avg_frame_rate.num;
  118.  
  119.     video_enc->max_b_frames = ist->codec->max_b_frames;
  120.  
  121.     if (codec && codec->pix_fmts) {
  122.         const enum PixelFormat *p = codec->pix_fmts;
  123.         for (; *p != -1; p++) {
  124.             if (*p == video_enc->pix_fmt)
  125.                 break;
  126.         }
  127.         if (*p == -1)
  128.             video_enc->pix_fmt = codec->pix_fmts[0];
  129.     }
  130.  
  131.     video_enc->rc_override_count = 0;
  132.     video_enc->me_threshold = 0;
  133.     video_enc->intra_dc_precision = 0;
  134.  
  135.     return 0;
  136. }
  137.  
  138. int new_audio_stream(AVFormatContext *oc, AVStream *ist) {
  139.     AVStream *st;
  140.     AVCodecContext *audio_enc;
  141.  
  142.     st = av_new_stream(oc, oc->nb_streams);
  143.     if (!st) {
  144.         fprintf(stderr, "Could not alloc stream\n");
  145.         return -1;
  146.     }
  147.     avcodec_get_context_defaults2(st->codec, AVMEDIA_TYPE_AUDIO);
  148.  
  149.     audio_enc = st->codec;
  150.     audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
  151.  
  152.     audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
  153.  
  154.     audio_enc->codec_id = CODEC_ID_MP3;
  155.     st->stream_copy = 1;
  156.     audio_enc->bit_rate = 56000;
  157.     audio_enc->extradata = ist->codec->extradata;
  158.     audio_enc->extradata_size = ist->codec->extradata_size;
  159.     audio_enc->channels = ist->codec->channels;
  160.  
  161.     audio_enc->sample_rate = 22050;
  162.     audio_enc->time_base.den = 1;
  163.     audio_enc->time_base.num = audio_enc->sample_rate;
  164.  
  165.     return 0;
  166. }
  167. int init_input_context(int argc, char *argv[]) {
  168.     if (argc != 3) {
  169.         printf("use: %s input_file1 output_file\n", argv[0]);
  170.         return -1;
  171.     }
  172.  
  173.     avcodec_register_all();
  174.     av_register_all();
  175.  
  176.     filein1 = argv[1];
  177.  
  178.     pFormatCtxIn1 = avformat_alloc_context();
  179.     if (!pFormatCtxIn1) {
  180.         fprintf(stderr, "Memory error\n");
  181.         return -1;
  182.     }
  183.  
  184.     if (av_open_input_file(&pFormatCtxIn1, filein1, NULL, 0, NULL) != 0) {
  185.         printf("Cannot open the file: %s\n", filein1);
  186.         return -1;
  187.     }
  188.  
  189.     fileout = argv[2];
  190.  
  191.     return 0;
  192. }
  193.  
  194. int init_output_context() {
  195.     fmt = av_guess_format(NULL, fileout, NULL);
  196.     if (!fmt) {
  197.         printf("Cannot guess output format: using MPEG.\n");
  198.         fmt = av_guess_format("mpeg", NULL, NULL);
  199.     }
  200.     if (!fmt) {
  201.         fprintf(stderr, "Cannot find any output format\n");
  202.         return -1;
  203.     }
  204.  
  205.     pFormatCtxOut = avformat_alloc_context();
  206.     if (!pFormatCtxOut) {
  207.         fprintf(stderr, "Memory \n");
  208.         return -1;
  209.     }
  210.     pFormatCtxOut->oformat = fmt;
  211.     av_strlcpy(pFormatCtxOut->filename, fileout,
  212.             sizeof(pFormatCtxOut->filename));
  213.     pFormatCtxOut->timestamp = 0;
  214.     if (url_fopen(&pFormatCtxOut->pb, fileout, URL_WRONLY) < 0) {
  215.         fprintf(stderr, "Cannot open '%s'\n", fileout);
  216.         return -1;
  217.     }
  218.     return 0;
  219. }
  220.  
  221. int init_input_stream() {
  222.     if (av_find_stream_info(pFormatCtxIn1) < 0)
  223.         return -1;
  224.     printf(
  225.             "\n ---------------------------------------------------------------------- \n");
  226.     dump_format(pFormatCtxIn1, 0, filein1, 0);
  227.     printf(
  228.             "\n ---------------------------------------------------------------------- \n");
  229.  
  230.     // Init input streams
  231.     ist_table1 = (AVInputStream **) av_mallocz(pFormatCtxIn1->nb_streams
  232.             * sizeof(AVInputStream *));
  233.     if (!ist_table1)
  234.         return -1;
  235.     // first allocate memory for av input stream
  236.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  237.         ist1 = (AVInputStream *) av_mallocz(sizeof(AVInputStream));
  238.         if (!ist1)
  239.             return -1;
  240.         ist_table1[i] = ist1;
  241.     }
  242.     // allocate input video stream to input stream
  243.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  244.         ist1 = ist_table1[i];
  245.         ist1->st = pFormatCtxIn1->streams[i];
  246.         ist1->file_index = 0;
  247.         ist1->index = i;
  248.     }
  249.  
  250.     return 0;
  251. }
  252.  
  253. int init_output_stream() {
  254.     // Init output streams
  255.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  256.         AVCodecContext *enc = pFormatCtxIn1->streams[i]->codec;
  257.         switch (enc->codec_type) {
  258.         case AVMEDIA_TYPE_AUDIO:
  259.             if (new_audio_stream(pFormatCtxOut, pFormatCtxIn1->streams[i]) != 0)
  260.                 return -1;
  261.             break;
  262.         case AVMEDIA_TYPE_VIDEO:
  263.             if (new_video_stream(pFormatCtxOut, pFormatCtxIn1->streams[i]) != 0)
  264.                 return -1;
  265.             break;
  266.         default:
  267.             break;
  268.         }
  269.     }
  270.  
  271.     if (!pFormatCtxOut->nb_streams) {
  272.         fprintf(stderr, "Output file %s doesn't contain any stream\n", fileout);
  273.         return -1;
  274.     }
  275.  
  276.     ost_table = (AVOutputStream **) av_mallocz(pFormatCtxOut->nb_streams
  277.             * sizeof(AVOutputStream *));
  278.     if (!ost_table)
  279.         return -1;
  280.  
  281.     for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  282.         ost = (AVOutputStream *) av_mallocz(sizeof(AVOutputStream));
  283.         if (!ost)
  284.             return -1;
  285.         ost_table[i] = ost;
  286.     }
  287.  
  288.     return 0;
  289. }
  290. int verify_streams_coreleation() {
  291.     // verify input and output streams co relation.
  292.     for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  293.         ost = ost_table[i];
  294.         ost->st = pFormatCtxOut->streams[i];
  295.         ost->file_index = 0;
  296.         ost->index = i;
  297.         int found = 0;
  298.         for (j = 0; j < pFormatCtxIn1->nb_streams; j++) {
  299.             ist1 = ist_table1[j];
  300.             if (ist1->st->codec->codec_type == ost->st->codec->codec_type) {
  301.                 ost->source_index = j;
  302.                 found = 1;
  303.                 break;
  304.             }
  305.         }
  306.  
  307.         if (!found) {
  308.             fprintf(stderr,
  309.                     "Some output stream doesn't fit with any input stream\n");
  310.             return -1;
  311.         }
  312.         ist1 = ist_table1[ost->source_index];
  313.         ost->sync_ist = ist1;
  314.     }
  315.  
  316.     return 0;
  317. }
  318. int verify_input_videostream() {
  319.     // verify if video stream is present in input file.
  320.     videoStream1 = -1;
  321.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  322.         if (pFormatCtxIn1->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  323.             videoStream1 = i;
  324.             break;
  325.         }
  326.     }
  327.     if (videoStream1 == -1) {
  328.         fprintf(stderr, "There is no video stream in the input file");
  329.         return -1;
  330.     }
  331.     return 0;
  332. }
  333. int set_metadata() {
  334.     for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  335.         ost = ost_table[i];
  336.         ist1 = ist_table1[ost->source_index];
  337.  
  338.         pCodecCtxOut = ost->st->codec;
  339.         pCodecCtxIn1 = ist1->st->codec;
  340.  
  341.         AVMetadataTag *lang;
  342.         if ((lang = av_metadata_get(ist1->st->metadata, "language", NULL, 0))
  343.                 && !av_metadata_get(ost->st->metadata, "language", NULL, 0))
  344.             av_metadata_set2(&ost->st->metadata, "language", lang->value, 0);
  345.  
  346.         ost->st->disposition = ist1->st->disposition;
  347.         pCodecCtxOut->bits_per_raw_sample = pCodecCtxIn1->bits_per_raw_sample;
  348.         pCodecCtxOut->chroma_sample_location
  349.                 = pCodecCtxIn1->chroma_sample_location;
  350.  
  351.         if (pCodecCtxOut->codec_type == AVMEDIA_TYPE_VIDEO) {
  352.             if (pCodecCtxIn1->pix_fmt == PIX_FMT_NONE) {
  353.                 fprintf(stderr,
  354.                         "Pixel format unknown. The stream can't be decoded\n");
  355.                 return -1;
  356.             }
  357.             ost->encoding_needed = 1;
  358.             ist1->decoding_needed = 1;
  359.             int size = pCodecCtxOut->width * pCodecCtxOut->height;
  360.             outbuf_size = FFMAX(outbuf_size, 6*size + 200);
  361.         }
  362.     }
  363.     // set the output buffer size
  364.     outbuf = NULL;
  365.     outbuf = (uint8_t *) av_malloc(outbuf_size);
  366.     return 0;
  367. }
  368.  
  369. int show_output_parameters() {
  370.     // Output parameters
  371.     if (av_set_parameters(pFormatCtxOut, NULL) < 0) {
  372.         fprintf(stderr, "Invalid output parameters\n");
  373.         return -1;
  374.     }
  375.  
  376.     dump_format(pFormatCtxOut, 0, fileout, 1);
  377.  
  378.     printf("\n\n\n ---------------------------------");
  379.     return 0;
  380. }
  381. int open_encoders_decoders() {
  382.     // Open encoders
  383.     for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  384.         ost = ost_table[i];
  385.         if (ost->encoding_needed) {
  386.             AVCodec *codec = output_codecs[i];
  387.             codec = avcodec_find_encoder(ost->st->codec->codec_id);
  388.             printf("\n codec-id is %d \n", ost->st->codec->codec_id);
  389.             if (!codec) {
  390.                 fprintf(stderr, "Cannot find any encoder for the output file\n");
  391.                 return -1;
  392.             }
  393.             if (avcodec_open(ost->st->codec, codec) < 0) {
  394.                 fprintf(stderr, "Error opening encoder\n");
  395.                 return -1;
  396.             }
  397.         }
  398.     }
  399.  
  400.     // Open decoders
  401.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  402.         ist1 = ist_table1[i];
  403.         if (ist1->decoding_needed) {
  404.             AVCodec *codec = input_codecs1[i];
  405.             codec = avcodec_find_decoder(ist1->st->codec->codec_id);
  406.             if (!codec) {
  407.                 fprintf(stderr, "Cannot find any decoder for the input file\n");
  408.                 return -1;
  409.             }
  410.             if (avcodec_open(ist1->st->codec, codec) < 0) {
  411.                 fprintf(stderr, "Error opening decoder\n");
  412.                 return -1;
  413.             }
  414.         }
  415.     }
  416.     return 0;
  417. }
  418.  
  419. int init_pts() {
  420.     // Init the pts
  421.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  422.         ist1 = ist_table1[i];
  423.         ist1->pts = 0;
  424.         ist1->next_pts = AV_NOPTS_VALUE;
  425.         ist1->is_start = 1;
  426.     }
  427.     return 0;
  428. }
  429. int allocate_frame_buffer() {
  430.     pFrame = avcodec_alloc_frame();
  431.     // With this var we will work with RGB
  432.     pFrameRGB = avcodec_alloc_frame();
  433.     if (pFrame == NULL || pFrameRGB == NULL)
  434.         return -1;
  435.  
  436.     numBytes = avpicture_get_size(PIX_FMT_YUV420P,
  437.             ist_table1[videoStream1]->st->codec->width,
  438.             ist_table1[videoStream1]->st->codec->height);
  439.     buffer = malloc(numBytes * sizeof(uint8_t));
  440.     avpicture_fill((AVPicture *) pFrameRGB, buffer, PIX_FMT_YUV420P,
  441.             ist_table1[videoStream1]->st->codec->width,
  442.             ist_table1[videoStream1]->st->codec->height);
  443.  
  444.     return 0;
  445. }
  446.  
  447. int write_header() {
  448.     printf("\n Writing Header \n");
  449.  
  450.     if (av_write_header(pFormatCtxOut) < 0) {
  451.         fprintf(stderr, "Error writing file header\n");
  452.         return -1;
  453.     }
  454.     return 0;
  455. }
  456. int read_packet_loop() {
  457.     while (av_read_frame(pFormatCtxIn1, &packet) >= 0) {
  458.         //printf("%d \n", packet.stream_index);
  459.         ist1 = ist_table1[packet.stream_index];
  460.         int64_t offset = 0 - pFormatCtxIn1->start_time;
  461.         AVRational avrat_base = { 1, AV_TIME_BASE };
  462.  
  463.         if (packet.dts != AV_NOPTS_VALUE)
  464.             packet.dts += av_rescale_q(offset, avrat_base, ist1->st->time_base);
  465.         if (packet.pts != AV_NOPTS_VALUE)
  466.             packet.pts += av_rescale_q(offset, avrat_base, ist1->st->time_base);
  467.  
  468.         if (ist1->next_pts == AV_NOPTS_VALUE)
  469.             ist1->next_pts = ist1->pts;
  470.         if (packet.dts != AV_NOPTS_VALUE)
  471.             ist1->next_pts = ist1->pts = av_rescale_q(packet.dts,
  472.                     ist1->st->time_base, avrat_base);
  473.         ist1->pts = ist1->next_pts;
  474.  
  475.         if (ist1->st->codec->codec_type == AVMEDIA_TYPE_VIDEO
  476.                 && ist1->decoding_needed) {
  477.  
  478.             avcodec_get_frame_defaults(pFrame);
  479.  
  480.             if (avcodec_decode_video2(ist1->st->codec, pFrame, &got_picture,
  481.                     &packet) < 0) {
  482.                 fprintf(stderr, "Error decoding video\n");
  483.                 return -1;
  484.             }
  485.             ist1->st->quality = pFrame->quality;
  486.  
  487.             if (got_picture) {
  488.  
  489.                 if (ist1->st->codec->time_base.num != 0) {
  490.                     int ticks =
  491.                             ist1->st->parser ? ist1->st->parser->repeat_pict
  492.                                     + 1 : ist1->st->codec->ticks_per_frame;
  493.                     ist1->next_pts += ((int64_t) AV_TIME_BASE
  494.                             * ist1->st->codec->time_base.num * ticks)
  495.                             / ist1->st->codec->time_base.den;
  496.                 }
  497.                 for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  498.                     ost = ost_table[i];
  499.                     if (ost->source_index == packet.stream_index
  500.                             && ost->encoding_needed
  501.                             && ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  502.                         av_init_packet(&pkt);
  503.                         pkt.stream_index = ost->index;
  504.  
  505.                         AVFrame big_picture;
  506.  
  507.                         big_picture = *pFrame;
  508.  
  509.                         big_picture.interlaced_frame = pFrame->interlaced_frame;
  510.                         big_picture.quality = ist1->st->quality;
  511.                         big_picture.pict_type = 0;
  512.                         big_picture.pts = ost->sync_opts;
  513.  
  514.                         out_size = avcodec_encode_video(ost->st->codec, outbuf,
  515.                                 outbuf_size, &big_picture);
  516.  
  517.                         if (out_size < 0) {
  518.                             fprintf(stderr, "Error video encoding\n");
  519.                             return -1;
  520.                         } else {
  521.                             //printf("%d\n",out_size);
  522.                             pkt.data = outbuf;
  523.                             pkt.size = out_size;
  524.                             if (ost->st->codec->coded_frame->pts
  525.                                     != AV_NOPTS_VALUE)
  526.                                 pkt.pts = av_rescale_q(
  527.                                         ost->st->codec->coded_frame->pts,
  528.                                         ost->st->codec->time_base,
  529.                                         ost->st->time_base);
  530.                             //pkt.pts = pFrame->pts;
  531.                             if (ost->st->codec->coded_frame->key_frame)
  532.                                 pkt.flags |= PKT_FLAG_KEY;
  533.                             if (av_interleaved_write_frame(pFormatCtxOut, &pkt)
  534.                                     < 0) {
  535.                                 fprintf(stderr,
  536.                                         "Error writing frame in the output file\n");
  537.                                 return -1;
  538.                             } else {
  539.                                 //printf("\n written encoded video nframe %d \n",pFrame->coded_picture_number);
  540.                             }
  541.                         }
  542.  
  543.                         av_free_packet(&pkt);
  544.                         ost->sync_opts++;
  545.                         ost->frame_number++;
  546.                     }
  547.                 }
  548.             }
  549.         } else {
  550.             //printf(" \n audio encoding \n");
  551.  
  552.             ist1->next_pts += ((int64_t) AV_TIME_BASE
  553.                     * ist1->st->codec->frame_size)
  554.                     / ist1->st->codec->sample_rate;
  555.             for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  556.                 ost = ost_table[i];
  557.                 if (ost->source_index == packet.stream_index) {
  558.                     AVFrame directframe;
  559.                     AVPacket opkt;
  560.                     int64_t ost_tb_start_time = av_rescale_q(0, avrat_base,
  561.                             ost->st->time_base);
  562.                     av_init_packet(&opkt);
  563.  
  564.                     avcodec_get_frame_defaults(&directframe);
  565.                     ost->st->codec->coded_frame = &directframe;
  566.                     directframe.key_frame = packet.flags & PKT_FLAG_KEY;
  567.                     opkt.stream_index = packet.stream_index;
  568.  
  569.                     if (packet.pts != AV_NOPTS_VALUE)
  570.                         opkt.pts = av_rescale_q(packet.pts,
  571.                                 ist1->st->time_base, ost->st->time_base)
  572.                                 - ost_tb_start_time;
  573.                     else
  574.                         opkt.pts = AV_NOPTS_VALUE;
  575.  
  576.                     if (packet.dts == AV_NOPTS_VALUE)
  577.                         opkt.dts = av_rescale_q(ist1->pts, avrat_base,
  578.                                 ost->st->time_base);
  579.                     else
  580.                         opkt.dts = av_rescale_q(packet.dts,
  581.                                 ist1->st->time_base, ost->st->time_base);
  582.                     opkt.dts -= ost_tb_start_time;
  583.  
  584.                     opkt.duration = av_rescale_q(packet.duration,
  585.                             ist1->st->time_base, ost->st->time_base);
  586.                     opkt.flags = packet.flags;
  587.  
  588.                     if (ost->st->codec->codec_id != CODEC_ID_H264
  589.                             && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
  590.                             && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
  591.                         if (av_parser_change(ist1->st->parser, ost->st->codec,
  592.                                 &opkt.data, &opkt.size, packet.data,
  593.                                 packet.size, packet.flags & PKT_FLAG_KEY))
  594.                             opkt.destruct = av_destruct_packet;
  595.                     } else {
  596.                         opkt.data = packet.data;
  597.                         opkt.size = packet.size;n
  598.                     }
  599.  
  600.                     if (av_interleaved_write_frame(pFormatCtxOut, &opkt) < 0) {
  601.                         fprintf(stderr,
  602.                                 "Error writing frame in the output file\n");
  603.                         return -1;
  604.                     } else {
  605.                         //printf("\n written encoded audio  frame %d \n",ost->st->codec->frame_number);
  606.  
  607.                     }
  608.                     ost->st->codec->frame_number++;
  609.                     ost->frame_number++;
  610.                     av_free_packet(&opkt);
  611.                 }
  612.             }
  613.  
  614.         }
  615.  
  616.         av_free_packet(&packet);
  617.     }
  618.     return 0;
  619. }
  620. int av_cleanup() {
  621.     for (i = 0; i < pFormatCtxOut->nb_streams; i++) {
  622.         ost = ost_table[i];
  623.         if (ost->encoding_needed) {
  624.             av_freep(&ost->st->codec->stats_in);
  625.             avcodec_close(ost->st->codec);
  626.         }
  627.         av_metadata_free(&pFormatCtxOut->streams[i]->metadata);
  628.         av_free(pFormatCtxOut->streams[i]->codec);
  629.         av_free(pFormatCtxOut->streams[i]);
  630.  
  631.         av_free(ost);
  632.     }
  633.     av_free(ost_table);
  634.  
  635.     for (i = 0; i < pFormatCtxIn1->nb_streams; i++) {
  636.         ist1 = ist_table1[i];
  637.         if (ist1->decoding_needed) {
  638.             avcodec_close(ist1->st->codec);
  639.         }
  640.         av_free(ist1);
  641.     }
  642.     av_free(ist_table1);
  643.  
  644.     free(buffer);
  645.     av_free(pFrameRGB);
  646.  
  647.     av_free(pFrame);
  648.  
  649.     av_freep(&outbuf);
  650.  
  651.     av_close_input_file(pFormatCtxIn1);
  652.  
  653.     if (!(fmt->flags & AVFMT_NOFILE)) {
  654.         url_fclose(pFormatCtxOut->pb);
  655.     }
  656.  
  657.     av_metadata_free(&pFormatCtxOut->metadata);
  658.     av_free(pFormatCtxOut);
  659.     return 0;
  660. }
  661.  
  662. int main(int argc, char *argv[]) {
  663.  
  664.     init_input_context(argc, argv);
  665.     init_output_context();
  666.     init_input_stream();
  667.     init_output_stream();
  668.     verify_streams_coreleation();
  669.     verify_input_videostream();
  670.     set_metadata();
  671.     show_output_parameters();
  672.     open_encoders_decoders();
  673.     init_pts();
  674.     allocate_frame_buffer();
  675.     write_header();
  676.     read_packet_loop();
  677.     av_write_trailer(pFormatCtxOut);
  678.     av_cleanup();
  679.     return 0;
  680. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement