Browse code

Merge commit '5e9c6ef8f3beb9ed7b271654a82349ac90fe43f2'

* commit '5e9c6ef8f3beb9ed7b271654a82349ac90fe43f2':
x86: h264_weight_10bit: port to cpuflags
libtheoraenc: add missing pixdesc.h header
avcodec: remove ff_is_hwaccel_pix_fmt
pixdesc: add av_pix_fmt_get_chroma_sub_sample
hlsenc: stand alone hls segmenter

Conflicts:
doc/muxers.texi
libavcodec/ffv1enc.c
libavcodec/imgconvert.c
libavcodec/mpegvideo_enc.c
libavcodec/tiffenc.c
libavformat/Makefile
libavformat/allformats.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2012/11/14 19:22:40
Showing 15 changed files
... ...
@@ -129,6 +129,27 @@ ffmpeg -i INPUT -f framemd5 -
129 129
 
130 130
 See also the @ref{md5} muxer.
131 131
 
132
+@anchor{hls}
133
+@section hls
134
+
135
+Apple HTTP Live Streaming muxer that segments MPEG-TS according to
136
+the HTTP Live Streaming specification.
137
+
138
+It creates a playlist file and numbered segment files. The output
139
+filename specifies the playlist filename; the segment filenames
140
+receive the same basename as the playlist, a sequential number and
141
+a .ts extension.
142
+
143
+@example
144
+ffmpeg -i in.nut out.m3u8
145
+@end example
146
+
147
+@table @option
148
+@item -hls_time segment length in seconds
149
+@item -hls_list_size maximum number of playlist entries
150
+@item -hls_wrap number after which index wraps
151
+@end table
152
+
132 153
 @anchor{ico}
133 154
 @section ico
134 155
 
... ...
@@ -4579,7 +4579,11 @@ int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
4579 4579
  * @{
4580 4580
  */
4581 4581
 
4582
-void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift);
4582
+/**
4583
+ * @deprecated Use av_pix_fmt_get_chroma_sub_sample
4584
+ */
4585
+
4586
+void attribute_deprecated avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift);
4583 4587
 
4584 4588
 /**
4585 4589
  * Return a value representing the fourCC code associated to the
... ...
@@ -431,12 +431,6 @@ void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int
431 431
     *v_shift = desc->log2_chroma_h;
432 432
 }
433 433
 
434
-int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
435
-{
436
-    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
437
-    return desc->flags & PIX_FMT_HWACCEL;
438
-}
439
-
440 434
 int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
441 435
                    enum AVPixelFormat pix_fmt, int width, int height)
442 436
 {
... ...
@@ -99,11 +99,6 @@ struct AVCodecDefault {
99 99
 };
100 100
 
101 101
 /**
102
- * Determine whether pix_fmt is a hardware accelerated format.
103
- */
104
-int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt);
105
-
106
-/**
107 102
  * Return the hardware accelerated codec for codec codec_id and
108 103
  * pixel format pix_fmt.
109 104
  *
... ...
@@ -33,6 +33,7 @@
33 33
 /* FFmpeg includes */
34 34
 #include "libavutil/common.h"
35 35
 #include "libavutil/intreadwrite.h"
36
+#include "libavutil/pixdesc.h"
36 37
 #include "libavutil/log.h"
37 38
 #include "libavutil/base64.h"
38 39
 #include "avcodec.h"
... ...
@@ -839,9 +839,8 @@ av_cold int ff_MPV_common_init(MpegEncContext *s)
839 839
     s->flags  = s->avctx->flags;
840 840
     s->flags2 = s->avctx->flags2;
841 841
 
842
-        /* set chroma shifts */
843
-        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
844
-                                      &s->chroma_y_shift);
842
+    /* set chroma shifts */
843
+    avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
845 844
 
846 845
     /* convert fourcc to upper case */
847 846
     s->codec_tag        = avpriv_toupper4(s->avctx->codec_tag);
... ...
@@ -1762,8 +1761,8 @@ void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
1762 1762
                                    (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1);
1763 1763
         s->low_delay = 0; // needed to see the vectors without trashing the buffers
1764 1764
 
1765
-        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
1766
-                                      &h_chroma_shift, &v_chroma_shift);
1765
+        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1766
+
1767 1767
         for (i = 0; i < 3; i++) {
1768 1768
             size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16):
1769 1769
                          pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift;
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include "libavutil/intmath.h"
31 31
 #include "libavutil/mathematics.h"
32
+#include "libavutil/pixdesc.h"
32 33
 #include "libavutil/opt.h"
33 34
 #include "avcodec.h"
34 35
 #include "dsputil.h"
... ...
@@ -687,8 +688,7 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
687 687
 
688 688
     av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
689 689
 
690
-    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
691
-                                  &chroma_v_shift);
690
+    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
692 691
 
693 692
     if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
694 693
         s->avctx->time_base.den > (1 << 16) - 1) {
... ...
@@ -1074,8 +1074,7 @@ static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg)
1074 1074
             // empty
1075 1075
         } else {
1076 1076
             int h_chroma_shift, v_chroma_shift;
1077
-            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift,
1078
-                                          &v_chroma_shift);
1077
+            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1079 1078
 
1080 1079
             for (i = 0; i < 3; i++) {
1081 1080
                 int src_stride = pic_arg->linesize[i];
... ...
@@ -293,8 +293,7 @@ static int encode_frame(AVCodecContext * avctx, AVPacket *pkt,
293 293
     case AV_PIX_FMT_YUV410P:
294 294
     case AV_PIX_FMT_YUV411P:
295 295
         s->photometric_interpretation = 6;
296
-        avcodec_get_chroma_sub_sample(avctx->pix_fmt,
297
-                &shift_h, &shift_v);
296
+        avcodec_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
298 297
         s->subsampling[0] = 1 << shift_h;
299 298
         s->subsampling[1] = 1 << shift_v;
300 299
         is_yuv = 1;
... ...
@@ -491,7 +491,8 @@ static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
491 491
         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
492 492
         const int pixel_size = desc->comp[0].step_minus1 + 1;
493 493
 
494
-        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
494
+        av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
495
+                                         &v_chroma_shift);
495 496
 
496 497
         avcodec_align_dimensions2(s, &w, &h, stride_align);
497 498
 
... ...
@@ -689,9 +690,15 @@ int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2,
689 689
     return 0;
690 690
 }
691 691
 
692
+static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
693
+{
694
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
695
+    return desc->flags & PIX_FMT_HWACCEL;
696
+}
697
+
692 698
 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
693 699
 {
694
-    while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
700
+    while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
695 701
         ++fmt;
696 702
     return fmt[0];
697 703
 }
... ...
@@ -48,7 +48,7 @@ SECTION .text
48 48
     movifnidn r5d, r5m
49 49
 %endmacro
50 50
 
51
-%macro WEIGHT_SETUP 1
51
+%macro WEIGHT_SETUP 0
52 52
     mova       m0, [pw_1]
53 53
     movd       m2, r3m
54 54
     pslld      m0, m2       ; 1<<log2_denom
... ...
@@ -59,19 +59,19 @@ SECTION .text
59 59
     pshufd     m3, m3, 0
60 60
     mova       m4, [pw_pixel_max]
61 61
     paddw      m2, [sq_1]   ; log2_denom+1
62
-%ifnidn %1, sse4
62
+%if notcpuflag(sse4)
63 63
     pxor       m7, m7
64 64
 %endif
65 65
 %endmacro
66 66
 
67
-%macro WEIGHT_OP 2-3
68
-%if %0==2
69
-    mova        m5, [r0+%2]
67
+%macro WEIGHT_OP 1-2
68
+%if %0==1
69
+    mova        m5, [r0+%1]
70 70
     punpckhwd   m6, m5, m0
71 71
     punpcklwd   m5, m0
72 72
 %else
73
-    movq        m5, [r0+%2]
74
-    movq        m6, [r0+%3]
73
+    movq        m5, [r0+%1]
74
+    movq        m6, [r0+%2]
75 75
     punpcklwd   m5, m0
76 76
     punpcklwd   m6, m0
77 77
 %endif
... ...
@@ -79,7 +79,7 @@ SECTION .text
79 79
     pmaddwd     m6, m3
80 80
     psrad       m5, m2
81 81
     psrad       m6, m2
82
-%ifidn %1, sse4
82
+%if cpuflag(sse4)
83 83
     packusdw    m5, m6
84 84
     pminsw      m5, m4
85 85
 %else
... ...
@@ -88,14 +88,14 @@ SECTION .text
88 88
 %endif
89 89
 %endmacro
90 90
 
91
-%macro WEIGHT_FUNC_DBL 1
92
-cglobal h264_weight_16_10_%1
91
+%macro WEIGHT_FUNC_DBL 0
92
+cglobal h264_weight_16_10
93 93
     WEIGHT_PROLOGUE
94
-    WEIGHT_SETUP %1
94
+    WEIGHT_SETUP
95 95
 .nextrow:
96
-    WEIGHT_OP %1,  0
96
+    WEIGHT_OP  0
97 97
     mova [r0   ], m5
98
-    WEIGHT_OP %1, 16
98
+    WEIGHT_OP 16
99 99
     mova [r0+16], m5
100 100
     add       r0, r1
101 101
     dec       r2d
... ...
@@ -103,17 +103,18 @@ cglobal h264_weight_16_10_%1
103 103
     REP_RET
104 104
 %endmacro
105 105
 
106
-INIT_XMM
107
-WEIGHT_FUNC_DBL sse2
108
-WEIGHT_FUNC_DBL sse4
106
+INIT_XMM sse2
107
+WEIGHT_FUNC_DBL
108
+INIT_XMM sse4
109
+WEIGHT_FUNC_DBL
109 110
 
110 111
 
111
-%macro WEIGHT_FUNC_MM 1
112
-cglobal h264_weight_8_10_%1
112
+%macro WEIGHT_FUNC_MM 0
113
+cglobal h264_weight_8_10
113 114
     WEIGHT_PROLOGUE
114
-    WEIGHT_SETUP %1
115
+    WEIGHT_SETUP
115 116
 .nextrow:
116
-    WEIGHT_OP  %1, 0
117
+    WEIGHT_OP   0
117 118
     mova     [r0], m5
118 119
     add        r0, r1
119 120
     dec        r2d
... ...
@@ -121,19 +122,20 @@ cglobal h264_weight_8_10_%1
121 121
     REP_RET
122 122
 %endmacro
123 123
 
124
-INIT_XMM
125
-WEIGHT_FUNC_MM sse2
126
-WEIGHT_FUNC_MM sse4
124
+INIT_XMM sse2
125
+WEIGHT_FUNC_MM
126
+INIT_XMM sse4
127
+WEIGHT_FUNC_MM
127 128
 
128 129
 
129
-%macro WEIGHT_FUNC_HALF_MM 1
130
-cglobal h264_weight_4_10_%1
130
+%macro WEIGHT_FUNC_HALF_MM 0
131
+cglobal h264_weight_4_10
131 132
     WEIGHT_PROLOGUE
132 133
     sar         r2d, 1
133
-    WEIGHT_SETUP %1
134
+    WEIGHT_SETUP
134 135
     lea         r3, [r1*2]
135 136
 .nextrow:
136
-    WEIGHT_OP   %1, 0, r1
137
+    WEIGHT_OP    0, r1
137 138
     movh      [r0], m5
138 139
     movhps [r0+r1], m5
139 140
     add         r0, r3
... ...
@@ -142,9 +144,10 @@ cglobal h264_weight_4_10_%1
142 142
     REP_RET
143 143
 %endmacro
144 144
 
145
-INIT_XMM
146
-WEIGHT_FUNC_HALF_MM sse2
147
-WEIGHT_FUNC_HALF_MM sse4
145
+INIT_XMM sse2
146
+WEIGHT_FUNC_HALF_MM
147
+INIT_XMM sse4
148
+WEIGHT_FUNC_HALF_MM
148 149
 
149 150
 
150 151
 ;-----------------------------------------------------------------------------
... ...
@@ -168,7 +171,7 @@ DECLARE_REG_TMP 7
168 168
     movifnidn t0d, r7m
169 169
 %endmacro
170 170
 
171
-%macro BIWEIGHT_SETUP 1
171
+%macro BIWEIGHT_SETUP 0
172 172
     lea        t0, [t0*4+1] ; (offset<<2)+1
173 173
     or         t0, 1
174 174
     shl        r6, 16
... ...
@@ -182,23 +185,23 @@ DECLARE_REG_TMP 7
182 182
     pshufd     m5, m5, 0
183 183
     mova       m3, [pw_pixel_max]
184 184
     movifnidn r3d, r3m
185
-%ifnidn %1, sse4
185
+%if notcpuflag(sse4)
186 186
     pxor       m7, m7
187 187
 %endif
188 188
 %endmacro
189 189
 
190
-%macro BIWEIGHT 2-3
191
-%if %0==2
192
-    mova       m0, [r0+%2]
193
-    mova       m1, [r1+%2]
190
+%macro BIWEIGHT 1-2
191
+%if %0==1
192
+    mova       m0, [r0+%1]
193
+    mova       m1, [r1+%1]
194 194
     punpckhwd  m2, m0, m1
195 195
     punpcklwd  m0, m1
196 196
 %else
197
-    movq       m0, [r0+%2]
198
-    movq       m1, [r1+%2]
197
+    movq       m0, [r0+%1]
198
+    movq       m1, [r1+%1]
199 199
     punpcklwd  m0, m1
200
-    movq       m2, [r0+%3]
201
-    movq       m1, [r1+%3]
200
+    movq       m2, [r0+%2]
201
+    movq       m1, [r1+%2]
202 202
     punpcklwd  m2, m1
203 203
 %endif
204 204
     pmaddwd    m0, m4
... ...
@@ -207,7 +210,7 @@ DECLARE_REG_TMP 7
207 207
     paddd      m2, m5
208 208
     psrad      m0, m6
209 209
     psrad      m2, m6
210
-%ifidn %1, sse4
210
+%if cpuflag(sse4)
211 211
     packusdw   m0, m2
212 212
     pminsw     m0, m3
213 213
 %else
... ...
@@ -216,14 +219,14 @@ DECLARE_REG_TMP 7
216 216
 %endif
217 217
 %endmacro
218 218
 
219
-%macro BIWEIGHT_FUNC_DBL 1
220
-cglobal h264_biweight_16_10_%1
219
+%macro BIWEIGHT_FUNC_DBL 0
220
+cglobal h264_biweight_16_10
221 221
     BIWEIGHT_PROLOGUE
222
-    BIWEIGHT_SETUP %1
222
+    BIWEIGHT_SETUP
223 223
 .nextrow:
224
-    BIWEIGHT  %1,  0
224
+    BIWEIGHT   0
225 225
     mova [r0   ], m0
226
-    BIWEIGHT  %1, 16
226
+    BIWEIGHT  16
227 227
     mova [r0+16], m0
228 228
     add       r0, r2
229 229
     add       r1, r2
... ...
@@ -232,16 +235,17 @@ cglobal h264_biweight_16_10_%1
232 232
     REP_RET
233 233
 %endmacro
234 234
 
235
-INIT_XMM
236
-BIWEIGHT_FUNC_DBL sse2
237
-BIWEIGHT_FUNC_DBL sse4
235
+INIT_XMM sse2
236
+BIWEIGHT_FUNC_DBL
237
+INIT_XMM sse4
238
+BIWEIGHT_FUNC_DBL
238 239
 
239
-%macro BIWEIGHT_FUNC 1
240
-cglobal h264_biweight_8_10_%1
240
+%macro BIWEIGHT_FUNC 0
241
+cglobal h264_biweight_8_10
241 242
     BIWEIGHT_PROLOGUE
242
-    BIWEIGHT_SETUP %1
243
+    BIWEIGHT_SETUP
243 244
 .nextrow:
244
-    BIWEIGHT %1, 0
245
+    BIWEIGHT  0
245 246
     mova   [r0], m0
246 247
     add      r0, r2
247 248
     add      r1, r2
... ...
@@ -250,18 +254,19 @@ cglobal h264_biweight_8_10_%1
250 250
     REP_RET
251 251
 %endmacro
252 252
 
253
-INIT_XMM
254
-BIWEIGHT_FUNC sse2
255
-BIWEIGHT_FUNC sse4
253
+INIT_XMM sse2
254
+BIWEIGHT_FUNC
255
+INIT_XMM sse4
256
+BIWEIGHT_FUNC
256 257
 
257
-%macro BIWEIGHT_FUNC_HALF 1
258
-cglobal h264_biweight_4_10_%1
258
+%macro BIWEIGHT_FUNC_HALF 0
259
+cglobal h264_biweight_4_10
259 260
     BIWEIGHT_PROLOGUE
260
-    BIWEIGHT_SETUP %1
261
+    BIWEIGHT_SETUP
261 262
     sar        r3d, 1
262 263
     lea        r4, [r2*2]
263 264
 .nextrow:
264
-    BIWEIGHT    %1, 0, r2
265
+    BIWEIGHT     0, r2
265 266
     movh   [r0   ], m0
266 267
     movhps [r0+r2], m0
267 268
     add         r0, r4
... ...
@@ -271,6 +276,7 @@ cglobal h264_biweight_4_10_%1
271 271
     REP_RET
272 272
 %endmacro
273 273
 
274
-INIT_XMM
275
-BIWEIGHT_FUNC_HALF sse2
276
-BIWEIGHT_FUNC_HALF sse4
274
+INIT_XMM sse2
275
+BIWEIGHT_FUNC_HALF
276
+INIT_XMM sse4
277
+BIWEIGHT_FUNC_HALF
... ...
@@ -125,6 +125,7 @@ OBJS-$(CONFIG_H263_MUXER)                += rawenc.o
125 125
 OBJS-$(CONFIG_H264_DEMUXER)              += h264dec.o rawdec.o
126 126
 OBJS-$(CONFIG_H264_MUXER)                += rawenc.o
127 127
 OBJS-$(CONFIG_HLS_DEMUXER)               += hls.o
128
+OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o mpegtsenc.o
128 129
 OBJS-$(CONFIG_ICO_DEMUXER)               += icodec.o
129 130
 OBJS-$(CONFIG_ICO_MUXER)                 += icoenc.o
130 131
 OBJS-$(CONFIG_IDCIN_DEMUXER)             += idcin.o
... ...
@@ -114,7 +114,7 @@ void av_register_all(void)
114 114
     REGISTER_MUXDEMUX (H261, h261);
115 115
     REGISTER_MUXDEMUX (H263, h263);
116 116
     REGISTER_MUXDEMUX (H264, h264);
117
-    REGISTER_DEMUXER  (HLS, hls);
117
+    REGISTER_MUXDEMUX (HLS, hls);
118 118
     REGISTER_MUXDEMUX (ICO, ico);
119 119
     REGISTER_DEMUXER  (IDCIN, idcin);
120 120
     REGISTER_DEMUXER  (IDF, idf);
121 121
new file mode 100644
... ...
@@ -0,0 +1,324 @@
0
+/*
1
+ * Apple HTTP Live Streaming segmenter
2
+ * Copyright (c) 2012, Luca Barbato
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <float.h>
22
+
23
+#include "libavutil/mathematics.h"
24
+#include "libavutil/parseutils.h"
25
+#include "libavutil/avstring.h"
26
+#include "libavutil/opt.h"
27
+#include "libavutil/log.h"
28
+
29
+#include "avformat.h"
30
+#include "internal.h"
31
+
32
+typedef struct ListEntry {
33
+    char  name[1024];
34
+    int   duration;
35
+    struct ListEntry *next;
36
+} ListEntry;
37
+
38
+typedef struct HLSContext {
39
+    const AVClass *class;  // Class for private options.
40
+    int number;
41
+    AVOutputFormat *oformat;
42
+    AVFormatContext *avf;
43
+    float time;            // Set by a private option.
44
+    int  size;             // Set by a private option.
45
+    int  wrap;             // Set by a private option.
46
+    int64_t recording_time;
47
+    int has_video;
48
+    int64_t start_pts;
49
+    int64_t end_pts;
50
+    ListEntry *list;
51
+    ListEntry *end_list;
52
+    char *basename;
53
+    AVIOContext *pb;
54
+} HLSContext;
55
+
56
+static int hls_mux_init(AVFormatContext *s)
57
+{
58
+    HLSContext *hls = s->priv_data;
59
+    AVFormatContext *oc;
60
+    int i;
61
+
62
+    hls->avf = oc = avformat_alloc_context();
63
+    if (!oc)
64
+        return AVERROR(ENOMEM);
65
+
66
+    oc->oformat            = hls->oformat;
67
+    oc->interrupt_callback = s->interrupt_callback;
68
+
69
+    for (i = 0; i < s->nb_streams; i++) {
70
+        AVStream *st;
71
+        if (!(st = avformat_new_stream(oc, NULL)))
72
+            return AVERROR(ENOMEM);
73
+        avcodec_copy_context(st->codec, s->streams[i]->codec);
74
+        st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
75
+    }
76
+
77
+    return 0;
78
+}
79
+
80
+static int append_entry(HLSContext *hls, uint64_t duration)
81
+{
82
+    ListEntry *en = av_malloc(sizeof(*en));
83
+
84
+    if (!en)
85
+        return AVERROR(ENOMEM);
86
+
87
+    av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
88
+                          hls->number -1);
89
+
90
+    en->duration = duration;
91
+    en->next     = NULL;
92
+
93
+    if (!hls->list)
94
+        hls->list = en;
95
+    else
96
+        hls->end_list->next = en;
97
+
98
+    hls->end_list = en;
99
+
100
+    if (hls->number >= hls->size) {
101
+        en = hls->list;
102
+        hls->list = en->next;
103
+        av_free(en);
104
+    }
105
+
106
+    return 0;
107
+}
108
+
109
+static void free_entries(HLSContext *hls)
110
+{
111
+    ListEntry *p = hls->list, *en;
112
+
113
+    while(p) {
114
+        en = p;
115
+        p = p->next;
116
+        av_free(en);
117
+    }
118
+}
119
+
120
+static int hls_window(AVFormatContext *s, int last)
121
+{
122
+    HLSContext *hls = s->priv_data;
123
+    ListEntry *en;
124
+    int ret = 0;
125
+
126
+    if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
127
+                          &s->interrupt_callback, NULL)) < 0)
128
+        goto fail;
129
+
130
+    avio_printf(hls->pb, "#EXTM3U\n");
131
+    avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
132
+    avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
133
+    avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
134
+                FFMAX(0, hls->number - hls->size));
135
+
136
+    for (en = hls->list; en; en = en->next) {
137
+        avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
138
+        avio_printf(hls->pb, "%s\n", en->name);
139
+    }
140
+
141
+    if (last)
142
+        avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
143
+
144
+fail:
145
+    avio_closep(&hls->pb);
146
+    return ret;
147
+}
148
+
149
+static int hls_start(AVFormatContext *s)
150
+{
151
+    HLSContext *c = s->priv_data;
152
+    AVFormatContext *oc = c->avf;
153
+    int err = 0;
154
+
155
+    if (c->wrap)
156
+        c->number %= c->wrap;
157
+
158
+    if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
159
+                              c->basename, c->number++) < 0)
160
+        return AVERROR(EINVAL);
161
+
162
+    if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
163
+                          &s->interrupt_callback, NULL)) < 0)
164
+        return err;
165
+
166
+    if (oc->oformat->priv_class && oc->priv_data)
167
+        av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
168
+
169
+    return 0;
170
+}
171
+
172
+static int hls_write_header(AVFormatContext *s)
173
+{
174
+    HLSContext *hls = s->priv_data;
175
+    int ret, i;
176
+    char *p;
177
+    const char *pattern = "%d.ts";
178
+    int basename_size = strlen(s->filename) + strlen(pattern);
179
+
180
+    hls->number      = 0;
181
+
182
+    hls->recording_time = hls->time * 1000000;
183
+    hls->start_pts      = AV_NOPTS_VALUE;
184
+
185
+    for (i = 0; i < s->nb_streams; i++)
186
+        hls->has_video +=
187
+            s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
188
+
189
+    if (hls->has_video > 1)
190
+        av_log(s, AV_LOG_WARNING,
191
+               "More than a single video stream present, "
192
+               "expect issues decoding it.\n");
193
+
194
+    hls->oformat = av_guess_format("mpegts", NULL, NULL);
195
+
196
+    if (!hls->oformat) {
197
+        ret = AVERROR_MUXER_NOT_FOUND;
198
+        goto fail;
199
+    }
200
+
201
+    hls->basename = av_malloc(basename_size);
202
+
203
+    if (!hls->basename) {
204
+        ret = AVERROR(ENOMEM);
205
+        goto fail;
206
+    }
207
+
208
+    strcpy(hls->basename, s->filename);
209
+
210
+    p = strrchr(hls->basename, '.');
211
+
212
+    if (p)
213
+        *p = '\0';
214
+
215
+    av_strlcat(hls->basename, "%d.ts", basename_size);
216
+
217
+    if ((ret = hls_mux_init(s)) < 0)
218
+        goto fail;
219
+
220
+    if ((ret = hls_start(s)) < 0)
221
+        goto fail;
222
+
223
+    if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
224
+        return ret;
225
+
226
+
227
+fail:
228
+    if (ret) {
229
+        av_free(hls->basename);
230
+        if (hls->avf)
231
+            avformat_free_context(hls->avf);
232
+    }
233
+    return ret;
234
+}
235
+
236
+static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
237
+{
238
+    HLSContext *hls = s->priv_data;
239
+    AVFormatContext *oc = hls->avf;
240
+    AVStream *st = s->streams[pkt->stream_index];
241
+    int64_t end_pts = hls->recording_time * hls->number;
242
+    int ret;
243
+
244
+    if (hls->start_pts == AV_NOPTS_VALUE) {
245
+        hls->start_pts = pkt->pts;
246
+        hls->end_pts   = pkt->pts;
247
+    }
248
+    end_pts += hls->start_pts;
249
+
250
+    if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)      &&
251
+        av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
252
+        pkt->flags & AV_PKT_FLAG_KEY) {
253
+
254
+        append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
255
+                                     st->time_base.num,
256
+                                     st->time_base.den));
257
+        hls->end_pts = pkt->pts;
258
+
259
+        av_write_frame(oc, NULL); /* Flush any buffered data */
260
+        avio_close(oc->pb);
261
+
262
+        ret = hls_start(s);
263
+
264
+        if (ret)
265
+            return ret;
266
+
267
+        oc = hls->avf;
268
+
269
+        if ((ret = hls_window(s, 0)) < 0)
270
+            return ret;
271
+    }
272
+
273
+    ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
274
+
275
+    return ret;
276
+}
277
+
278
+static int hls_write_trailer(struct AVFormatContext *s)
279
+{
280
+    HLSContext *hls = s->priv_data;
281
+    AVFormatContext *oc = hls->avf;
282
+
283
+    av_write_trailer(oc);
284
+    avio_closep(&oc->pb);
285
+    avformat_free_context(oc);
286
+    av_free(hls->basename);
287
+    hls_window(s, 1);
288
+
289
+    free_entries(hls);
290
+    avio_close(hls->pb);
291
+    return 0;
292
+}
293
+
294
+#define OFFSET(x) offsetof(HLSContext, x)
295
+#define E AV_OPT_FLAG_ENCODING_PARAM
296
+static const AVOption options[] = {
297
+    {"hls_time",      "segment length in seconds",               OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E},
298
+    {"hls_list_size", "maximum number of playlist entries",      OFFSET(size),    AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E},
299
+    {"hls_wrap",      "number after which the index wraps",      OFFSET(wrap),    AV_OPT_TYPE_INT,    {.i64 = 0},     0, INT_MAX, E},
300
+    { NULL },
301
+};
302
+
303
+static const AVClass hls_class = {
304
+    .class_name = "hls muxer",
305
+    .item_name  = av_default_item_name,
306
+    .option     = options,
307
+    .version    = LIBAVUTIL_VERSION_INT,
308
+};
309
+
310
+
311
+AVOutputFormat ff_hls_muxer = {
312
+    .name           = "hls",
313
+    .long_name      = NULL_IF_CONFIG_SMALL("hls"),
314
+    .extensions     = "m3u8",
315
+    .priv_data_size = sizeof(HLSContext),
316
+    .audio_codec    = AV_CODEC_ID_MP2,
317
+    .video_codec    = AV_CODEC_ID_MPEG2VIDEO,
318
+    .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
319
+    .write_header   = hls_write_header,
320
+    .write_packet   = hls_write_packet,
321
+    .write_trailer  = hls_write_trailer,
322
+    .priv_class     = &hls_class,
323
+};
... ...
@@ -1749,3 +1749,15 @@ enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
1749 1749
 
1750 1750
     return desc - av_pix_fmt_descriptors;
1751 1751
 }
1752
+
1753
+int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt,
1754
+                                     int *h_shift, int *v_shift)
1755
+{
1756
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1757
+    if (!desc)
1758
+        return AVERROR(ENOSYS);
1759
+    *h_shift = desc->log2_chroma_w;
1760
+    *v_shift = desc->log2_chroma_h;
1761
+
1762
+    return 0;
1763
+}
... ...
@@ -206,4 +206,18 @@ const AVPixFmtDescriptor *av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev);
206 206
  */
207 207
 enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc);
208 208
 
209
+/**
210
+ * Utility function to access log2_chroma_w log2_chroma_h from
211
+ * the pixel format AVPixFmtDescriptor.
212
+ *
213
+ * @param[in]  pix_fmt the pixel format
214
+ * @param[out] h_shift store log2_chroma_h
215
+ * @param[out] v_shift store log2_chroma_w
216
+ *
217
+ * @return 0 on success, AVERROR(ENOSYS) on invalid or unknown pixel format
218
+ */
219
+int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt,
220
+                                     int *h_shift, int *v_shift);
221
+
222
+
209 223
 #endif /* AVUTIL_PIXDESC_H */