diff --git a/doc/muxers.texi b/doc/muxers.texi index 34eed0fd20..1c8f93bdb0 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -129,6 +129,27 @@ ffmpeg -i INPUT -f framemd5 - See also the @ref{md5} muxer. +@anchor{hls} +@section hls + +Apple HTTP Live Streaming muxer that segments MPEG-TS according to +the HTTP Live Streaming specification. + +It creates a playlist file and numbered segment files. The output +filename specifies the playlist filename; the segment filenames +receive the same basename as the playlist, a sequential number and +a .ts extension. + +@example +ffmpeg -i in.nut out.m3u8 +@end example + +@table @option +@item -hls_time segment length in seconds +@item -hls_list_size maximum number of playlist entries +@item -hls_wrap number after which index wraps +@end table + @anchor{ico} @section ico diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 4e662c2cd0..f6e0ee78c1 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -4579,7 +4579,11 @@ int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width, * @{ */ -void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift); +/** + * @deprecated Use av_pix_fmt_get_chroma_sub_sample + */ + +void attribute_deprecated avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift); /** * Return a value representing the fourCC code associated to the diff --git a/libavcodec/imgconvert.c b/libavcodec/imgconvert.c index de64b87743..43eeba7082 100644 --- a/libavcodec/imgconvert.c +++ b/libavcodec/imgconvert.c @@ -431,12 +431,6 @@ void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift = desc->log2_chroma_h; } -int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt) -{ - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); - return desc->flags & PIX_FMT_HWACCEL; -} - int avpicture_fill(AVPicture *picture, const uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height) { diff --git a/libavcodec/internal.h b/libavcodec/internal.h index a5e52b0c10..17ca8bb8c2 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -98,11 +98,6 @@ struct AVCodecDefault { const uint8_t *value; }; -/** - * Determine whether pix_fmt is a hardware accelerated format. - */ -int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt); - /** * Return the hardware accelerated codec for codec codec_id and * pixel format pix_fmt. diff --git a/libavcodec/libtheoraenc.c b/libavcodec/libtheoraenc.c index 364581a4b2..5a866740c8 100644 --- a/libavcodec/libtheoraenc.c +++ b/libavcodec/libtheoraenc.c @@ -33,6 +33,7 @@ /* FFmpeg includes */ #include "libavutil/common.h" #include "libavutil/intreadwrite.h" +#include "libavutil/pixdesc.h" #include "libavutil/log.h" #include "libavutil/base64.h" #include "avcodec.h" diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 1bf8c6e85e..f496e830a2 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -839,9 +839,8 @@ av_cold int ff_MPV_common_init(MpegEncContext *s) s->flags = s->avctx->flags; s->flags2 = s->avctx->flags2; - /* set chroma shifts */ - avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, - &s->chroma_y_shift); + /* set chroma shifts */ + avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift); /* convert fourcc to upper case */ s->codec_tag = avpriv_toupper4(s->avctx->codec_tag); @@ -1762,8 +1761,8 @@ void ff_print_debug_info(MpegEncContext *s, AVFrame *pict) (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1); s->low_delay = 0; // needed to see the vectors without trashing the buffers - avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, - &h_chroma_shift, &v_chroma_shift); + avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); + for (i = 0; i < 3; i++) { size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16): pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift; diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 256b8d4e4d..3cf75113b8 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -29,6 +29,7 @@ #include "libavutil/intmath.h" #include "libavutil/mathematics.h" +#include "libavutil/pixdesc.h" #include "libavutil/opt.h" #include "avcodec.h" #include "dsputil.h" @@ -687,8 +688,7 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias); - avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, - &chroma_v_shift); + avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift); if (avctx->codec_id == AV_CODEC_ID_MPEG4 && s->avctx->time_base.den > (1 << 16) - 1) { @@ -1074,8 +1074,7 @@ static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg) // empty } else { int h_chroma_shift, v_chroma_shift; - avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, - &v_chroma_shift); + avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); for (i = 0; i < 3; i++) { int src_stride = pic_arg->linesize[i]; diff --git a/libavcodec/tiffenc.c b/libavcodec/tiffenc.c index d4402859f0..61294b9207 100644 --- a/libavcodec/tiffenc.c +++ b/libavcodec/tiffenc.c @@ -293,8 +293,7 @@ static int encode_frame(AVCodecContext * avctx, AVPacket *pkt, case AV_PIX_FMT_YUV410P: case AV_PIX_FMT_YUV411P: s->photometric_interpretation = 6; - avcodec_get_chroma_sub_sample(avctx->pix_fmt, - &shift_h, &shift_v); + avcodec_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v); s->subsampling[0] = 1 << shift_h; s->subsampling[1] = 1 << shift_v; is_yuv = 1; diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 0d3cb81bf1..364ce0f0ae 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -491,7 +491,8 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic) const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt); const int pixel_size = desc->comp[0].step_minus1 + 1; - avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift); + av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, + &v_chroma_shift); avcodec_align_dimensions2(s, &w, &h, stride_align); @@ -689,9 +690,15 @@ int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, return 0; } +static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); + return desc->flags & PIX_FMT_HWACCEL; +} + enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt) { - while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt)) + while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt)) ++fmt; return fmt[0]; } diff --git a/libavcodec/x86/h264_weight_10bit.asm b/libavcodec/x86/h264_weight_10bit.asm index 20d26a49c3..3b09e420d2 100644 --- a/libavcodec/x86/h264_weight_10bit.asm +++ b/libavcodec/x86/h264_weight_10bit.asm @@ -48,7 +48,7 @@ SECTION .text movifnidn r5d, r5m %endmacro -%macro WEIGHT_SETUP 1 +%macro WEIGHT_SETUP 0 mova m0, [pw_1] movd m2, r3m pslld m0, m2 ; 1< + +#include "libavutil/mathematics.h" +#include "libavutil/parseutils.h" +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "libavutil/log.h" + +#include "avformat.h" +#include "internal.h" + +typedef struct ListEntry { + char name[1024]; + int duration; + struct ListEntry *next; +} ListEntry; + +typedef struct HLSContext { + const AVClass *class; // Class for private options. + int number; + AVOutputFormat *oformat; + AVFormatContext *avf; + float time; // Set by a private option. + int size; // Set by a private option. + int wrap; // Set by a private option. + int64_t recording_time; + int has_video; + int64_t start_pts; + int64_t end_pts; + ListEntry *list; + ListEntry *end_list; + char *basename; + AVIOContext *pb; +} HLSContext; + +static int hls_mux_init(AVFormatContext *s) +{ + HLSContext *hls = s->priv_data; + AVFormatContext *oc; + int i; + + hls->avf = oc = avformat_alloc_context(); + if (!oc) + return AVERROR(ENOMEM); + + oc->oformat = hls->oformat; + oc->interrupt_callback = s->interrupt_callback; + + for (i = 0; i < s->nb_streams; i++) { + AVStream *st; + if (!(st = avformat_new_stream(oc, NULL))) + return AVERROR(ENOMEM); + avcodec_copy_context(st->codec, s->streams[i]->codec); + st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio; + } + + return 0; +} + +static int append_entry(HLSContext *hls, uint64_t duration) +{ + ListEntry *en = av_malloc(sizeof(*en)); + + if (!en) + return AVERROR(ENOMEM); + + av_get_frame_filename(en->name, sizeof(en->name), hls->basename, + hls->number -1); + + en->duration = duration; + en->next = NULL; + + if (!hls->list) + hls->list = en; + else + hls->end_list->next = en; + + hls->end_list = en; + + if (hls->number >= hls->size) { + en = hls->list; + hls->list = en->next; + av_free(en); + } + + return 0; +} + +static void free_entries(HLSContext *hls) +{ + ListEntry *p = hls->list, *en; + + while(p) { + en = p; + p = p->next; + av_free(en); + } +} + +static int hls_window(AVFormatContext *s, int last) +{ + HLSContext *hls = s->priv_data; + ListEntry *en; + int ret = 0; + + if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE, + &s->interrupt_callback, NULL)) < 0) + goto fail; + + avio_printf(hls->pb, "#EXTM3U\n"); + avio_printf(hls->pb, "#EXT-X-VERSION:3\n"); + avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time); + avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", + FFMAX(0, hls->number - hls->size)); + + for (en = hls->list; en; en = en->next) { + avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration); + avio_printf(hls->pb, "%s\n", en->name); + } + + if (last) + avio_printf(hls->pb, "#EXT-X-ENDLIST\n"); + +fail: + avio_closep(&hls->pb); + return ret; +} + +static int hls_start(AVFormatContext *s) +{ + HLSContext *c = s->priv_data; + AVFormatContext *oc = c->avf; + int err = 0; + + if (c->wrap) + c->number %= c->wrap; + + if (av_get_frame_filename(oc->filename, sizeof(oc->filename), + c->basename, c->number++) < 0) + return AVERROR(EINVAL); + + if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE, + &s->interrupt_callback, NULL)) < 0) + return err; + + if (oc->oformat->priv_class && oc->priv_data) + av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0); + + return 0; +} + +static int hls_write_header(AVFormatContext *s) +{ + HLSContext *hls = s->priv_data; + int ret, i; + char *p; + const char *pattern = "%d.ts"; + int basename_size = strlen(s->filename) + strlen(pattern); + + hls->number = 0; + + hls->recording_time = hls->time * 1000000; + hls->start_pts = AV_NOPTS_VALUE; + + for (i = 0; i < s->nb_streams; i++) + hls->has_video += + s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO; + + if (hls->has_video > 1) + av_log(s, AV_LOG_WARNING, + "More than a single video stream present, " + "expect issues decoding it.\n"); + + hls->oformat = av_guess_format("mpegts", NULL, NULL); + + if (!hls->oformat) { + ret = AVERROR_MUXER_NOT_FOUND; + goto fail; + } + + hls->basename = av_malloc(basename_size); + + if (!hls->basename) { + ret = AVERROR(ENOMEM); + goto fail; + } + + strcpy(hls->basename, s->filename); + + p = strrchr(hls->basename, '.'); + + if (p) + *p = '\0'; + + av_strlcat(hls->basename, "%d.ts", basename_size); + + if ((ret = hls_mux_init(s)) < 0) + goto fail; + + if ((ret = hls_start(s)) < 0) + goto fail; + + if ((ret = avformat_write_header(hls->avf, NULL)) < 0) + return ret; + + +fail: + if (ret) { + av_free(hls->basename); + if (hls->avf) + avformat_free_context(hls->avf); + } + return ret; +} + +static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + HLSContext *hls = s->priv_data; + AVFormatContext *oc = hls->avf; + AVStream *st = s->streams[pkt->stream_index]; + int64_t end_pts = hls->recording_time * hls->number; + int ret; + + if (hls->start_pts == AV_NOPTS_VALUE) { + hls->start_pts = pkt->pts; + hls->end_pts = pkt->pts; + } + end_pts += hls->start_pts; + + if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) && + av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 && + pkt->flags & AV_PKT_FLAG_KEY) { + + append_entry(hls, av_rescale(pkt->pts - hls->end_pts, + st->time_base.num, + st->time_base.den)); + hls->end_pts = pkt->pts; + + av_write_frame(oc, NULL); /* Flush any buffered data */ + avio_close(oc->pb); + + ret = hls_start(s); + + if (ret) + return ret; + + oc = hls->avf; + + if ((ret = hls_window(s, 0)) < 0) + return ret; + } + + ret = ff_write_chained(oc, pkt->stream_index, pkt, s); + + return ret; +} + +static int hls_write_trailer(struct AVFormatContext *s) +{ + HLSContext *hls = s->priv_data; + AVFormatContext *oc = hls->avf; + + av_write_trailer(oc); + avio_closep(&oc->pb); + avformat_free_context(oc); + av_free(hls->basename); + hls_window(s, 1); + + free_entries(hls); + avio_close(hls->pb); + return 0; +} + +#define OFFSET(x) offsetof(HLSContext, x) +#define E AV_OPT_FLAG_ENCODING_PARAM +static const AVOption options[] = { + {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E}, + {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E}, + {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E}, + { NULL }, +}; + +static const AVClass hls_class = { + .class_name = "hls muxer", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + + +AVOutputFormat ff_hls_muxer = { + .name = "hls", + .long_name = NULL_IF_CONFIG_SMALL("hls"), + .extensions = "m3u8", + .priv_data_size = sizeof(HLSContext), + .audio_codec = AV_CODEC_ID_MP2, + .video_codec = AV_CODEC_ID_MPEG2VIDEO, + .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH, + .write_header = hls_write_header, + .write_packet = hls_write_packet, + .write_trailer = hls_write_trailer, + .priv_class = &hls_class, +}; diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c index 6ee7024d24..bbe64bee7a 100644 --- a/libavutil/pixdesc.c +++ b/libavutil/pixdesc.c @@ -1749,3 +1749,15 @@ enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc) return desc - av_pix_fmt_descriptors; } + +int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, + int *h_shift, int *v_shift) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); + if (!desc) + return AVERROR(ENOSYS); + *h_shift = desc->log2_chroma_w; + *v_shift = desc->log2_chroma_h; + + return 0; +} diff --git a/libavutil/pixdesc.h b/libavutil/pixdesc.h index ccbee9b2f1..d4e8766132 100644 --- a/libavutil/pixdesc.h +++ b/libavutil/pixdesc.h @@ -206,4 +206,18 @@ const AVPixFmtDescriptor *av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev); */ enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc); +/** + * Utility function to access log2_chroma_w log2_chroma_h from + * the pixel format AVPixFmtDescriptor. + * + * @param[in] pix_fmt the pixel format + * @param[out] h_shift store log2_chroma_h + * @param[out] v_shift store log2_chroma_w + * + * @return 0 on success, AVERROR(ENOSYS) on invalid or unknown pixel format + */ +int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, + int *h_shift, int *v_shift); + + #endif /* AVUTIL_PIXDESC_H */