--- ffmpeg/libavformat/mpjpegdec.c
+++ patches/libavformat/mpjpegdec.c
@@ -40,17 +40,41 @@ static int get_line(AVIOContext *pb, char *line, int line_size)
     return 0;
 }
 
+
+static void trim_right(char* p)
+{
+    if (!p || !*p)
+        return;
+    char* end=p+strlen(p)-1;
+    while (end!=p && av_isspace(*end)) {
+        *end='\0';
+        end--;
+    }
+}
+
 static int split_tag_value(char **tag, char **value, char *line)
 {
+    *tag = NULL;
+    *value = NULL;
+
     char *p = line;
+    int  foundData = 0;
 
-    while (*p != '\0' && *p != ':')
+    while (*p != '\0' && *p != ':') {
+        if (!av_isspace(*p)) {
+            foundData = 1;
+        }
         p++;
-    if (*p != ':')
-        return AVERROR_INVALIDDATA;
+    }
+    if (*p != ':') {
+        if (*p!='\0' || foundData)
+            return AVERROR_INVALIDDATA;
+        return 0;
+    }
 
     *p   = '\0';
     *tag = line;
+    trim_right(*tag);
 
     p++;
 
@@ -59,6 +83,8 @@ static int split_tag_value(char **tag, char **value, char *line)
 
     *value = p;
 
+    trim_right(*value);
+
     return 0;
 }
 
@@ -67,7 +93,7 @@ static int check_content_type(char *line)
     char *tag, *value;
     int ret = split_tag_value(&tag, &value, line);
 
-    if (ret < 0)
+    if (ret < 0 || tag == NULL || value == NULL)
         return ret;
 
     if (av_strcasecmp(tag, "Content-type") ||
@@ -165,7 +191,7 @@ static int parse_multipart_header(AVFormatContext *s)
 
         ret = get_line(s->pb, line, sizeof(line));
         if (ret < 0)
-            return ret;
+            break;
 
         if (line[0] == '\0')
             break;
@@ -173,6 +199,8 @@ static int parse_multipart_header(AVFormatContext *s)
         ret = split_tag_value(&tag, &value, line);
         if (ret < 0)
             return ret;
+        if (value==NULL || tag==NULL)
+            break;
 
         if (!av_strcasecmp(tag, "Content-type")) {
             if (av_strcasecmp(value, "image/jpeg")) {
