]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavformat/rtp_h264.c
The FFMpeg can be made to build by OMK hackish way.
[frescor/ffmpeg.git] / libavformat / rtp_h264.c
index 1d5c7665950ad6d681b3c4b3e5fd0b6e4ee5360f..f15b703d935ca4a7afc90469a1f6d231dcc4f2a6 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * RTP H264 Protocol (RFC3984)
- * Copyright (c) 2006 Ryan Martell.
+ * Copyright (c) 2006 Ryan Martell
  *
  * This file is part of FFmpeg.
  *
@@ -20,7 +20,7 @@
  */
 
 /**
-* @file rtp_h264.c
+* @file libavformat/rtp_h264.c
  * @brief H.264 / RTP Code (RFC3984)
  * @author Ryan Martell <rdm4@martellventures.com>
  *
  *
  */
 
+#include "libavutil/base64.h"
+#include "libavutil/avstring.h"
+#include "libavcodec/get_bits.h"
 #include "avformat.h"
 #include "mpegts.h"
-#include "bitstream.h"
 
 #include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
+#include "network.h"
 #include <assert.h>
-#include <arpa/inet.h>
-#include <netdb.h>
 
-#include "rtp_internal.h"
+#include "rtpdec.h"
 #include "rtp_h264.h"
-#include "base64.h"
 
 /**
     RTP/H264 specific private data.
 */
