static int read_depends_file(const char *dirname, const char *start_name, struct list_head *list) { char *modules_dep_name; char *line; struct index_file *modules_dep; nofail_asprintf(&modules_dep_name, "%s/%s", dirname, "modules.dep.bin"); modules_dep = index_file_open(modules_dep_name); if (!modules_dep) { free(modules_dep_name); return 0; } line = index_search(modules_dep, start_name); if (line) { /* Value is standard dependency line format */ if (!add_modules_dep_line(line, start_name, list, dirname)) fatal("Module index is inconsistent\n"); free(line); } index_file_close(modules_dep); free(modules_dep_name); return 1; }
void create_segments(struct segment_context * ctx) { long max_tsfiles = 0; char *max_tsfiles_check; double prev_segment_time = -1; unsigned int output_index = ctx->start_from; AVInputFormat *ifmt; AVOutputFormat *ofmt; AVFormatContext *ic = NULL; AVFormatContext *oc; AVStream *video_st = NULL; AVStream *audio_st = NULL;; AVCodec *codec; char *output_filename; char *output_format; int video_index; int audio_index; unsigned int first_segment = 1; unsigned int last_segment = 0; int decode_done; int ret; int i; int remove_file; int write_index = -1; av_register_all(); ifmt = av_find_input_format("mpegts"); if (!ifmt) { fprintf(stderr, "Could not find MPEG-TS demuxer\n"); exit(1); } ret = avformat_open_input(&ic, ctx->input, ifmt, NULL); if (ret != 0) { fprintf(stderr, "Could not open input file, make sure it is an mpegts file: %d\n", ret); exit(1); } if (avformat_find_stream_info(ic, NULL) < 0) { fprintf(stderr, "Could not read stream information\n"); exit(1); } ofmt = av_guess_format("mpegts", NULL, NULL); if (!ofmt) { fprintf(stderr, "Could not find MPEG-TS muxer\n"); exit(1); } write_index = index_file_open(ctx); if(write_index >= 0) index_file_write_headers(ctx); oc = avformat_alloc_context(); if (!oc) { fprintf(stderr, "Could not allocated output context"); exit(1); } oc->oformat = ofmt; video_index = -1; audio_index = -1; for (i = 0; i < ic->nb_streams && (video_index < 0 || audio_index < 0); i++) { switch (ic->streams[i]->codec->codec_type) { case AVMEDIA_TYPE_VIDEO: video_index = i; ic->streams[i]->discard = AVDISCARD_NONE; video_st = add_output_stream(oc, ic->streams[i]); break; case AVMEDIA_TYPE_AUDIO: audio_index = i; ic->streams[i]->discard = AVDISCARD_NONE; audio_st = add_output_stream(oc, ic->streams[i]); break; default: ic->streams[i]->discard = AVDISCARD_ALL; break; } } av_dump_format(oc, 0, ctx->output_prefix, 1); if(video_st) { codec = avcodec_find_decoder(video_st->codec->codec_id); if (!codec) { fprintf(stderr, "Could not find video decoder, key frames will not be honored\n"); } if (avcodec_open2(video_st->codec, codec, NULL) < 0) { fprintf(stderr, "Could not open video decoder, key frames will not be honored\n"); } } output_filename = malloc(sizeof(char) * (strlen(ctx->output_prefix) + 15)); if (!output_filename) { fprintf(stderr, "Could not allocate space for output filename\n"); exit(1); } output_format = malloc(sizeof(char) * (strlen(ctx->output_prefix) + 15)); if (!output_format) { fprintf(stderr, "Could not allocate space for output format\n"); exit(1); } sprintf(output_format, "%%s%s%%0%uu.ts", ctx->separator, ctx->precision); sprintf(output_filename, output_format, ctx->output_prefix, output_index++); if (avio_open(&oc->pb, output_filename, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open '%s'\n", output_filename); exit(1); } if (avformat_write_header(oc, NULL) < 0) { fprintf(stderr, "Could not write mpegts header to first output file\n"); exit(1); } AVPacket packet; do { double segment_time; decode_done = av_read_frame(ic, &packet); if (decode_done < 0) { break; } if (prev_segment_time < 0) { if (packet.stream_index == video_index) { prev_segment_time = packet.pts * av_q2d(video_st->time_base); } else { prev_segment_time = packet.pts * av_q2d(audio_st->time_base); } } if (av_dup_packet(&packet) < 0) { fprintf(stderr, "Could not duplicate packet"); av_free_packet(&packet); break; } if (packet.stream_index == video_index && (packet.flags & AV_PKT_FLAG_KEY)) { segment_time = packet.pts * av_q2d(video_st->time_base); } else if (video_index < 0) { segment_time = packet.pts * av_q2d(audio_st->time_base); } else { segment_time = prev_segment_time; } if (segment_time - prev_segment_time >= ctx->segment_duration) { avio_flush(oc->pb); avio_close(oc->pb); if(write_index >= 0) index_file_write_segment(ctx, floor(segment_time - prev_segment_time), output_filename); sprintf(output_filename, output_format, ctx->output_prefix, output_index++); if (avio_open(&oc->pb, output_filename, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open '%s'\n", output_filename); break; } prev_segment_time = segment_time; } ret = av_write_frame(oc, &packet); if (ret < 0) { fprintf(stderr, "Warning: Could not write frame of stream\n"); break; } else if (ret > 0) { fprintf(stderr, "End of stream requested\n"); av_free_packet(&packet); break; } av_free_packet(&packet); } while (1); /* loop is exited on break */ double input_duration = (double)ic->duration / 1000000; if(write_index >= 0) index_file_write_segment(ctx, ceil(input_duration - prev_segment_time), output_filename); av_write_trailer(oc); if(video_st) avcodec_close(video_st->codec); for(i = 0; i < oc->nb_streams; i++) { av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]); } avio_close(oc->pb); av_free(oc); if(write_index >= 0) index_file_close(ctx); }