[FFmpeg-devel] [PATCH] Make nutdec return meaningful error codes.

Stefano Sabatini stefano.sabatini-lala
Tue Dec 28 02:32:44 CET 2010


---
 libavformat/nutdec.c |   62 +++++++++++++++++++++++++-------------------------
 1 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c
index d5d7c2b..ee953d5 100644
--- a/libavformat/nutdec.c
+++ b/libavformat/nutdec.c
@@ -49,7 +49,7 @@ static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){
         string[FFMIN(len, maxlen-1)]= 0;
 
     if(maxlen == len)
-        return -1;
+        return AVERROR(EINVAL);
     else
         return 0;
 }
@@ -66,7 +66,7 @@ static uint64_t get_fourcc(ByteIOContext *bc){
 
     if     (len==2) return get_le16(bc);
     else if(len==4) return get_le32(bc);
-    else            return -1;
+    else            return AVERROR(EINVAL);
 }
 
 #ifdef TRACE
@@ -108,7 +108,7 @@ static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_ch
     if(size > 4096)
         get_be32(bc);
     if(get_checksum(bc) && size > 4096)
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
 
@@ -142,7 +142,7 @@ static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
  * Find the given startcode.
  * @param code the startcode
  * @param pos the start position of the search, or -1 if the current position
- * @return the position of the startcode or -1 if not found
+ * @return the position of the startcode or AVERROR(EINVAL) if not found
  */
 static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
     for(;;){
@@ -150,7 +150,7 @@ static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
         if(startcode == code)
             return url_ftell(bc) - 8;
         else if(startcode == 0)
-            return -1;
+            return AVERROR(EINVAL);
         pos=-1;
     }
 }
@@ -171,7 +171,7 @@ static int nut_probe(AVProbeData *p){
     tmp= ff_get_v(bc);\
     if(!(check)){\
         av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\
-        return -1;\
+        return AVERROR(EINVAL);                                         \
     }\
     dst= tmp;
 
@@ -179,7 +179,7 @@ static int skip_reserved(ByteIOContext *bc, int64_t pos){
     pos -= url_ftell(bc);
     if(pos<0){
         url_fseek(bc, pos, SEEK_CUR);
-        return -1;
+        return pos;
     }else{
         while(pos--)
             get_byte(bc);
@@ -215,7 +215,7 @@ static int decode_main_header(NUTContext *nut){
         GET_V(nut->time_base[i].den, tmp>0 && tmp<(1ULL<<31))
         if(av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1){
             av_log(s, AV_LOG_ERROR, "time base invalid\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
     }
     tmp_pts=0;
@@ -243,11 +243,11 @@ static int decode_main_header(NUTContext *nut){
 
         if(count == 0 || i+count > 256){
             av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
         if(tmp_stream >= stream_count){
             av_log(s, AV_LOG_ERROR, "illegal stream number\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
 
         for(j=0; j<count; j++,i++){
@@ -276,7 +276,7 @@ static int decode_main_header(NUTContext *nut){
             rem -= nut->header_len[i];
             if(rem < 0){
                 av_log(s, AV_LOG_ERROR, "invalid elision header\n");
-                return -1;
+                return AVERROR_INVALIDDATA;
             }
             nut->header[i]= av_malloc(nut->header_len[i]);
             get_buffer(bc, nut->header[i], nut->header_len[i]);
@@ -286,7 +286,7 @@ static int decode_main_header(NUTContext *nut){
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
         av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     nut->stream = av_mallocz(sizeof(StreamContext)*stream_count);
@@ -339,7 +339,7 @@ static int decode_stream_header(NUTContext *nut){
             break;
         default:
             av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
-            return -1;
+            return AVERROR_INVALIDDATA;
     }
     if(class<3 && st->codec->codec_id == CODEC_ID_NONE)
         av_log(s, AV_LOG_ERROR, "Unknown codec tag '0x%04x' for stream number %d\n",
@@ -365,7 +365,7 @@ static int decode_stream_header(NUTContext *nut){
         st->sample_aspect_ratio.den= ff_get_v(bc);
         if((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)){
             av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n", st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
         ff_get_v(bc); /* csp type */
     }else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
@@ -375,7 +375,7 @@ static int decode_stream_header(NUTContext *nut){
     }
     if(skip_reserved(bc, end) || get_checksum(bc)){
         av_log(s, AV_LOG_ERROR, "stream header %d checksum mismatch\n", stream_id);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     stc->time_base= &nut->time_base[stc->time_base_id];
     av_set_pts_info(s->streams[stream_id], 63, stc->time_base->num, stc->time_base->den);
@@ -470,7 +470,7 @@ static int decode_info_header(NUTContext *nut){
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
         av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     return 0;
 }
@@ -488,13 +488,13 @@ static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
     tmp= ff_get_v(bc);
     *back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);
     if(*back_ptr < 0)
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp / nut->time_base_count);
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
         av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     *ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;
@@ -517,7 +517,7 @@ static int find_and_decode_index(NUTContext *nut){
     url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
     if(get_be64(bc) != INDEX_STARTCODE){
         av_log(s, AV_LOG_ERROR, "no index at the end\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     end= get_packetheader(nut, bc, 1, INDEX_STARTCODE);
@@ -615,7 +615,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
         pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
         if (pos<0+1){
             av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
     }while(decode_main_header(nut) < 0);
 
@@ -625,7 +625,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
         pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
         if (pos<0+1){
             av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
         if(decode_stream_header(nut) >= 0)
             initialized_stream_count++;
@@ -639,7 +639,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
         if(startcode==0){
             av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }else if(startcode == SYNCPOINT_STARTCODE){
             nut->next_startcode= startcode;
             break;
@@ -673,7 +673,7 @@ static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, ui
 
     if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
         av_log(s, AV_LOG_ERROR, "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n", url_ftell(bc), nut->last_syncpoint_pos, nut->max_distance);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     flags          = nut->frame_code[frame_code].flags;
@@ -685,7 +685,7 @@ static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, ui
     *header_idx    = nut->frame_code[frame_code].header_idx;
 
     if(flags & FLAG_INVALID)
-        return -1;
+        return AVERROR_INVALIDDATA;
     if(flags & FLAG_CODED)
         flags ^= ff_get_v(bc);
     if(flags & FLAG_STREAM_ID){
@@ -715,7 +715,7 @@ static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, ui
 
     if(*header_idx >= (unsigned)nut->header_count){
         av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if(size > 4096)
         *header_idx=0;
@@ -725,7 +725,7 @@ static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, ui
         get_be32(bc); //FIXME check this
     }else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
         av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     stc->last_pts= *pts;
@@ -744,7 +744,7 @@ static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
 
     size= decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
     if(size < 0)
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     stc= &nut->stream[stream_id];
 
@@ -790,8 +790,8 @@ static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
             pos-=8;
         }else{
             frame_code = get_byte(bc);
-            if(url_feof(bc))
-                return -1;
+            if ((ret = url_feof(bc)))
+                return ret;
             if(frame_code == 'N'){
                 tmp= frame_code;
                 for(i=1; i<8; i++)
@@ -824,7 +824,7 @@ resync:
 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
             tmp= find_any_startcode(bc, nut->last_syncpoint_pos+1);
             if(tmp==0)
-                return -1;
+                return AVERROR_INVALIDDATA;
 av_log(s, AV_LOG_DEBUG, "sync\n");
             nut->next_startcode= tmp;
         }
@@ -868,7 +868,7 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flag
     if(st->index_entries){
         int index= av_index_search_timestamp(st, pts, flags);
         if(index<0)
-            return -1;
+            return AVERROR_INVALIDDATA;
 
         pos2= st->index_entries[index].pos;
         ts  = st->index_entries[index].timestamp;
-- 
1.7.2.3




More information about the ffmpeg-devel mailing list