X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffmpeg.c;h=ac428d5d1f0c120ed7913a66fb3052e75d4dab96;hb=aca0f78114a4f74e1b59d706c11c1bd5ff6f92d9;hp=cc1b32fc7995e0962d43270578003878cb06e68e;hpb=53a3748ed23136615e488dc463b91aa57c0e9ec6;p=ffmpeg diff --git a/ffmpeg.c b/ffmpeg.c index cc1b32fc799..ac428d5d1f0 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -249,6 +249,12 @@ typedef struct InputStream { int resample_channels; uint64_t resample_channel_layout; + struct sub2video { + int64_t last_pts; + AVFilterBufferRef *ref; + int w, h; + } sub2video; + /* a pool of free buffers for decoded data */ FrameBuffer *buffer_pool; int dr1; @@ -504,6 +510,155 @@ static void update_benchmark(const char *fmt, ...) } } +/* sub2video hack: + Convert subtitles to video with alpha to insert them in filter graphs. + This is a temporary solution until libavfilter gets real subtitles support. + */ + + +static int sub2video_prepare(InputStream *ist) +{ + AVFormatContext *avf = input_files[ist->file_index]->ctx; + int i, ret, w, h; + uint8_t *image[4]; + int linesize[4]; + + /* Compute the size of the canvas for the subtitles stream. + If the subtitles codec has set a size, use it. Otherwise use the + maximum dimensions of the video streams in the same file. */ + w = ist->st->codec->width; + h = ist->st->codec->height; + if (!(w && h)) { + for (i = 0; i < avf->nb_streams; i++) { + if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { + w = FFMAX(w, avf->streams[i]->codec->width); + h = FFMAX(h, avf->streams[i]->codec->height); + } + } + if (!(w && h)) { + w = FFMAX(w, 720); + h = FFMAX(h, 576); + } + av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h); + } + ist->sub2video.w = ist->st->codec->width = w; + ist->sub2video.h = ist->st->codec->height = h; + + /* rectangles are PIX_FMT_PAL8, but we have no guarantee that the + palettes for all rectangles are identical or compatible */ + ist->st->codec->pix_fmt = PIX_FMT_RGB32; + + ret = av_image_alloc(image, linesize, w, h, PIX_FMT_RGB32, 32); + if (ret < 0) + return ret; + memset(image[0], 0, h * linesize[0]); + ist->sub2video.ref = avfilter_get_video_buffer_ref_from_arrays( + image, linesize, AV_PERM_READ | AV_PERM_PRESERVE, + w, h, PIX_FMT_RGB32); + if (!ist->sub2video.ref) { + av_free(image[0]); + return AVERROR(ENOMEM); + } + return 0; +} + +static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h, + AVSubtitleRect *r) +{ + uint32_t *pal, *dst2; + uint8_t *src, *src2; + int x, y; + + if (r->type != SUBTITLE_BITMAP) { + av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n"); + return; + } + if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) { + av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle overflowing\n"); + return; + } + + dst += r->y * dst_linesize + r->x * 4; + src = r->pict.data[0]; + pal = (uint32_t *)r->pict.data[1]; + for (y = 0; y < r->h; y++) { + dst2 = (uint32_t *)dst; + src2 = src; + for (x = 0; x < r->w; x++) + *(dst2++) = pal[*(src2++)]; + dst += dst_linesize; + src += r->pict.linesize[0]; + } +} + +static void sub2video_push_ref(InputStream *ist, int64_t pts) +{ + AVFilterBufferRef *ref = ist->sub2video.ref; + int i; + + ist->sub2video.last_pts = ref->pts = pts; + for (i = 0; i < ist->nb_filters; i++) + av_buffersrc_add_ref(ist->filters[i]->filter, + avfilter_ref_buffer(ref, ~0), + AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT | + AV_BUFFERSRC_FLAG_NO_COPY); +} + +static void sub2video_update(InputStream *ist, AVSubtitle *sub, int64_t pts) +{ + int w = ist->sub2video.w, h = ist->sub2video.h; + AVFilterBufferRef *ref = ist->sub2video.ref; + int8_t *dst; + int dst_linesize; + int i; + + if (!ref) + return; + dst = ref->data [0]; + dst_linesize = ref->linesize[0]; + memset(dst, 0, h * dst_linesize); + for (i = 0; i < sub->num_rects; i++) + sub2video_copy_rect(dst, dst_linesize, w, h, sub->rects[i]); + sub2video_push_ref(ist, pts); +} + +static void sub2video_heartbeat(InputStream *ist, int64_t pts) +{ + InputFile *infile = input_files[ist->file_index]; + int i, j, nb_reqs; + int64_t pts2; + + /* When a frame is read from a file, examine all sub2video streams in + the same file and send the sub2video frame again. Otherwise, decoded + video frames could be accumulating in the filter graph while a filter + (possibly overlay) is desperately waiting for a subtitle frame. */ + for (i = 0; i < infile->nb_streams; i++) { + InputStream *ist2 = input_streams[infile->ist_index + i]; + if (!ist2->sub2video.ref) + continue; + /* subtitles seem to be usually muxed ahead of other streams; + if not, substracting a larger time here is necessary */ + pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1; + /* do not send the heartbeat frame if the subtitle is already ahead */ + if (pts2 <= ist2->sub2video.last_pts) + continue; + for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++) + nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter); + if (nb_reqs) + sub2video_push_ref(ist2, pts2); + } +} + +static void sub2video_flush(InputStream *ist) +{ + int i; + + for (i = 0; i < ist->nb_filters; i++) + av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0); +} + +/* end of sub2video hack */ + static void reset_options(OptionsContext *o, int is_input) { const OptionDef *po = options; @@ -560,9 +715,9 @@ static enum PixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum Pixe int has_alpha= av_pix_fmt_descriptors[target].nb_components % 2 == 0; enum PixelFormat best= PIX_FMT_NONE; if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) { - if (st->codec->codec_id == CODEC_ID_MJPEG) { + if (st->codec->codec_id == AV_CODEC_ID_MJPEG) { p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE }; - } else if (st->codec->codec_id == CODEC_ID_LJPEG) { + } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) { p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE }; } @@ -608,9 +763,9 @@ static char *choose_pix_fmts(OutputStream *ost) p = ost->enc->pix_fmts; if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) { - if (ost->st->codec->codec_id == CODEC_ID_MJPEG) { + if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) { p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE }; - } else if (ost->st->codec->codec_id == CODEC_ID_LJPEG) { + } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) { p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE }; } @@ -745,7 +900,10 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in) s = input_files[file_idx]->ctx; for (i = 0; i < s->nb_streams; i++) { - if (s->streams[i]->codec->codec_type != type) + enum AVMediaType stream_type = s->streams[i]->codec->codec_type; + if (stream_type != type && + !(stream_type == AVMEDIA_TYPE_SUBTITLE && + type == AVMEDIA_TYPE_VIDEO /* sub2video hack */)) continue; if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) { st = s->streams[i]; @@ -1014,8 +1172,7 @@ static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterContext *first_filter = in->filter_ctx; AVFilter *filter = avfilter_get_by_name("buffer"); InputStream *ist = ifilter->ist; - AVRational tb = ist->framerate.num ? (AVRational){ist->framerate.den, - ist->framerate.num} : + AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) : ist->st->time_base; AVRational fr = ist->framerate.num ? ist->framerate : ist->st->r_frame_rate; @@ -1025,6 +1182,12 @@ static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, int pad_idx = in->pad_idx; int ret; + if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) { + ret = sub2video_prepare(ist); + if (ret < 0) + return ret; + } + sar = ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio : ist->st->codec->sample_aspect_ratio; @@ -1413,6 +1576,7 @@ void av_noreturn exit_program(int ret) av_freep(&input_streams[i]->decoded_frame); av_dict_free(&input_streams[i]->opts); free_buffer_pool(&input_streams[i]->buffer_pool); + avfilter_unref_bufferp(&input_streams[i]->sub2video.ref); av_freep(&input_streams[i]->filters); av_freep(&input_streams[i]); } @@ -1678,7 +1842,7 @@ static void do_subtitle_out(AVFormatContext *s, /* Note: DVB subtitle need one packet to draw them and one other packet to clear them */ /* XXX: signal it in the codec context ? */ - if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) + if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) nb = 2; else nb = 1; @@ -1708,7 +1872,7 @@ static void do_subtitle_out(AVFormatContext *s, pkt.size = subtitle_out_size; pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base); - if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) { + if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) { /* XXX: the pts correction is handled here. Maybe handling it in the codec would be better */ if (i == 0) @@ -1800,7 +1964,7 @@ static void do_video_out(AVFormatContext *s, return; if (s->oformat->flags & AVFMT_RAWPICTURE && - enc->codec->id == CODEC_ID_RAWVIDEO) { + enc->codec->id == AV_CODEC_ID_RAWVIDEO) { /* raw pictures are written as AVPicture structure to avoid any copies. We support temporarily the older method. */ @@ -2216,7 +2380,7 @@ static void flush_encoders(void) if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1) continue; - if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == CODEC_ID_RAWVIDEO) + if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO) continue; for (;;) { @@ -2335,10 +2499,10 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p opkt.flags = pkt->flags; // FIXME remove the following 2 lines they shall be replaced by the bitstream filters - if ( ost->st->codec->codec_id != CODEC_ID_H264 - && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO - && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO - && ost->st->codec->codec_id != CODEC_ID_VC1 + if ( ost->st->codec->codec_id != AV_CODEC_ID_H264 + && ost->st->codec->codec_id != AV_CODEC_ID_MPEG1VIDEO + && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO + && ost->st->codec->codec_id != AV_CODEC_ID_VC1 ) { if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY)) opkt.destruct = av_destruct_packet; @@ -2636,13 +2800,16 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output) AVSubtitle subtitle; int i, ret = avcodec_decode_subtitle2(ist->st->codec, &subtitle, got_output, pkt); - if (ret < 0) - return ret; - if (!*got_output) + if (ret < 0 || !*got_output) { + if (!pkt->size) + sub2video_flush(ist); return ret; + } rate_emu_sleep(ist); + sub2video_update(ist, &subtitle, pkt->pts); + for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; @@ -3021,7 +3188,7 @@ static int transcode_init(void) } if(ost->frame_rate.num) - codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num}; + codec->time_base = av_inv_q(ost->frame_rate); av_reduce(&codec->time_base.num, &codec->time_base.den, codec->time_base.num, codec->time_base.den, INT_MAX); @@ -3038,9 +3205,9 @@ static int transcode_init(void) codec->frame_size = icodec->frame_size; codec->audio_service_type = icodec->audio_service_type; codec->block_align = icodec->block_align; - if((codec->block_align == 1 || codec->block_align == 1152) && codec->codec_id == CODEC_ID_MP3) + if((codec->block_align == 1 || codec->block_align == 1152) && codec->codec_id == AV_CODEC_ID_MP3) codec->block_align= 0; - if(codec->codec_id == CODEC_ID_AC3) + if(codec->codec_id == AV_CODEC_ID_AC3) codec->block_align= 0; break; case AVMEDIA_TYPE_VIDEO: @@ -3114,7 +3281,7 @@ static int transcode_init(void) codec->time_base = (AVRational){ 1, codec->sample_rate }; break; case AVMEDIA_TYPE_VIDEO: - codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num}; + codec->time_base = av_inv_q(ost->frame_rate); if (ost->filter && !(codec->time_base.num && codec->time_base.den)) codec->time_base = ost->filter->filter->inputs[0]->time_base; if ( av_q2d(codec->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH @@ -3198,7 +3365,8 @@ static int transcode_init(void) if ((ist = get_input_stream(ost))) dec = ist->st->codec; if (dec && dec->subtitle_header) { - ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size); + /* ASS code assumes this buffer is null terminated so add extra byte. */ + ost->st->codec->subtitle_header = av_mallocz(dec->subtitle_header_size + 1); if (!ost->st->codec->subtitle_header) { ret = AVERROR(ENOMEM); goto dump_format; @@ -3847,6 +4015,8 @@ static int transcode(void) } } + sub2video_heartbeat(ist, pkt.pts); + // fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size); if ((ret = output_packet(ist, &pkt)) < 0 || ((ret = poll_filters()) < 0 && ret != AVERROR_EOF)) { @@ -4514,11 +4684,11 @@ static int opt_input_file(OptionsContext *o, const char *opt, const char *filena av_dict_set(&format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0); ic->video_codec_id = video_codec_name ? - find_codec_or_die(video_codec_name , AVMEDIA_TYPE_VIDEO , 0)->id : CODEC_ID_NONE; + find_codec_or_die(video_codec_name , AVMEDIA_TYPE_VIDEO , 0)->id : AV_CODEC_ID_NONE; ic->audio_codec_id = audio_codec_name ? - find_codec_or_die(audio_codec_name , AVMEDIA_TYPE_AUDIO , 0)->id : CODEC_ID_NONE; + find_codec_or_die(audio_codec_name , AVMEDIA_TYPE_AUDIO , 0)->id : AV_CODEC_ID_NONE; ic->subtitle_codec_id= subtitle_codec_name ? - find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0)->id : CODEC_ID_NONE; + find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0)->id : AV_CODEC_ID_NONE; ic->flags |= AVFMT_FLAG_NONBLOCK; ic->interrupt_callback = int_cb; @@ -5228,7 +5398,7 @@ static void opt_output_file(void *optctx, const char *filename) /* pick the "best" stream of each type */ /* video: highest resolution */ - if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) { + if (!o->video_disable && oc->oformat->video_codec != AV_CODEC_ID_NONE) { int area = 0, idx = -1; for (i = 0; i < nb_input_streams; i++) { ist = input_streams[i]; @@ -5243,7 +5413,7 @@ static void opt_output_file(void *optctx, const char *filename) } /* audio: most channels */ - if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) { + if (!o->audio_disable && oc->oformat->audio_codec != AV_CODEC_ID_NONE) { int channels = 0, idx = -1; for (i = 0; i < nb_input_streams; i++) { ist = input_streams[i]; @@ -5258,7 +5428,7 @@ static void opt_output_file(void *optctx, const char *filename) } /* subtitles: pick first */ - if (!o->subtitle_disable && (oc->oformat->subtitle_codec != CODEC_ID_NONE || subtitle_codec_name)) { + if (!o->subtitle_disable && (oc->oformat->subtitle_codec != AV_CODEC_ID_NONE || subtitle_codec_name)) { for (i = 0; i < nb_input_streams; i++) if (input_streams[i]->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) { new_subtitle_stream(o, oc, i);