-typedef struct h264_rtp_extra_data {
+struct PayloadContext {
     unsigned long cookie;       ///< sanity check, to make sure we get the pointer we're expecting.
 
     //sdp setup parameters
@@ -66,14 +63,14 @@ typedef struct h264_rtp_extra_data {
 #ifdef DEBUG
     int packet_types_received[32];
 #endif
-} h264_rtp_extra_data;
+};
 
 #define MAGIC_COOKIE (0xdeadbeef)       ///< Cookie for the extradata; to verify we are what we think we are, and that we haven't been freed.
 #define DEAD_COOKIE (0xdeaddead)        ///< Cookie for the extradata; once it is freed.
 
 /* ---------------- private code */
 static void sdp_parse_fmtp_config_h264(AVStream * stream,
-                                       h264_rtp_extra_data * h264_data,
+                                       PayloadContext * h264_data,
                                        char *attr, char *value)
 {
     AVCodecContext *codec = stream->codec;
@@ -81,8 +78,8 @@ static void sdp_parse_fmtp_config_h264(AVStream * stream,
     assert(h264_data != NULL);
 
     if (!strcmp(attr, "packetization-mode")) {
-        av_log(NULL, AV_LOG_DEBUG, "H.264/RTP Packetization Mode: %d\n", atoi(attr));
-        h264_data->packetization_mode = atoi(attr);
+        av_log(codec, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
+        h264_data->packetization_mode = atoi(value);
         /*
            Packetization Mode:
            0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
@@ -90,8 +87,8 @@ static void sdp_parse_fmtp_config_h264(AVStream * stream,
            2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A), and 29 (FU-B) are allowed.
          */
         if (h264_data->packetization_mode > 1)
-            av_log(stream, AV_LOG_ERROR,
-                   "H.264/RTP Interleaved RTP mode is not supported yet.");
+            av_log(codec, AV_LOG_ERROR,
+                   "Interleaved RTP mode is not supported yet.");
     } else if (!strcmp(attr, "profile-level-id")) {
         if (strlen(value) == 6) {
             char buffer[3];
@@ -108,8 +105,8 @@ static void sdp_parse_fmtp_config_h264(AVStream * stream,
             level_idc = strtol(buffer, NULL, 16);
 
             // set the parameters...
-            av_log(NULL, AV_LOG_DEBUG,
-                   "H.264/RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
+            av_log(codec, AV_LOG_DEBUG,
+                   "RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
                    profile_idc, profile_iop, level_idc);
             h264_data->profile_idc = profile_idc;
             h264_data->profile_iop = profile_iop;
@@ -153,29 +150,32 @@ static void sdp_parse_fmtp_config_h264(AVStream * stream,
                     codec->extradata= dest;
                     codec->extradata_size+= sizeof(start_sequence)+packet_size;
                 } else {
-                    av_log(NULL, AV_LOG_ERROR, "H.264/RTP Unable to allocate memory for extradata!");
+                    av_log(codec, AV_LOG_ERROR, "Unable to allocate memory for extradata!");
                 }
             }
         }
-        av_log(NULL, AV_LOG_DEBUG, "H.264/RTP Extradata set to %p (size: %d)!", codec->extradata, codec->extradata_size);
+        av_log(codec, AV_LOG_DEBUG, "Extradata set to %p (size: %d)!", codec->extradata, codec->extradata_size);
     }
 }
 
 // return 0 on packet, no more left, 1 on packet, 1 on partial packet...
-static int h264_handle_packet(RTPDemuxContext * s,
+static int h264_handle_packet(AVFormatContext *ctx,
+                              PayloadContext *data,
+                              AVStream *st,
                               AVPacket * pkt,
                               uint32_t * timestamp,
                               const uint8_t * buf,
-                              int len)
+                              int len, int flags)
 {
-//    h264_rtp_extra_data *data = s->dynamic_protocol_context;
     uint8_t nal = buf[0];
     uint8_t type = (nal & 0x1f);
     int result= 0;
     uint8_t start_sequence[]= {0, 0, 1};
 
+#ifdef DEBUG
     assert(data);
     assert(data->cookie == MAGIC_COOKIE);
+#endif
     assert(buf);
 
     if (type >= 1 && type <= 23)
@@ -231,7 +231,7 @@ static int h264_handle_packet(RTPDemuxContext * s,
                             dst+= nal_size;
                         }
                     } else {
-                        av_log(NULL, AV_LOG_ERROR,
+                        av_log(ctx, AV_LOG_ERROR,
                                "nal size exceeds length: %d %d\n", nal_size, src_len);
                     }
 
@@ -240,7 +240,7 @@ static int h264_handle_packet(RTPDemuxContext * s,
                     src_len -= nal_size;
 
                     if (src_len < 0)
-                        av_log(NULL, AV_LOG_ERROR,
+                        av_log(ctx, AV_LOG_ERROR,
                                "Consumed more bytes than we got! (%d)\n", src_len);
                 } while (src_len > 2);      // because there could be rtp padding..
 
@@ -259,7 +259,7 @@ static int h264_handle_packet(RTPDemuxContext * s,
     case 26:                   // MTAP-16
     case 27:                   // MTAP-24
     case 29:                   // FU-B
-        av_log(NULL, AV_LOG_ERROR,
+        av_log(ctx, AV_LOG_ERROR,
                "Unhandled type (%d) (See RFC for implementation details\n",
                type);
         result= -1;
@@ -272,14 +272,14 @@ static int h264_handle_packet(RTPDemuxContext * s,
             // these are the same as above, we just redo them here for clarity...
             uint8_t fu_indicator = nal;
             uint8_t fu_header = *buf;   // read the fu_header.
-            uint8_t start_bit = (fu_header & 0x80) >> 7;
+            uint8_t start_bit = fu_header >> 7;
 //            uint8_t end_bit = (fu_header & 0x40) >> 6;
             uint8_t nal_type = (fu_header & 0x1f);
             uint8_t reconstructed_nal;
 
             // reconstruct this packet's true nal; only the data follows..
             reconstructed_nal = fu_indicator & (0xe0);  // the original nal forbidden bit and NRI are stored in this packet's nal;
-            reconstructed_nal |= (nal_type & 0x1f);
+            reconstructed_nal |= nal_type;
 
             // skip the fu_header...
             buf++;
@@ -287,7 +287,7 @@ static int h264_handle_packet(RTPDemuxContext * s,
 
 #ifdef DEBUG
             if (start_bit)
-                data->packet_types_received[nal_type & 0x1f]++;
+                data->packet_types_received[nal_type]++;
 #endif
             if(start_bit) {
                 // copy in the start sequence, and the reconstructed nal....
@@ -305,19 +305,21 @@ static int h264_handle_packet(RTPDemuxContext * s,
     case 30:                   // undefined
     case 31:                   // undefined
     default:
-        av_log(NULL, AV_LOG_ERROR, "Undefined type (%d)", type);
+        av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)", type);
         result= -1;
         break;
     }
 
+    pkt->stream_index = st->index;
+
     return result;
 }
 
 /* ---------------- public code */
-static void *h264_new_extradata()
+static PayloadContext *h264_new_extradata(void)
 {
-    h264_rtp_extra_data *data =
-        av_mallocz(sizeof(h264_rtp_extra_data) +
+    PayloadContext *data =
+        av_mallocz(sizeof(PayloadContext) +
                    FF_INPUT_BUFFER_PADDING_SIZE);
 
     if (data) {
@@ -327,9 +329,8 @@ static void *h264_new_extradata()
     return data;
 }
 
-static void h264_free_extradata(void *d)
+static void h264_free_extradata(PayloadContext *data)
 {
-    h264_rtp_extra_data *data = (h264_rtp_extra_data *) d;
 #ifdef DEBUG
     int ii;
 
@@ -350,16 +351,16 @@ static void h264_free_extradata(void *d)
     av_free(data);
 }
 
-static int parse_h264_sdp_line(AVStream * stream, void *data,
-                               const char *line)
+static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
+                               PayloadContext *h264_data, const char *line)
 {
+    AVStream *stream = s->streams[st_index];
     AVCodecContext *codec = stream->codec;
-    h264_rtp_extra_data *h264_data = (h264_rtp_extra_data *) data;
     const char *p = line;
 
     assert(h264_data->cookie == MAGIC_COOKIE);
 
-    if (strstart(p, "framesize:", &p)) {
+    if (av_strstart(p, "framesize:", &p)) {
         char buf1[50];
         char *dst = buf1;
 
@@ -377,7 +378,7 @@ static int parse_h264_sdp_line(AVStream * stream, void *data,
         codec->width = atoi(buf1);
         codec->height = atoi(p + 1); // skip the -
         codec->pix_fmt = PIX_FMT_YUV420P;
-    } else if (strstart(p, "fmtp:", &p)) {
+    } else if (av_strstart(p, "fmtp:", &p)) {
         char attr[256];
         char value[4096];
 
@@ -392,7 +393,7 @@ static int parse_h264_sdp_line(AVStream * stream, void *data,
             /* grab the codec extra_data from the config parameter of the fmtp line */
             sdp_parse_fmtp_config_h264(stream, h264_data, attr, value);
         }
-    } else if (strstart(p, "cliprect:", &p)) {
+    } else if (av_strstart(p, "cliprect:", &p)) {
         // could use this if we wanted.
     }