Hi, This cleans up FMTP parsing code somewhat. I only did it for H.264 and MPEG-4/AAC; if this approach is OK, I'll do the rest (Xiph, AMR, etc).
0001 -- rtpdec.h is a better place for SPACE_CHARS. Removes a dependency on internal.h, and avoids having to #include it in later patches. 0002 -- self explanatory. It is likely I will have to change value[4096] to be malloc'd due to massive Xiph extradata. 003-005 -- H.264 related 0006-007 -- MPEG-4/AAC related Josh
From fc8dbe211bc6d536fae8dba75c83d24ee32c93d8 Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 14:24:58 -0700 Subject: [PATCH 1/7] Move space_chars from avformat/internal to rtpdec. --- libavformat/internal.h | 2 -- libavformat/rtpdec.h | 2 ++ libavformat/rtpdec_mpeg4.c | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/libavformat/internal.h b/libavformat/internal.h index 8a164bd..4489ffe 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -174,6 +174,4 @@ void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src); -#define SPACE_CHARS " \t\r\n" - #endif /* AVFORMAT_INTERNAL_H */ diff --git a/libavformat/rtpdec.h b/libavformat/rtpdec.h index 8a62034..439fcc2 100644 --- a/libavformat/rtpdec.h +++ b/libavformat/rtpdec.h @@ -26,6 +26,8 @@ #include "avformat.h" #include "rtp.h" +#define SPACE_CHARS " \t\r\n" + typedef struct PayloadContext PayloadContext; typedef struct RTPDynamicProtocolHandler_s RTPDynamicProtocolHandler; diff --git a/libavformat/rtpdec_mpeg4.c b/libavformat/rtpdec_mpeg4.c index dec45b4..cb22b32 100644 --- a/libavformat/rtpdec_mpeg4.c +++ b/libavformat/rtpdec_mpeg4.c @@ -28,7 +28,6 @@ */ #include "rtpdec_mpeg4.h" -#include "internal.h" #include "libavutil/avstring.h" #include "libavcodec/get_bits.h" #include <strings.h> -- 1.7.0.4
From 935e858c5d75a8101c9667e21f5ab05ac6942fab Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 15:07:42 -0700 Subject: [PATCH 2/7] Added generic function for iterating over FMTP extradata. This will clean up code that is common among RTP depacketizers. --- libavformat/rtpdec.c | 26 ++++++++++++++++++++++++++ libavformat/rtpdec.h | 6 ++++++ 2 files changed, 32 insertions(+), 0 deletions(-) diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c index 456e2e4..fd8d390 100644 --- a/libavformat/rtpdec.c +++ b/libavformat/rtpdec.c @@ -531,3 +531,29 @@ void rtp_parse_close(RTPDemuxContext *s) } av_free(s); } + +int ff_parse_fmtp(AVFormatContext *s, int st_index, + void *data, const char *p, + int (*parse_fmtp)(AVFormatContext *s, int st_index, + PayloadContext *data, + char *attr, char *value)) +{ + char attr[256]; + char value[4096]; + int res; + + // remove protocol identifier + p += strspn(p, SPACE_CHARS); // strip spaces + while (!(res = strspn(p, SPACE_CHARS))) p++; // protocol identifier + p += res; // strip trailing spaces + + while (ff_rtsp_next_attr_and_value(&p, + attr, sizeof(attr), + value, sizeof(value))) { + + res = parse_fmtp(s, st_index, data, attr, value); + if (res < 0) + return res; + } + return 0; +} diff --git a/libavformat/rtpdec.h b/libavformat/rtpdec.h index 439fcc2..34ad8ba 100644 --- a/libavformat/rtpdec.h +++ b/libavformat/rtpdec.h @@ -172,6 +172,12 @@ void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler); int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size); ///< from rtsp.c, but used by rtp dynamic protocol handlers. +int ff_parse_fmtp(AVFormatContext *s, int st_index, + void *data, const char *p, + int (*parse_fmtp)(AVFormatContext *s, int st_index, + PayloadContext *data, + char *attr, char *value)); + void av_register_rtp_dynamic_payload_handlers(void); #endif /* AVFORMAT_RTPDEC_H */ -- 1.7.0.4
From 4e0b4cac20e92c875428a6232daef4b88eb5183c Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 15:03:06 -0700 Subject: [PATCH 3/7] Return ENOMEM if H.264 RTP fails to allocate memory for SDP extradata. --- libavformat/rtpdec_h264.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-) diff --git a/libavformat/rtpdec_h264.c b/libavformat/rtpdec_h264.c index 49db08a..a6e0de8 100644 --- a/libavformat/rtpdec_h264.c +++ b/libavformat/rtpdec_h264.c @@ -69,7 +69,7 @@ struct PayloadContext { #define DEAD_COOKIE (0xdeaddead) ///< Cookie for the extradata; once it is freed. /* ---------------- private code */ -static void sdp_parse_fmtp_config_h264(AVStream * stream, +static int sdp_parse_fmtp_config_h264(AVStream * stream, PayloadContext * h264_data, char *attr, char *value) { @@ -155,11 +155,13 @@ static void sdp_parse_fmtp_config_h264(AVStream * stream, codec->extradata_size+= sizeof(start_sequence)+packet_size; } else { av_log(codec, AV_LOG_ERROR, "Unable to allocate memory for extradata!"); + return AVERROR(ENOMEM); } } } av_log(codec, AV_LOG_DEBUG, "Extradata set to %p (size: %d)!", codec->extradata, codec->extradata_size); } + return 0; } // return 0 on packet, no more left, 1 on packet, 1 on partial packet... -- 1.7.0.4
From 5733068b37d3bf0d5370e19f409fb2640dcd9a89 Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 15:10:49 -0700 Subject: [PATCH 4/7] Clean up FMTP parsing code in H.264 RTP depacketizer. --- libavformat/rtpdec_h264.c | 19 ++++--------------- 1 files changed, 4 insertions(+), 15 deletions(-) diff --git a/libavformat/rtpdec_h264.c b/libavformat/rtpdec_h264.c index a6e0de8..3ecbf42 100644 --- a/libavformat/rtpdec_h264.c +++ b/libavformat/rtpdec_h264.c @@ -69,10 +69,11 @@ struct PayloadContext { #define DEAD_COOKIE (0xdeaddead) ///< Cookie for the extradata; once it is freed. /* ---------------- private code */ -static int sdp_parse_fmtp_config_h264(AVStream * stream, +static int sdp_parse_fmtp_config_h264(AVFormatContext *s, int st_index, PayloadContext * h264_data, char *attr, char *value) { + AVStream *stream = s->streams[st_index]; AVCodecContext *codec = stream->codec; assert(codec->codec_id == CODEC_ID_H264); assert(h264_data != NULL); @@ -385,20 +386,8 @@ static int parse_h264_sdp_line(AVFormatContext *s, int st_index, codec->height = atoi(p + 1); // skip the - codec->pix_fmt = PIX_FMT_YUV420P; } else if (av_strstart(p, "fmtp:", &p)) { - char attr[256]; - char value[4096]; - - // remove the protocol identifier.. - while (*p && *p == ' ') p++; // strip spaces. - while (*p && *p != ' ') p++; // eat protocol identifier - while (*p && *p == ' ') p++; // strip trailing spaces. - - /* loop on each attribute */ - while (ff_rtsp_next_attr_and_value - (&p, attr, sizeof(attr), value, sizeof(value))) { - /* grab the codec extra_data from the config parameter of the fmtp line */ - sdp_parse_fmtp_config_h264(stream, h264_data, attr, value); - } + return ff_parse_fmtp(s, st_index, h264_data, p, + &sdp_parse_fmtp_config_h264); } else if (av_strstart(p, "cliprect:", &p)) { // could use this if we wanted. } -- 1.7.0.4
From 54718e614c700a2ee497dab7221fd82f16689428 Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 15:16:05 -0700 Subject: [PATCH 5/7] h264 rtp cosmetics --- libavformat/rtpdec_h264.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libavformat/rtpdec_h264.c b/libavformat/rtpdec_h264.c index 3ecbf42..3161b94 100644 --- a/libavformat/rtpdec_h264.c +++ b/libavformat/rtpdec_h264.c @@ -70,8 +70,8 @@ struct PayloadContext { /* ---------------- private code */ static int sdp_parse_fmtp_config_h264(AVFormatContext *s, int st_index, - PayloadContext * h264_data, - char *attr, char *value) + PayloadContext * h264_data, + char *attr, char *value) { AVStream *stream = s->streams[st_index]; AVCodecContext *codec = stream->codec; -- 1.7.0.4
From 5002e69b85a7d718156367edee38d148636e3475 Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 15:11:36 -0700 Subject: [PATCH 6/7] Clean up FMTP parsing code in MPEG-4/AAC RTP depacketizer. --- libavformat/rtpdec_mpeg4.c | 33 ++++++++++++++------------------- 1 files changed, 14 insertions(+), 19 deletions(-) diff --git a/libavformat/rtpdec_mpeg4.c b/libavformat/rtpdec_mpeg4.c index cb22b32..d1999b9 100644 --- a/libavformat/rtpdec_mpeg4.c +++ b/libavformat/rtpdec_mpeg4.c @@ -219,24 +219,12 @@ static int aac_parse_packet(AVFormatContext *ctx, return 0; } -static int parse_sdp_line(AVFormatContext *s, int st_index, - PayloadContext *data, const char *line) +static int parse_fmtp(AVFormatContext *s, int st_index, + PayloadContext *data, char *attr, char *value) { - const char *p; - char value[4096], attr[25]; - int res = 0, i; - AVStream *st = s->streams[st_index]; - AVCodecContext* codec = st->codec; - - if (av_strstart(line, "fmtp:", &p)) { - // remove protocol identifier - while (*p && *p == ' ') p++; // strip spaces - while (*p && *p != ' ') p++; // eat protocol identifier - while (*p && *p == ' ') p++; // strip trailing spaces - - while (ff_rtsp_next_attr_and_value(&p, - attr, sizeof(attr), - value, sizeof(value))) { + AVCodecContext *codec = s->streams[st_index]->codec; + int res, i; + if (!strcmp(attr, "config")) { res = parse_fmtp_config(codec, value); @@ -257,11 +245,18 @@ static int parse_sdp_line(AVFormatContext *s, int st_index, } } } + return 0; } - } - return 0; +static int parse_sdp_line(AVFormatContext *s, int st_index, + PayloadContext *data, const char *line) +{ + const char *p; + + if (av_strstart(line, "fmtp:", &p)) + return ff_parse_fmtp(s, st_index, data, p, &parse_fmtp); + return 0; } RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = { -- 1.7.0.4
From e8f3cf8238129c155b8df2287c4867477f753a3f Mon Sep 17 00:00:00 2001 From: Josh Allmann <[email protected]> Date: Fri, 25 Jun 2010 15:16:53 -0700 Subject: [PATCH 7/7] mpeg4/aac rtp cosmetics. --- libavformat/rtpdec_mpeg4.c | 35 ++++++++++++++++++----------------- 1 files changed, 18 insertions(+), 17 deletions(-) diff --git a/libavformat/rtpdec_mpeg4.c b/libavformat/rtpdec_mpeg4.c index d1999b9..1e67d1e 100644 --- a/libavformat/rtpdec_mpeg4.c +++ b/libavformat/rtpdec_mpeg4.c @@ -225,28 +225,29 @@ static int parse_fmtp(AVFormatContext *s, int st_index, AVCodecContext *codec = s->streams[st_index]->codec; int res, i; - if (!strcmp(attr, "config")) { - res = parse_fmtp_config(codec, value); + if (!strcmp(attr, "config")) { + res = parse_fmtp_config(codec, value); - if (res < 0) - return res; - } + if (res < 0) + return res; + } - if (codec->codec_id == CODEC_ID_AAC) { - /* Looking for a known attribute */ - for (i = 0; attr_names[i].str; ++i) { - if (!strcasecmp(attr, attr_names[i].str)) { - if (attr_names[i].type == ATTR_NAME_TYPE_INT) { - *(int *)((char *)data+ - attr_names[i].offset) = atoi(value); - } else if (attr_names[i].type == ATTR_NAME_TYPE_STR) - *(char **)((char *)data+ - attr_names[i].offset) = av_strdup(value); - } + if (codec->codec_id == CODEC_ID_AAC) { + /* Looking for a known attribute */ + for (i = 0; attr_names[i].str; ++i) { + if (!strcasecmp(attr, attr_names[i].str)) { + if (attr_names[i].type == ATTR_NAME_TYPE_INT) { + *(int *)((char *)data+ + attr_names[i].offset) = atoi(value); + } else if (attr_names[i].type == ATTR_NAME_TYPE_STR) { + *(char **)((char *)data+ + attr_names[i].offset) = av_strdup(value); } } - return 0; } + } + return 0; +} static int parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line) -- 1.7.0.4
_______________________________________________ FFmpeg-soc mailing list [email protected] https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc
