avformat/dashenc: Added comments
[ffmpeg.git] / libavformat / rtpdec_mpeg4.c
index 167322b..4f70599 100644 (file)
@@ -22,7 +22,7 @@
 
 /**
  * @file
- * @brief MPEG4 / RTP Code
+ * @brief MPEG-4 / RTP Code
  * @author Fabrice Bellard
  * @author Romain Degez
  */
@@ -91,26 +91,20 @@ static const AttrNameMap attr_names[] = {
     { NULL, -1, -1 },
 };
 
-static PayloadContext *new_context(void)
-{
-    return av_mallocz(sizeof(PayloadContext));
-}
-
-static void free_context(PayloadContext *data)
+static void close_context(PayloadContext *data)
 {
     av_freep(&data->au_headers);
     av_freep(&data->mode);
-    av_freep(&data);
 }
 
-static int parse_fmtp_config(AVCodecContext *codec, char *value)
+static int parse_fmtp_config(AVCodecParameters *par, const char *value)
 {
     /* decode the hexa encoded parameter */
     int len = ff_hex_to_data(NULL, value);
-    av_freep(&codec->extradata);
-    if (ff_alloc_extradata(codec, len))
+    av_freep(&par->extradata);
+    if (ff_alloc_extradata(par, len))
         return AVERROR(ENOMEM);
-    ff_hex_to_data(codec->extradata, value);
+    ff_hex_to_data(par->extradata, value);
     return 0;
 }
 
@@ -218,9 +212,9 @@ static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
             data->timestamp = *timestamp;
         }
 
-        if (data->timestamp != *timestamp
-            || data->au_headers[0].size != data->buf_size
-            || data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
+        if (data->timestamp != *timestamp ||
+            data->au_headers[0].size != data->buf_size ||
+            data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
             data->buf_pos = 0;
             data->buf_size = 0;
             av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
@@ -278,28 +272,39 @@ static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
 
 static int parse_fmtp(AVFormatContext *s,
                       AVStream *stream, PayloadContext *data,
-                      char *attr, char *value)
+                      const char *attr, const char *value)
 {
-    AVCodecContext *codec = stream->codec;
+    AVCodecParameters *par = stream->codecpar;
     int res, i;
 
     if (!strcmp(attr, "config")) {
-        res = parse_fmtp_config(codec, value);
+        res = parse_fmtp_config(par, value);
 
         if (res < 0)
             return res;
     }
 
-    if (codec->codec_id == AV_CODEC_ID_AAC) {
+    if (par->codec_id == AV_CODEC_ID_AAC) {
         /* Looking for a known attribute */
         for (i = 0; attr_names[i].str; ++i) {
             if (!av_strcasecmp(attr, attr_names[i].str)) {
                 if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
+                    int val = atoi(value);
+                    if (val > 32) {
+                        av_log(s, AV_LOG_ERROR,
+                               "The %s field size is invalid (%d)\n",
+                               attr, val);
+                        return AVERROR_INVALIDDATA;
+                    }
                     *(int *)((char *)data+
-                        attr_names[i].offset) = atoi(value);
-                } else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
+                        attr_names[i].offset) = val;
+                } else if (attr_names[i].type == ATTR_NAME_TYPE_STR) {
+                    char *val = av_strdup(value);
+                    if (!val)
+                        return AVERROR(ENOMEM);
                     *(char **)((char *)data+
-                        attr_names[i].offset) = av_strdup(value);
+                        attr_names[i].offset) = val;
+                }
             }
         }
     }
@@ -320,29 +325,21 @@ static int parse_sdp_line(AVFormatContext *s, int st_index,
     return 0;
 }
 
-static av_cold int init_video(AVFormatContext *s, int st_index,
-                              PayloadContext *data)
-{
-    if (st_index < 0)
-        return 0;
-    s->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
-    return 0;
-}
-
-RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
+const RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
     .enc_name           = "MP4V-ES",
     .codec_type         = AVMEDIA_TYPE_VIDEO,
     .codec_id           = AV_CODEC_ID_MPEG4,
-    .init               = init_video,
+    .need_parsing       = AVSTREAM_PARSE_FULL,
+    .priv_data_size     = sizeof(PayloadContext),
     .parse_sdp_a_line   = parse_sdp_line,
 };
 
-RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
+const RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
     .enc_name           = "mpeg4-generic",
     .codec_type         = AVMEDIA_TYPE_AUDIO,
     .codec_id           = AV_CODEC_ID_AAC,
+    .priv_data_size     = sizeof(PayloadContext),
     .parse_sdp_a_line   = parse_sdp_line,
-    .alloc              = new_context,
-    .free               = free_context,
-    .parse_packet       = aac_parse_packet
+    .close              = close_context,
+    .parse_packet       = aac_parse_packet,
 };