Advertisement
Guest User

Untitled

a guest
Jan 7th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.80 KB | None | 0 0
  1. diff --git a/ffmpeg.c b/ffmpeg.c
  2. index 5ccbf10..631ddfd 100644
  3. --- a/ffmpeg.c
  4. +++ b/ffmpeg.c
  5. @@ -547,7 +547,7 @@ static void update_benchmark(const char *fmt, ...)
  6.      }
  7.  }
  8.  
  9. -static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  10. +static int write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  11.  {
  12.      AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
  13.      AVCodecContext          *avctx = ost->st->codec;
  14. @@ -567,7 +567,7 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  15.      if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
  16.          if (ost->frame_number >= ost->max_frames) {
  17.              av_free_packet(pkt);
  18. -            return;
  19. +            return 0;
  20.          }
  21.          ost->frame_number++;
  22.      }
  23. @@ -647,8 +647,9 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  24.      ret = av_interleaved_write_frame(s, pkt);
  25.      if (ret < 0) {
  26.          print_error("av_interleaved_write_frame()", ret);
  27. -        exit_program(1);
  28.      }
  29. +
  30. +    return ret;
  31.  }
  32.  
  33.  static void close_output_stream(OutputStream *ost)
  34. @@ -675,19 +676,20 @@ static int check_recording_time(OutputStream *ost)
  35.      return 1;
  36.  }
  37.  
  38. -static void do_audio_out(AVFormatContext *s, OutputStream *ost,
  39. +static int do_audio_out(AVFormatContext *s, OutputStream *ost,
  40.                           AVFrame *frame)
  41.  {
  42.      AVCodecContext *enc = ost->st->codec;
  43.      AVPacket pkt;
  44.      int got_packet = 0;
  45. +    int ret = 0;
  46.  
  47.      av_init_packet(&pkt);
  48.      pkt.data = NULL;
  49.      pkt.size = 0;
  50.  
  51.      if (!check_recording_time(ost))
  52. -        return;
  53. +        return 0;
  54.  
  55.      if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
  56.          frame->pts = ost->sync_opts;
  57. @@ -717,13 +719,15 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost,
  58.          }
  59.  
  60.          audio_size += pkt.size;
  61. -        write_frame(s, &pkt, ost);
  62. +        ret = write_frame(s, &pkt, ost);
  63.  
  64.          av_free_packet(&pkt);
  65.      }
  66. +
  67. +    return ret;
  68.  }
  69.  
  70. -static void do_subtitle_out(AVFormatContext *s,
  71. +static int do_subtitle_out(AVFormatContext *s,
  72.                              OutputStream *ost,
  73.                              InputStream *ist,
  74.                              AVSubtitle *sub)
  75. @@ -733,12 +737,13 @@ static void do_subtitle_out(AVFormatContext *s,
  76.      AVCodecContext *enc;
  77.      AVPacket pkt;
  78.      int64_t pts;
  79. +    int ret = 0;
  80.  
  81.      if (sub->pts == AV_NOPTS_VALUE) {
  82.          av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
  83.          if (exit_on_error)
  84.              exit_program(1);
  85. -        return;
  86. +        return 0;
  87.      }
  88.  
  89.      enc = ost->st->codec;
  90. @@ -762,7 +767,7 @@ static void do_subtitle_out(AVFormatContext *s,
  91.      for (i = 0; i < nb; i++) {
  92.          ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
  93.          if (!check_recording_time(ost))
  94. -            return;
  95. +            return 0;
  96.  
  97.          sub->pts = pts;
  98.          // start_display_time is required to be 0
  99. @@ -792,15 +797,19 @@ static void do_subtitle_out(AVFormatContext *s,
  100.                  pkt.pts += 90 * sub->end_display_time;
  101.          }
  102.          subtitle_size += pkt.size;
  103. -        write_frame(s, &pkt, ost);
  104. +        ret = write_frame(s, &pkt, ost);
  105. +        if (ret < 0)
  106. +            break;
  107.      }
  108. +
  109. +    return ret;
  110.  }
  111.  
  112. -static void do_video_out(AVFormatContext *s,
  113. +static int do_video_out(AVFormatContext *s,
  114.                           OutputStream *ost,
  115.                           AVFrame *in_picture)
  116.  {
  117. -    int ret, format_video_sync;
  118. +    int ret = 0, format_video_sync;
  119.      AVPacket pkt;
  120.      AVCodecContext *enc = ost->st->codec;
  121.      int nb_frames, i;
  122. @@ -870,12 +879,12 @@ static void do_video_out(AVFormatContext *s,
  123.      if (nb_frames == 0) {
  124.          nb_frames_drop++;
  125.          av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
  126. -        return;
  127. +        return 0;
  128.      } else if (nb_frames > 1) {
  129.          if (nb_frames > dts_error_threshold * 30) {
  130.              av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
  131.              nb_frames_drop++;
  132. -            return;
  133. +            return 0;
  134.          }
  135.          nb_frames_dup += nb_frames - 1;
  136.          av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);
  137. @@ -894,7 +903,7 @@ static void do_video_out(AVFormatContext *s,
  138.  #else
  139.      if (ost->frame_number >= ost->max_frames)
  140.  #endif
  141. -        return;
  142. +        return 0;
  143.  
  144.      if (s->oformat->flags & AVFMT_RAWPICTURE &&
  145.          enc->codec->id == AV_CODEC_ID_RAWVIDEO) {
  146. @@ -913,7 +922,7 @@ static void do_video_out(AVFormatContext *s,
  147.          pkt.flags |= AV_PKT_FLAG_KEY;
  148.  
  149.          video_size += pkt.size;
  150. -        write_frame(s, &pkt, ost);
  151. +        ret = write_frame(s, &pkt, ost);
  152.      } else {
  153.          int got_packet, forced_keyframe = 0;
  154.          double pts_time;
  155. @@ -994,7 +1003,7 @@ static void do_video_out(AVFormatContext *s,
  156.  
  157.              frame_size = pkt.size;
  158.              video_size += pkt.size;
  159. -            write_frame(s, &pkt, ost);
  160. +            ret = write_frame(s, &pkt, ost);
  161.              av_free_packet(&pkt);
  162.  
  163.              /* if two pass, output log */
  164. @@ -1013,7 +1022,12 @@ static void do_video_out(AVFormatContext *s,
  165.  
  166.      if (vstats_filename && frame_size)
  167.          do_video_stats(ost, frame_size);
  168. +
  169. +    if (ret < 0)
  170. +        break;
  171.    }
  172. +
  173. +    return ret;
  174.  }
  175.  
  176.  static double psnr(double d)
  177. @@ -1075,6 +1089,9 @@ static int reap_filters(void)
  178.          OutputFile    *of = output_files[ost->file_index];
  179.          int ret = 0;
  180.  
  181. +        if (ost->finished)
  182. +            continue;
  183. +
  184.          if (!ost->filter)
  185.              continue;
  186.  
  187. @@ -1113,7 +1130,7 @@ static int reap_filters(void)
  188.                  if (!ost->frame_aspect_ratio.num)
  189.                      ost->st->codec->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
  190.  
  191. -                do_video_out(of->ctx, ost, filtered_frame);
  192. +                ret = do_video_out(of->ctx, ost, filtered_frame);
  193.                  break;
  194.              case AVMEDIA_TYPE_AUDIO:
  195.                  filtered_frame->pts = frame_pts;
  196. @@ -1123,7 +1140,7 @@ static int reap_filters(void)
  197.                             "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
  198.                      break;
  199.                  }
  200. -                do_audio_out(of->ctx, ost, filtered_frame);
  201. +                ret = do_audio_out(of->ctx, ost, filtered_frame);
  202.                  break;
  203.              default:
  204.                  // TODO support subtitle filters
  205. @@ -1131,6 +1148,11 @@ static int reap_filters(void)
  206.              }
  207.  
  208.              av_frame_unref(filtered_frame);
  209. +
  210. +            if (ret < 0) {
  211. +                close_output_stream(ost);
  212. +                break;
  213. +            }
  214.          }
  215.      }
  216.  
  217. @@ -1324,6 +1346,9 @@ static void flush_encoders(void)
  218.          AVFormatContext *os = output_files[ost->file_index]->ctx;
  219.          int stop_encoding = 0;
  220.  
  221. +        if (ost->finished)
  222. +            continue;
  223. +
  224.          if (!ost->encoding_needed)
  225.              continue;
  226.  
  227. @@ -1380,7 +1405,11 @@ static void flush_encoders(void)
  228.                      pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
  229.                  if (pkt.duration > 0)
  230.                      pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
  231. -                write_frame(os, &pkt, ost);
  232. +                ret = write_frame(os, &pkt, ost);
  233. +                if (ret < 0) {
  234. +                    stop_encoding = 1;
  235. +                    break;
  236. +                }
  237.                  if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
  238.                      do_video_stats(ost, pkt.size);
  239.                  }
  240. @@ -1409,7 +1438,7 @@ static int check_output_constraints(InputStream *ist, OutputStream *ost)
  241.      return 1;
  242.  }
  243.  
  244. -static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
  245. +static int do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
  246.  {
  247.      OutputFile *of = output_files[ost->file_index];
  248.      InputFile   *f = input_files [ist->file_index];
  249. @@ -1418,27 +1447,28 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
  250.      int64_t ist_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base);
  251.      AVPicture pict;
  252.      AVPacket opkt;
  253. +    int ret = 0;
  254.  
  255.      av_init_packet(&opkt);
  256.  
  257.      if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
  258.          !ost->copy_initial_nonkeyframes)
  259. -        return;
  260. +        return 0;
  261.  
  262.      if (pkt->pts == AV_NOPTS_VALUE) {
  263.          if (!ost->frame_number && ist->pts < start_time &&
  264.              !ost->copy_prior_start)
  265. -            return;
  266. +            return 0;
  267.      } else {
  268.          if (!ost->frame_number && pkt->pts < ist_tb_start_time &&
  269.              !ost->copy_prior_start)
  270. -            return;
  271. +            return 0;
  272.      }
  273.  
  274.      if (of->recording_time != INT64_MAX &&
  275.          ist->pts >= of->recording_time + start_time) {
  276.          close_output_stream(ost);
  277. -        return;
  278. +        return 0;
  279.      }
  280.  
  281.      if (f->recording_time != INT64_MAX) {
  282. @@ -1447,7 +1477,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
  283.              start_time += f->start_time;
  284.          if (ist->pts >= f->recording_time + start_time) {
  285.              close_output_stream(ost);
  286. -            return;
  287. +            return 0;
  288.          }
  289.      }
  290.  
  291. @@ -1512,8 +1542,10 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
  292.          opkt.flags |= AV_PKT_FLAG_KEY;
  293.      }
  294.  
  295. -    write_frame(of->ctx, &opkt, ost);
  296. +    ret = write_frame(of->ctx, &opkt, ost);
  297.      ost->st->codec->frame_number++;
  298. +
  299. +    return ret;
  300.  }
  301.  
  302.  int guess_input_channel_layout(InputStream *ist)
  303. @@ -1836,10 +1868,15 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
  304.      for (i = 0; i < nb_output_streams; i++) {
  305.          OutputStream *ost = output_streams[i];
  306.  
  307. +        if (ost->finished)
  308. +            continue;
  309. +
  310.          if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
  311.              continue;
  312.  
  313. -        do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle);
  314. +        ret = do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle);
  315. +        if (ret < 0)
  316. +            close_output_stream(ost);
  317.      }
  318.  
  319.  out:
  320. @@ -1981,7 +2018,10 @@ static int output_packet(InputStream *ist, const AVPacket *pkt)
  321.          if (!check_output_constraints(ist, ost) || ost->encoding_needed)
  322.              continue;
  323.  
  324. -        do_streamcopy(ist, ost, pkt);
  325. +        ret = do_streamcopy(ist, ost, pkt);
  326. +        if (ret < 0) {
  327. +            close_output_stream(ost);
  328. +        }
  329.      }
  330.  
  331.      return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement