Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
build: fix standalone compilation of OMA muxer
build: fix standalone compilation of Microsoft XMV demuxer
build: fix standalone compilation of Core Audio Format demuxer
kvmc: fix invalid reads
4xm: Add a check in decode_i_frame to prevent buffer overreads
adpcm: fix IMA SMJPEG decoding
options: set minimum for "threads" to zero
bsd: use number of logical CPUs as automatic thread count
windows: use number of CPUs as automatic thread count
linux: use number of CPUs as automatic thread count
pthreads: reset active_thread_type when slice thread_init returrns early
v410dec: include correct headers
Drop ALT_ prefix from BITSTREAM_READER_LE name.
lavfi: always build vsrc_buffer.
ra144enc: zero the reflection coeffs if the filter is unstable
sws: readd PAL8 to isPacked()
mov: Don't stick the QuickTime field ordering atom in extradata.
truespeech: fix invalid reads in truespeech_apply_twopoint_filter()

Conflicts:
configure
libavcodec/4xm.c
libavcodec/avcodec.h
libavfilter/Makefile
libavfilter/allfilters.c
libavformat/Makefile
libswscale/swscale_internal.h

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

Michael Niedermayer authored on 2011/12/23 11:00:12
Showing 48 changed files
... ...
@@ -2139,6 +2139,7 @@ static int transcode_init(OutputFile *output_files,
2139 2139
             codec->bit_rate       = icodec->bit_rate;
2140 2140
             codec->rc_max_rate    = icodec->rc_max_rate;
2141 2141
             codec->rc_buffer_size = icodec->rc_buffer_size;
2142
+            codec->field_order    = icodec->field_order;
2142 2143
             codec->extradata      = av_mallocz(extra_size);
2143 2144
             if (!codec->extradata) {
2144 2145
                 return AVERROR(ENOMEM);
... ...
@@ -1174,6 +1174,7 @@ HAVE_LIST="
1174 1174
     gethrtime
1175 1175
     GetProcessMemoryInfo
1176 1176
     GetProcessTimes
1177
+    GetSystemInfo
1177 1178
     getrusage
1178 1179
     gnu_as
1179 1180
     ibm_asm
... ...
@@ -1206,6 +1207,7 @@ HAVE_LIST="
1206 1206
     posix_memalign
1207 1207
     round
1208 1208
     roundf
1209
+    sched_getaffinity
1209 1210
     sdl
1210 1211
     sdl_video_size
1211 1212
     setmode
... ...
@@ -1224,6 +1226,7 @@ HAVE_LIST="
1224 1224
     symver
1225 1225
     symver_asm_label
1226 1226
     symver_gnu_asm
1227
+    sysctl
1227 1228
     sys_mman_h
1228 1229
     sys_resource_h
1229 1230
     sys_select_h
... ...
@@ -1654,14 +1657,13 @@ postproc_deps="gpl"
1654 1654
 
1655 1655
 # programs
1656 1656
 avconv_deps="avcodec avformat swscale"
1657
-avconv_select="buffer_filter"
1658 1657
 ffplay_deps="avcodec avformat swscale sdl"
1659 1658
 ffplay_select="buffersink_filter rdft"
1660 1659
 ffprobe_deps="avcodec avformat"
1661 1660
 ffserver_deps="avformat ffm_muxer fork rtp_protocol rtsp_demuxer"
1662 1661
 ffserver_extralibs='$ldl'
1663 1662
 ffmpeg_deps="avcodec avformat swscale swresample"
1664
-ffmpeg_select="buffer_filter buffersink_filter"
1663
+ffmpeg_select="buffersink_filter"
1665 1664
 
1666 1665
 doc_deps="texi2html"
1667 1666
 
... ...
@@ -3000,12 +3002,15 @@ check_func  ${malloc_prefix}posix_memalign      && enable posix_memalign
3000 3000
 check_func  setrlimit
3001 3001
 check_func  strerror_r
3002 3002
 check_func  strptime
3003
+check_func  sched_getaffinity
3004
+check_func  sysctl
3003 3005
 check_func_headers conio.h kbhit
3004 3006
 check_func_headers windows.h PeekNamedPipe
3005 3007
 check_func_headers io.h setmode
3006 3008
 check_func_headers lzo/lzo1x.h lzo1x_999_compress
3007 3009
 check_lib2 "windows.h psapi.h" GetProcessMemoryInfo -lpsapi
3008 3010
 check_func_headers windows.h GetProcessTimes
3011
+check_func_headers windows.h GetSystemInfo
3009 3012
 check_func_headers windows.h MapViewOfFile
3010 3013
 check_func_headers windows.h VirtualAlloc
3011 3014
 
... ...
@@ -2213,6 +2213,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2213 2213
             codec->bit_rate       = icodec->bit_rate;
2214 2214
             codec->rc_max_rate    = icodec->rc_max_rate;
2215 2215
             codec->rc_buffer_size = icodec->rc_buffer_size;
2216
+            codec->field_order    = icodec->field_order;
2216 2217
             codec->extradata      = av_mallocz(extra_size);
2217 2218
             if (!codec->extradata) {
2218 2219
                 return AVERROR(ENOMEM);
... ...
@@ -688,10 +688,13 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length){
688 688
     unsigned int prestream_size;
689 689
     const uint8_t *prestream;
690 690
 
691
-    if (bitstream_size > (1<<26) || length < bitstream_size + 12)
692
-        return -1;
693
-    prestream_size = 4*AV_RL32(buf + bitstream_size + 4);
694
-    prestream = buf + bitstream_size + 12;
691
+    if (bitstream_size > (1<<26) || length < bitstream_size + 12) {
692
+        av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
693
+        return AVERROR_INVALIDDATA;
694
+    }
695
+
696
+    prestream_size = 4 * AV_RL32(buf + bitstream_size + 4);
697
+    prestream      = buf + bitstream_size + 12;
695 698
 
696 699
     if (prestream_size > (1<<26) ||
697 700
         prestream_size != length - (bitstream_size + 12)){
... ...
@@ -1007,11 +1007,15 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
1007 1007
         break;
1008 1008
     case CODEC_ID_ADPCM_IMA_AMV:
1009 1009
     case CODEC_ID_ADPCM_IMA_SMJPEG:
1010
-        c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
1011
-        c->status[0].step_index = bytestream_get_le16(&src);
1012
-
1013
-        if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
1014
-            src+=4;
1010
+        if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
1011
+            c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16);
1012
+            c->status[0].step_index = bytestream_get_le16(&src);
1013
+            src += 4;
1014
+        } else {
1015
+            c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16);
1016
+            c->status[0].step_index = bytestream_get_byte(&src);
1017
+            src += 1;
1018
+        }
1015 1019
 
1016 1020
         for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
1017 1021
             char hi, lo;
... ...
@@ -20,7 +20,7 @@
20 20
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 21
  */
22 22
 
23
-#define ALT_BITSTREAM_READER_LE
23
+#define BITSTREAM_READER_LE
24 24
 #include "avcodec.h"
25 25
 #include "dsputil.h"
26 26
 #include "get_bits.h"
... ...
@@ -1335,6 +1335,15 @@ typedef struct AVFrame {
1335 1335
 
1336 1336
 struct AVCodecInternal;
1337 1337
 
1338
+enum AVFieldOrder {
1339
+    AV_FIELD_UNKNOWN,
1340
+    AV_FIELD_PROGRESSIVE,
1341
+    AV_FIELD_TT,          //< Top coded_first, top displayed first
1342
+    AV_FIELD_BB,          //< Bottom coded first, bottom displayed first
1343
+    AV_FIELD_TB,          //< Top coded first, bottom displayed first
1344
+    AV_FIELD_BT,          //< Bottom coded first, top displayed first
1345
+};
1346
+
1338 1347
 /**
1339 1348
  * main external API structure.
1340 1349
  * New fields can be added to the end with minor version bumps.
... ...
@@ -3191,6 +3200,12 @@ typedef struct AVCodecContext {
3191 3191
      */
3192 3192
     struct AVCodecInternal *internal;
3193 3193
 
3194
+    /** Field order
3195
+     * - encoding: set by libavcodec
3196
+     * - decoding: Set by libavcodec
3197
+     */
3198
+    enum AVFieldOrder field_order;
3199
+
3194 3200
     /**
3195 3201
      * Current statistics for PTS correction.
3196 3202
      * - decoding: maintained and used by libavcodec, not intended to be used by user apps
... ...
@@ -27,7 +27,7 @@
27 27
 #include "binkdsp.h"
28 28
 #include "mathops.h"
29 29
 
30
-#define ALT_BITSTREAM_READER_LE
30
+#define BITSTREAM_READER_LE
31 31
 #include "get_bits.h"
32 32
 
33 33
 #define BINK_FLAG_ALPHA 0x00100000
... ...
@@ -29,7 +29,7 @@
29 29
  */
30 30
 
31 31
 #include "avcodec.h"
32
-#define ALT_BITSTREAM_READER_LE
32
+#define BITSTREAM_READER_LE
33 33
 #include "get_bits.h"
34 34
 #include "dsputil.h"
35 35
 #include "dct.h"
... ...
@@ -29,7 +29,7 @@
29 29
  */
30 30
 
31 31
 #include "avcodec.h"
32
-#define ALT_BITSTREAM_READER_LE
32
+#define BITSTREAM_READER_LE
33 33
 #include "get_bits.h"
34 34
 #include "bytestream.h"
35 35
 #include "dsputil.h"
... ...
@@ -29,7 +29,7 @@
29 29
  */
30 30
 
31 31
 #include "avcodec.h"
32
-#define ALT_BITSTREAM_READER_LE
32
+#define BITSTREAM_READER_LE
33 33
 #include "get_bits.h"
34 34
 #include "libavutil/lzo.h"
35 35
 #include "libavutil/imgutils.h"
... ...
@@ -21,7 +21,7 @@
21 21
 
22 22
 #include "avcodec.h"
23 23
 
24
-#define ALT_BITSTREAM_READER_LE
24
+#define BITSTREAM_READER_LE
25 25
 #include "get_bits.h"
26 26
 
27 27
 typedef union MacroBlock {
... ...
@@ -21,7 +21,7 @@
21 21
 
22 22
 #include "avcodec.h"
23 23
 
24
-#define ALT_BITSTREAM_READER_LE
24
+#define BITSTREAM_READER_LE
25 25
 #include "get_bits.h"
26 26
 
27 27
 typedef struct Escape130Context {
... ...
@@ -26,7 +26,7 @@
26 26
  */
27 27
 
28 28
 #include "avcodec.h"
29
-#define ALT_BITSTREAM_READER_LE
29
+#define BITSTREAM_READER_LE
30 30
 #include "get_bits.h"
31 31
 #include "acelp_vectors.h"
32 32
 #include "celp_filters.h"
... ...
@@ -124,7 +124,7 @@ for examples see get_bits, show_bits, skip_bits, get_vlc
124 124
 
125 125
 #define CLOSE_READER(name, gb) (gb)->index = name##_index
126 126
 
127
-#ifdef ALT_BITSTREAM_READER_LE
127
+#ifdef BITSTREAM_READER_LE
128 128
 
129 129
 # ifdef LONG_BITSTREAM_READER
130 130
 #   define UPDATE_CACHE(name, gb) name##_cache = \
... ...
@@ -164,7 +164,7 @@ for examples see get_bits, show_bits, skip_bits, get_vlc
164 164
 
165 165
 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
166 166
 
167
-#ifdef ALT_BITSTREAM_READER_LE
167
+#ifdef BITSTREAM_READER_LE
168 168
 #   define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
169 169
 #   define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
170 170
 #else
... ...
@@ -254,7 +254,7 @@ static inline unsigned int get_bits1(GetBitContext *s)
254 254
 {
255 255
     unsigned int index = s->index;
256 256
     uint8_t result = s->buffer[index>>3];
257
-#ifdef ALT_BITSTREAM_READER_LE
257
+#ifdef BITSTREAM_READER_LE
258 258
     result >>= index & 7;
259 259
     result &= 1;
260 260
 #else
... ...
@@ -288,7 +288,7 @@ static inline unsigned int get_bits_long(GetBitContext *s, int n)
288 288
     if (n <= MIN_CACHE_BITS)
289 289
         return get_bits(s, n);
290 290
     else {
291
-#ifdef ALT_BITSTREAM_READER_LE
291
+#ifdef BITSTREAM_READER_LE
292 292
         int ret = get_bits(s, 16);
293 293
         return ret | (get_bits(s, n-16) << 16);
294 294
 #else
... ...
@@ -23,7 +23,7 @@
23 23
  * @file
24 24
  * Intel Indeo 2 decoder.
25 25
  */
26
-#define ALT_BITSTREAM_READER_LE
26
+#define BITSTREAM_READER_LE
27 27
 #include "avcodec.h"
28 28
 #include "get_bits.h"
29 29
 #include "indeo2data.h"
... ...
@@ -163,7 +163,7 @@ static int ir2_decode_frame(AVCodecContext *avctx,
163 163
     s->decode_delta = buf[18];
164 164
 
165 165
     /* decide whether frame uses deltas or not */
166
-#ifndef ALT_BITSTREAM_READER_LE
166
+#ifndef BITSTREAM_READER_LE
167 167
     for (i = 0; i < buf_size; i++)
168 168
         buf[i] = av_reverse[buf[i]];
169 169
 #endif
... ...
@@ -205,7 +205,7 @@ static av_cold int ir2_decode_init(AVCodecContext *avctx){
205 205
 
206 206
     ir2_vlc.table = vlc_tables;
207 207
     ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
208
-#ifdef ALT_BITSTREAM_READER_LE
208
+#ifdef BITSTREAM_READER_LE
209 209
         init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
210 210
                  &ir2_codes[0][1], 4, 2,
211 211
                  &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
... ...
@@ -26,7 +26,7 @@
26 26
 
27 27
 #define IR2_CODES 143
28 28
 static const uint16_t ir2_codes[IR2_CODES][2] = {
29
-#ifdef ALT_BITSTREAM_READER_LE
29
+#ifdef BITSTREAM_READER_LE
30 30
 {0x0000,  3}, {0x0004,  3}, {0x0006,  3}, {0x0001,  5},
31 31
 {0x0009,  5}, {0x0019,  5}, {0x000D,  5}, {0x001D,  5},
32 32
 {0x0023,  6}, {0x0013,  6}, {0x0033,  6}, {0x000B,  6},
... ...
@@ -27,7 +27,7 @@
27 27
  * Known FOURCCs: 'IV50'
28 28
  */
29 29
 
30
-#define ALT_BITSTREAM_READER_LE
30
+#define BITSTREAM_READER_LE
31 31
 #include "avcodec.h"
32 32
 #include "get_bits.h"
33 33
 #include "dsputil.h"
... ...
@@ -41,7 +41,7 @@
41 41
 #include "avcodec.h"
42 42
 #include "bytestream.h"
43 43
 #include "dsputil.h"
44
-#define ALT_BITSTREAM_READER_LE
44
+#define BITSTREAM_READER_LE
45 45
 #include "get_bits.h"
46 46
 
47 47
 #define PALETTE_COUNT 256
... ...
@@ -26,7 +26,7 @@
26 26
  * Indeo5 decoders.
27 27
  */
28 28
 
29
-#define ALT_BITSTREAM_READER_LE
29
+#define BITSTREAM_READER_LE
30 30
 #include "avcodec.h"
31 31
 #include "get_bits.h"
32 32
 #include "ivi_common.h"
... ...
@@ -57,17 +57,21 @@ typedef struct BitBuf {
57 57
 
58 58
 #define kmvc_init_getbits(bb, src)  bb.bits = 7; bb.bitbuf = *src++;
59 59
 
60
-#define kmvc_getbit(bb, src, res) {\
60
+#define kmvc_getbit(bb, src, src_end, res) {\
61 61
     res = 0; \
62 62
     if (bb.bitbuf & (1 << bb.bits)) res = 1; \
63 63
     bb.bits--; \
64 64
     if(bb.bits == -1) { \
65
+        if (src >= src_end) { \
66
+            av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); \
67
+            return AVERROR_INVALIDDATA; \
68
+        } \
65 69
         bb.bitbuf = *src++; \
66 70
         bb.bits = 7; \
67 71
     } \
68 72
 }
69 73
 
70
-static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h)
74
+static int kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int src_size, int w, int h)
71 75
 {
72 76
     BitBuf bb;
73 77
     int res, val;
... ...
@@ -75,13 +79,18 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w,
75 75
     int bx, by;
76 76
     int l0x, l1x, l0y, l1y;
77 77
     int mx, my;
78
+    const uint8_t *src_end = src + src_size;
78 79
 
79 80
     kmvc_init_getbits(bb, src);
80 81
 
81 82
     for (by = 0; by < h; by += 8)
82 83
         for (bx = 0; bx < w; bx += 8) {
83
-            kmvc_getbit(bb, src, res);
84
+            kmvc_getbit(bb, src, src_end, res);
84 85
             if (!res) {         // fill whole 8x8 block
86
+                if (src >= src_end) {
87
+                    av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
88
+                    return AVERROR_INVALIDDATA;
89
+                }
85 90
                 val = *src++;
86 91
                 for (i = 0; i < 64; i++)
87 92
                     BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
... ...
@@ -89,14 +98,22 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w,
89 89
                 for (i = 0; i < 4; i++) {
90 90
                     l0x = bx + (i & 1) * 4;
91 91
                     l0y = by + (i & 2) * 2;
92
-                    kmvc_getbit(bb, src, res);
92
+                    kmvc_getbit(bb, src, src_end, res);
93 93
                     if (!res) {
94
-                        kmvc_getbit(bb, src, res);
94
+                        kmvc_getbit(bb, src, src_end, res);
95 95
                         if (!res) {     // fill whole 4x4 block
96
+                            if (src >= src_end) {
97
+                                av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
98
+                                return AVERROR_INVALIDDATA;
99
+                            }
96 100
                             val = *src++;
97 101
                             for (j = 0; j < 16; j++)
98 102
                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
99 103
                         } else {        // copy block from already decoded place
104
+                            if (src >= src_end) {
105
+                                av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
106
+                                return AVERROR_INVALIDDATA;
107
+                            }
100 108
                             val = *src++;
101 109
                             mx = val & 0xF;
102 110
                             my = val >> 4;
... ...
@@ -108,16 +125,24 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w,
108 108
                         for (j = 0; j < 4; j++) {
109 109
                             l1x = l0x + (j & 1) * 2;
110 110
                             l1y = l0y + (j & 2);
111
-                            kmvc_getbit(bb, src, res);
111
+                            kmvc_getbit(bb, src, src_end, res);
112 112
                             if (!res) {
113
-                                kmvc_getbit(bb, src, res);
113
+                                kmvc_getbit(bb, src, src_end, res);
114 114
                                 if (!res) {     // fill whole 2x2 block
115
+                                    if (src >= src_end) {
116
+                                        av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
117
+                                        return AVERROR_INVALIDDATA;
118
+                                    }
115 119
                                     val = *src++;
116 120
                                     BLK(ctx->cur, l1x, l1y) = val;
117 121
                                     BLK(ctx->cur, l1x + 1, l1y) = val;
118 122
                                     BLK(ctx->cur, l1x, l1y + 1) = val;
119 123
                                     BLK(ctx->cur, l1x + 1, l1y + 1) = val;
120 124
                                 } else {        // copy block from already decoded place
125
+                                    if (src >= src_end) {
126
+                                        av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
127
+                                        return AVERROR_INVALIDDATA;
128
+                                    }
121 129
                                     val = *src++;
122 130
                                     mx = val & 0xF;
123 131
                                     my = val >> 4;
... ...
@@ -140,9 +165,11 @@ static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w,
140 140
                 }
141 141
             }
142 142
         }
143
+
144
+    return 0;
143 145
 }
144 146
 
145
-static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h)
147
+static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src_size, int w, int h)
146 148
 {
147 149
     BitBuf bb;
148 150
     int res, val;
... ...
@@ -150,15 +177,20 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w,
150 150
     int bx, by;
151 151
     int l0x, l1x, l0y, l1y;
152 152
     int mx, my;
153
+    const uint8_t *src_end = src + src_size;
153 154
 
154 155
     kmvc_init_getbits(bb, src);
155 156
 
156 157
     for (by = 0; by < h; by += 8)
157 158
         for (bx = 0; bx < w; bx += 8) {
158
-            kmvc_getbit(bb, src, res);
159
+            kmvc_getbit(bb, src, src_end, res);
159 160
             if (!res) {
160
-                kmvc_getbit(bb, src, res);
161
+                kmvc_getbit(bb, src, src_end, res);
161 162
                 if (!res) {     // fill whole 8x8 block
163
+                    if (src >= src_end) {
164
+                        av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
165
+                        return AVERROR_INVALIDDATA;
166
+                    }
162 167
                     val = *src++;
163 168
                     for (i = 0; i < 64; i++)
164 169
                         BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
... ...
@@ -171,14 +203,22 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w,
171 171
                 for (i = 0; i < 4; i++) {
172 172
                     l0x = bx + (i & 1) * 4;
173 173
                     l0y = by + (i & 2) * 2;
174
-                    kmvc_getbit(bb, src, res);
174
+                    kmvc_getbit(bb, src, src_end, res);
175 175
                     if (!res) {
176
-                        kmvc_getbit(bb, src, res);
176
+                        kmvc_getbit(bb, src, src_end, res);
177 177
                         if (!res) {     // fill whole 4x4 block
178
+                            if (src >= src_end) {
179
+                                av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
180
+                                return AVERROR_INVALIDDATA;
181
+                            }
178 182
                             val = *src++;
179 183
                             for (j = 0; j < 16; j++)
180 184
                                 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
181 185
                         } else {        // copy block
186
+                            if (src >= src_end) {
187
+                                av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
188
+                                return AVERROR_INVALIDDATA;
189
+                            }
182 190
                             val = *src++;
183 191
                             mx = (val & 0xF) - 8;
184 192
                             my = (val >> 4) - 8;
... ...
@@ -190,16 +230,24 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w,
190 190
                         for (j = 0; j < 4; j++) {
191 191
                             l1x = l0x + (j & 1) * 2;
192 192
                             l1y = l0y + (j & 2);
193
-                            kmvc_getbit(bb, src, res);
193
+                            kmvc_getbit(bb, src, src_end, res);
194 194
                             if (!res) {
195
-                                kmvc_getbit(bb, src, res);
195
+                                kmvc_getbit(bb, src, src_end, res);
196 196
                                 if (!res) {     // fill whole 2x2 block
197
+                                    if (src >= src_end) {
198
+                                        av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
199
+                                        return AVERROR_INVALIDDATA;
200
+                                    }
197 201
                                     val = *src++;
198 202
                                     BLK(ctx->cur, l1x, l1y) = val;
199 203
                                     BLK(ctx->cur, l1x + 1, l1y) = val;
200 204
                                     BLK(ctx->cur, l1x, l1y + 1) = val;
201 205
                                     BLK(ctx->cur, l1x + 1, l1y + 1) = val;
202 206
                                 } else {        // copy block
207
+                                    if (src >= src_end) {
208
+                                        av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
209
+                                        return AVERROR_INVALIDDATA;
210
+                                    }
203 211
                                     val = *src++;
204 212
                                     mx = (val & 0xF) - 8;
205 213
                                     my = (val >> 4) - 8;
... ...
@@ -222,6 +270,8 @@ static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w,
222 222
                 }
223 223
             }
224 224
         }
225
+
226
+    return 0;
225 227
 }
226 228
 
227 229
 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPacket *avpkt)
... ...
@@ -299,10 +349,10 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa
299 299
         memcpy(ctx->cur, ctx->prev, 320 * 200);
300 300
         break;
301 301
     case 3:
302
-        kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
302
+        kmvc_decode_intra_8x8(ctx, buf, buf_size, avctx->width, avctx->height);
303 303
         break;
304 304
     case 4:
305
-        kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
305
+        kmvc_decode_inter_8x8(ctx, buf, buf_size, avctx->width, avctx->height);
306 306
         break;
307 307
     default:
308 308
         av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
... ...
@@ -112,12 +112,9 @@ av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
112 112
             build_basic_mjpeg_vlc(s);
113 113
         }
114 114
     }
115
-    if (avctx->extradata_size > 9 &&
116
-        AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
117
-        if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
118
-            s->interlace_polarity = 1; /* bottom field first */
119
-            av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
120
-        }
115
+    if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
116
+        s->interlace_polarity = 1; /* bottom field first */
117
+        av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
121 118
     }
122 119
     if (avctx->codec->id == CODEC_ID_AMV)
123 120
         s->flipped = 1;
... ...
@@ -19,7 +19,7 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
-#define ALT_BITSTREAM_READER_LE
22
+#define BITSTREAM_READER_LE
23 23
 #include "avcodec.h"
24 24
 #include "msgsmdec.h"
25 25
 #include "gsmdec_template.c"
... ...
@@ -35,7 +35,7 @@
35 35
 #include "avcodec.h"
36 36
 #include "dsputil.h"
37 37
 
38
-#define ALT_BITSTREAM_READER_LE
38
+#define BITSTREAM_READER_LE
39 39
 #include "get_bits.h"
40 40
 
41 41
 const float ff_nelly_dequantization_table[127] = {
... ...
@@ -41,7 +41,7 @@
41 41
 #include "fmtconvert.h"
42 42
 #include "sinewin.h"
43 43
 
44
-#define ALT_BITSTREAM_READER_LE
44
+#define BITSTREAM_READER_LE
45 45
 #include "get_bits.h"
46 46
 
47 47
 
... ...
@@ -371,7 +371,8 @@ static const AVOption options[]={
371 371
 {"float", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_AA_FLOAT }, INT_MIN, INT_MAX, V|D, "aa"},
372 372
 #endif
373 373
 {"qns", "quantizer noise shaping", OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
374
-{"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.dbl = 1 }, INT_MIN, INT_MAX, V|E|D},
374
+{"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.dbl = 1 }, 0, INT_MAX, V|E|D, "threads"},
375
+{"auto", "detect a good number of threads", 0, AV_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, V|E|D, "threads"},
375 376
 {"me_threshold", "motion estimaton threshold", OFFSET(me_threshold), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
376 377
 {"mb_threshold", "macroblock threshold", OFFSET(mb_threshold), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
377 378
 {"dc", "intra_dc_precision", OFFSET(intra_dc_precision), AV_OPT_TYPE_INT, {.dbl = 0 }, INT_MIN, INT_MAX, V|E},
... ...
@@ -30,6 +30,17 @@
30 30
  */
31 31
 
32 32
 #include "config.h"
33
+
34
+#if HAVE_SCHED_GETAFFINITY
35
+#define _GNU_SOURCE
36
+#include <sched.h>
37
+#elif HAVE_GETSYSTEMINFO
38
+#include <windows.h>
39
+#elif HAVE_SYSCTL
40
+#include <sys/sysctl.h>
41
+#include <sys/types.h>
42
+#endif
43
+
33 44
 #include "avcodec.h"
34 45
 #include "internal.h"
35 46
 #include "thread.h"
... ...
@@ -135,6 +146,40 @@ typedef struct FrameThreadContext {
135 135
     int die;                       ///< Set when threads should exit.
136 136
 } FrameThreadContext;
137 137
 
138
+
139
+/* H264 slice threading seems to be buggy with more than 16 threads,
140
+ * limit the number of threads to 16 for automatic detection */
141
+#define MAX_AUTO_THREADS 16
142
+
143
+static int get_logical_cpus(AVCodecContext *avctx)
144
+{
145
+    int ret, nb_cpus = 1;
146
+#if HAVE_SCHED_GETAFFINITY
147
+    cpu_set_t cpuset;
148
+
149
+    CPU_ZERO(&cpuset);
150
+
151
+    ret = sched_getaffinity(0, sizeof(cpuset), &cpuset);
152
+    if (!ret) {
153
+        nb_cpus = CPU_COUNT(&cpuset);
154
+    }
155
+#elif HAVE_GETSYSTEMINFO
156
+    SYSTEM_INFO sysinfo;
157
+    GetSystemInfo(&sysinfo);
158
+    nb_cpus = sysinfo.dwNumberOfProcessors;
159
+#elif HAVE_SYSCTL
160
+    int mib[2] = { CTL_HW, HW_NCPU };
161
+    size_t len = sizeof(nb_cpus);
162
+
163
+    ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0);
164
+    if (ret == -1)
165
+        nb_cpus = 0;
166
+#endif
167
+    av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
168
+    return FFMIN(nb_cpus, MAX_AUTO_THREADS);
169
+}
170
+
171
+
138 172
 static void* attribute_align_arg worker(void *v)
139 173
 {
140 174
     AVCodecContext *avctx = v;
... ...
@@ -239,8 +284,17 @@ static int thread_init(AVCodecContext *avctx)
239 239
     ThreadContext *c;
240 240
     int thread_count = avctx->thread_count;
241 241
 
242
-    if (thread_count <= 1)
242
+    if (!thread_count) {
243
+        int nb_cpus = get_logical_cpus(avctx);
244
+        // use number of cores + 1 as thread count if there is motre than one
245
+        if (nb_cpus > 1)
246
+            thread_count = avctx->thread_count = nb_cpus + 1;
247
+    }
248
+
249
+    if (thread_count <= 1) {
250
+        avctx->active_thread_type = 0;
243 251
         return 0;
252
+    }
244 253
 
245 254
     c = av_mallocz(sizeof(ThreadContext));
246 255
     if (!c)
... ...
@@ -704,6 +758,13 @@ static int frame_thread_init(AVCodecContext *avctx)
704 704
     FrameThreadContext *fctx;
705 705
     int i, err = 0;
706 706
 
707
+    if (!thread_count) {
708
+        int nb_cpus = get_logical_cpus(avctx);
709
+        // use number of cores + 1 as thread count if there is motre than one
710
+        if (nb_cpus > 1)
711
+            thread_count = avctx->thread_count = nb_cpus + 1;
712
+    }
713
+
707 714
     if (thread_count <= 1) {
708 715
         avctx->active_thread_type = 0;
709 716
         return 0;
... ...
@@ -35,7 +35,7 @@
35 35
 #include <stddef.h>
36 36
 #include <stdio.h>
37 37
 
38
-#define ALT_BITSTREAM_READER_LE
38
+#define BITSTREAM_READER_LE
39 39
 #include "avcodec.h"
40 40
 #include "get_bits.h"
41 41
 #include "dsputil.h"
... ...
@@ -477,7 +477,10 @@ static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
477 477
          * The filter is unstable: use the coefficients of the previous frame.
478 478
          */
479 479
         ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
480
-        ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx);
480
+        if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
481
+            /* the filter is still unstable. set reflection coeffs to zero. */
482
+            memset(lpc_refl, 0, sizeof(lpc_refl));
483
+        }
481 484
     }
482 485
     init_put_bits(&pb, frame, buf_size);
483 486
     for (i = 0; i < LPC_ORDER; i++) {
... ...
@@ -20,7 +20,7 @@
20 20
  */
21 21
 
22 22
 #include "avcodec.h"
23
-#define ALT_BITSTREAM_READER_LE
23
+#define BITSTREAM_READER_LE
24 24
 #include "get_bits.h"
25 25
 #include "ra288.h"
26 26
 #include "lpc.h"
... ...
@@ -27,7 +27,7 @@
27 27
 
28 28
 #include "libavutil/mathematics.h"
29 29
 #include "avcodec.h"
30
-#define ALT_BITSTREAM_READER_LE
30
+#define BITSTREAM_READER_LE
31 31
 #include "get_bits.h"
32 32
 #include "dsputil.h"
33 33
 
... ...
@@ -35,7 +35,7 @@
35 35
 #include "libavutil/audioconvert.h"
36 36
 #include "mathops.h"
37 37
 
38
-#define ALT_BITSTREAM_READER_LE
38
+#define BITSTREAM_READER_LE
39 39
 #include "get_bits.h"
40 40
 #include "bytestream.h"
41 41
 
... ...
@@ -25,7 +25,7 @@
25 25
  */
26 26
 
27 27
 #include "avcodec.h"
28
-#define ALT_BITSTREAM_READER_LE
28
+#define BITSTREAM_READER_LE
29 29
 #include "get_bits.h"
30 30
 
31 31
 
... ...
@@ -179,6 +179,7 @@ static void truespeech_apply_twopoint_filter(TSContext *dec, int quart)
179 179
     for(i = 0; i < 146; i++)
180 180
         tmp[i] = dec->filtbuf[i];
181 181
     off = (t / 25) + dec->offset1[quart >> 1] + 18;
182
+    off = av_clip(off, 0, 145);
182 183
     ptr0 = tmp + 145 - off;
183 184
     ptr1 = tmp + 146;
184 185
     filter = (const int16_t*)ts_order2_coeffs + (t % 25) * 2;
... ...
@@ -27,7 +27,7 @@
27 27
  * @author Alex Beregszaszi
28 28
  */
29 29
 
30
-#define ALT_BITSTREAM_READER_LE
30
+#define BITSTREAM_READER_LE
31 31
 //#define DEBUG
32 32
 #include <limits.h>
33 33
 #include "avcodec.h"
... ...
@@ -20,8 +20,8 @@
20 20
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 21
  */
22 22
 
23
+#include "libavutil/intreadwrite.h"
23 24
 #include "avcodec.h"
24
-#include "get_bits.h"
25 25
 
26 26
 static av_cold int v410_decode_init(AVCodecContext *avctx)
27 27
 {
... ...
@@ -24,7 +24,7 @@
24 24
  * VBLE Decoder
25 25
  */
26 26
 
27
-#define ALT_BITSTREAM_READER_LE
27
+#define BITSTREAM_READER_LE
28 28
 
29 29
 #include "avcodec.h"
30 30
 #include "dsputil.h"
... ...
@@ -26,7 +26,7 @@
26 26
  * @author Denes Balatoni  ( dbalatoni programozo hu )
27 27
  */
28 28
 
29
-#define ALT_BITSTREAM_READER_LE
29
+#define BITSTREAM_READER_LE
30 30
 #include "avcodec.h"
31 31
 #include "get_bits.h"
32 32
 
... ...
@@ -29,7 +29,7 @@
29 29
 #include <inttypes.h>
30 30
 #include <math.h>
31 31
 
32
-#define ALT_BITSTREAM_READER_LE
32
+#define BITSTREAM_READER_LE
33 33
 #include "avcodec.h"
34 34
 #include "get_bits.h"
35 35
 #include "dsputil.h"
... ...
@@ -18,7 +18,7 @@
18 18
  * License along with FFmpeg; if not, write to the Free Software
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21
-#define ALT_BITSTREAM_READER_LE
21
+#define BITSTREAM_READER_LE
22 22
 #include "avcodec.h"
23 23
 #include "get_bits.h"
24 24
 #include "unary.h"
... ...
@@ -35,7 +35,7 @@
35 35
 #include "libavutil/intreadwrite.h"
36 36
 #include "avcodec.h"
37 37
 #include "bytestream.h"
38
-#define ALT_BITSTREAM_READER_LE
38
+#define BITSTREAM_READER_LE
39 39
 #include "get_bits.h"
40 40
 // for av_memcpy_backptr
41 41
 #include "libavutil/lzo.h"
... ...
@@ -23,7 +23,7 @@
23 23
 #include "avcodec.h"
24 24
 #include "libavutil/intreadwrite.h"
25 25
 #include "bytestream.h"
26
-#define ALT_BITSTREAM_READER_LE
26
+#define BITSTREAM_READER_LE
27 27
 #include "get_bits.h"
28 28
 // for av_memcpy_backptr
29 29
 #include "libavutil/lzo.h"
... ...
@@ -20,6 +20,7 @@ OBJS = allfilters.o                                                     \
20 20
        formats.o                                                        \
21 21
        graphparser.o                                                    \
22 22
        transform.o                                                      \
23
+       vsrc_buffer.o
23 24
 
24 25
 OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
25 26
 
... ...
@@ -83,7 +84,6 @@ OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
83 83
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
84 84
 OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
85 85
 
86
-OBJS-$(CONFIG_BUFFER_FILTER)                 += vsrc_buffer.o
87 86
 OBJS-$(CONFIG_CELLAUTO_FILTER)               += vsrc_cellauto.o
88 87
 OBJS-$(CONFIG_COLOR_FILTER)                  += vsrc_color.o
89 88
 OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
... ...
@@ -94,7 +94,6 @@ void avfilter_register_all(void)
94 94
     REGISTER_FILTER (VFLIP,       vflip,       vf);
95 95
     REGISTER_FILTER (YADIF,       yadif,       vf);
96 96
 
97
-    REGISTER_FILTER (BUFFER,      buffer,      vsrc);
98 97
     REGISTER_FILTER (CELLAUTO,    cellauto,    vsrc);
99 98
     REGISTER_FILTER (COLOR,       color,       vsrc);
100 99
     REGISTER_FILTER (FREI0R,      frei0r_src,  vsrc);
... ...
@@ -108,4 +107,10 @@ void avfilter_register_all(void)
108 108
 
109 109
     REGISTER_FILTER (BUFFERSINK,  buffersink,  vsink);
110 110
     REGISTER_FILTER (NULLSINK,    nullsink,    vsink);
111
+
112
+    /* vsrc_buffer is a part of public API => registered unconditionally */
113
+    {
114
+        extern avfilter_vsrc_buffer;
115
+        avfilter_register(&avfilter_vsrc_buffer);
116
+    }
111 117
 }
... ...
@@ -56,7 +56,8 @@ OBJS-$(CONFIG_BIT_DEMUXER)               += bit.o
56 56
 OBJS-$(CONFIG_BIT_MUXER)                 += bit.o
57 57
 OBJS-$(CONFIG_BMV_DEMUXER)               += bmv.o
58 58
 OBJS-$(CONFIG_C93_DEMUXER)               += c93.o vocdec.o voc.o
59
-OBJS-$(CONFIG_CAF_DEMUXER)               += cafdec.o caf.o mov.o riff.o isom.o
59
+OBJS-$(CONFIG_CAF_DEMUXER)               += cafdec.o caf.o mov.o mov_chan.o \
60
+                                            riff.o isom.o
60 61
 OBJS-$(CONFIG_CAF_MUXER)                 += cafenc.o caf.o riff.o isom.o
61 62
 OBJS-$(CONFIG_CAVSVIDEO_DEMUXER)         += cavsvideodec.o rawdec.o
62 63
 OBJS-$(CONFIG_CAVSVIDEO_MUXER)           += rawenc.o
... ...
@@ -195,7 +196,7 @@ OBJS-$(CONFIG_OGG_DEMUXER)               += oggdec.o         \
195 195
 OBJS-$(CONFIG_OGG_MUXER)                 += oggenc.o \
196 196
                                             vorbiscomment.o
197 197
 OBJS-$(CONFIG_OMA_DEMUXER)               += omadec.o pcm.o oma.o
198
-OBJS-$(CONFIG_OMA_MUXER)                 += omaenc.o rawenc.o oma.o
198
+OBJS-$(CONFIG_OMA_MUXER)                 += omaenc.o rawenc.o oma.o id3v2enc.o
199 199
 OBJS-$(CONFIG_PCM_ALAW_DEMUXER)          += pcmdec.o pcm.o rawdec.o
200 200
 OBJS-$(CONFIG_PCM_ALAW_MUXER)            += pcmenc.o rawenc.o
201 201
 OBJS-$(CONFIG_PCM_F32BE_DEMUXER)         += pcmdec.o pcm.o rawdec.o
... ...
@@ -331,7 +332,7 @@ OBJS-$(CONFIG_WTV_MUXER)                 += wtvenc.o wtv.o asf.o asfenc.o riff.o
331 331
 OBJS-$(CONFIG_WV_DEMUXER)                += wv.o apetag.o
332 332
 OBJS-$(CONFIG_XA_DEMUXER)                += xa.o
333 333
 OBJS-$(CONFIG_XBIN_DEMUXER)              += bintext.o sauce.o
334
-OBJS-$(CONFIG_XMV_DEMUXER)               += xmv.o
334
+OBJS-$(CONFIG_XMV_DEMUXER)               += xmv.o riff.o
335 335
 OBJS-$(CONFIG_XWMA_DEMUXER)              += xwma.o riff.o
336 336
 OBJS-$(CONFIG_YOP_DEMUXER)               += yop.o
337 337
 OBJS-$(CONFIG_YUV4MPEGPIPE_MUXER)        += yuv4mpeg.o
... ...
@@ -857,6 +857,40 @@ static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
857 857
     return 0;
858 858
 }
859 859
 
860
+static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
861
+{
862
+    AVStream *st;
863
+    unsigned mov_field_order;
864
+    enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
865
+
866
+    if (c->fc->nb_streams < 1) // will happen with jp2 files
867
+        return 0;
868
+    st = c->fc->streams[c->fc->nb_streams-1];
869
+    if (atom.size < 2)
870
+        return AVERROR_INVALIDDATA;
871
+    mov_field_order = avio_rb16(pb);
872
+    if ((mov_field_order & 0xFF00) == 0x0100)
873
+        decoded_field_order = AV_FIELD_PROGRESSIVE;
874
+    else if ((mov_field_order & 0xFF00) == 0x0200) {
875
+        switch (mov_field_order & 0xFF) {
876
+        case 0x01: decoded_field_order = AV_FIELD_TT;
877
+                   break;
878
+        case 0x06: decoded_field_order = AV_FIELD_BB;
879
+                   break;
880
+        case 0x09: decoded_field_order = AV_FIELD_TB;
881
+                   break;
882
+        case 0x0E: decoded_field_order = AV_FIELD_BT;
883
+                   break;
884
+        }
885
+    }
886
+    if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
887
+        av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
888
+    }
889
+    st->codec->field_order = decoded_field_order;
890
+
891
+    return 0;
892
+}
893
+
860 894
 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
861 895
 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
862 896
                               enum CodecID codec_id)
... ...
@@ -898,11 +932,6 @@ static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
898 898
     return mov_read_extradata(c, pb, atom, CODEC_ID_AVS);
899 899
 }
900 900
 
901
-static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
902
-{
903
-    return mov_read_extradata(c, pb, atom, CODEC_ID_MJPEG);
904
-}
905
-
906 901
 static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
907 902
 {
908 903
     return mov_read_extradata(c, pb, atom, CODEC_ID_JPEG2000);
... ...
@@ -950,6 +979,15 @@ static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
950 950
     if ((uint64_t)atom.size > (1<<30))
951 951
         return -1;
952 952
 
953
+    if (atom.size >= 10) {
954
+        // Broken files created by legacy versions of Libav and FFmpeg will
955
+        // wrap a whole fiel atom inside of a glbl atom.
956
+        unsigned size = avio_rb32(pb);
957
+        unsigned type = avio_rl32(pb);
958
+        avio_seek(pb, -8, SEEK_CUR);
959
+        if (type == MKTAG('f','i','e','l') && size == atom.size)
960
+            return mov_read_default(c, pb, atom);
961
+    }
953 962
     av_free(st->codec->extradata);
954 963
     st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
955 964
     if (!st->codec->extradata)
... ...
@@ -799,6 +799,23 @@ static int mov_write_uuid_tag_ipod(AVIOContext *pb)
799 799
     return 28;
800 800
 }
801 801
 
802
+static const uint16_t fiel_data[] = {
803
+    0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
804
+};
805
+
806
+static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
807
+{
808
+    unsigned mov_field_order = 0;
809
+    if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
810
+        mov_field_order = fiel_data[track->enc->field_order];
811
+    else
812
+        return 0;
813
+    avio_wb32(pb, 10);
814
+    ffio_wfourcc(pb, "fiel");
815
+    avio_wb16(pb, mov_field_order);
816
+    return 10;
817
+}
818
+
802 819
 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
803 820
 {
804 821
     int64_t pos = avio_tell(pb);
... ...
@@ -885,7 +902,9 @@ static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
885 885
         mov_write_avcc_tag(pb, track);
886 886
         if(track->mode == MODE_IPOD)
887 887
             mov_write_uuid_tag_ipod(pb);
888
-    } else if(track->vosLen > 0)
888
+    } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
889
+        mov_write_fiel_tag(pb, track);
890
+    else if(track->vosLen > 0)
889 891
         mov_write_glbl_tag(pb, track);
890 892
 
891 893
     if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
... ...
@@ -655,9 +655,11 @@ const char *sws_format_name(enum PixelFormat format);
655 655
         ||  isBGRinInt(x)           \
656 656
     )
657 657
 #else
658
-#define isPacked(x) \
658
+#define isPacked(x) (\
659 659
     (av_pix_fmt_descriptors[x].nb_components >= 2 && \
660
-     !(av_pix_fmt_descriptors[x].flags & PIX_FMT_PLANAR))
660
+     !(av_pix_fmt_descriptors[x].flags & PIX_FMT_PLANAR)) || \
661
+    (x) == PIX_FMT_PAL8\
662
+    )
661 663
 
662 664
 #endif
663 665
 #define isPlanar(x) \