LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2012, 08:40 AM   #1
MiniComa
LQ Newbie
 
Registered: Jun 2012
Posts: 15

Rep: Reputation: Disabled
Libipq and c programming (how to read data in packet)


Hello,

I am working with video packets and know that MPEG4 packets begin with hex values 00 00 01 B6. I want to know if I am working with MPEG 4 or not. How can I read information from packet to equal these values? m->payload gives only payload of packet without any headers.

Code:
 case IPQM_PACKET:
 {
// take packets from buffer here
  ipq_packet_msg_t *m = ipq_get_packet(buf);

   int x;
  if(value in m->payload[x] == value) ??
{
  printf("MPEG packet");
x++;
}
 
 // and put them back here
status = ipq_set_verdict(h, m->packet_id,NF_ACCEPT, 0, NULL);
    
  break;
     }
Full code: http://pastebin.com/raw.php?i=c2UgZkEj

MC

Last edited by MiniComa; 07-15-2012 at 07:56 AM.
 
Old 07-15-2012, 06:27 AM   #2
MiniComa
LQ Newbie
 
Registered: Jun 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Tried memcmp() function but this didnt give me wanted results.
 
1 members found this post helpful.
Old 07-15-2012, 06:39 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

According to this man page you can read packet payload using code like
Code:
     int i;
     if( m->data_len > 0 ){ 
         for(i=0; i < m->data_len; i++)
             printf("%02x ", m->payload[i]);
         printf("\n");
     }
I think this payload contains all data except for IP headers.

Are we talking about MPEG4 streaming or transferring of mp4 file over HTTP? What do you mean by "video packet" ?

AFAICT you should decode the payload data depending on the protocol stack used. These four bytes may identify MPEG4 packet only when you know their expected position in the stream, which depends on the protocol stack involved. For example BMP file can also contain this sequence of bytes and only if you know that the file is transferred over (e.g.) TCP/HTTP protocol you may find the expected position of magic numbers.

From your description I suspect that something like MPEG packetized stream is used in your case, but it is hard to say how is it transmitted over IP.

P.S. According to this page libipq has been deprecated in favour of libnetfilter_queue.
 
1 members found this post helpful.
Old 07-15-2012, 07:16 AM   #4
MiniComa
LQ Newbie
 
Registered: Jun 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hi,

Thank you firstfire for replying.

I am using vlc for UDP streaming. All the information is encapsulated in MPEG TS. I use iptables to get all the UDP packets from server to my buffer first. I think that libipq remove all headers (IP,UDP,TCP) and now payload contains MPEG TS header and data. Video packet = mpeg ts + data.

As I work with Ubuntu and execute code, I got error: "Segmentation fault (core dumped)" instantly. I will try to debug it and see where is the problem.

EDIT: I was wrong. This is part of payload of one packet (no headers removed at all ):
IP header [45 00 05 40 87 9b 00 00 80] UDP [11] 2b 96 SRC adr [c0 a8 00 65] c0 a8 00 c6 04 0e 04 d2 05 2c 5c 0f 47 00 e7 1c cc 9b 75
15 9f 70 4d 3c 0f 1e cc cc cd 45 49 a4 64 50 e4 aa e4 6....
I know that I send MPEG 4 but cant find value 00 00 01 B6. So this value can be incorrect.

EDIT 2: I do not use .mp4 container. Information in first post was misleading.

EDIT 3: 45 00 05 40 87 95 00 00 80 11 2b 9c c0 a8 00 65 c0 a8 00 c6 04 0e 04 d2 05 2c ee f3 MPEG TS sync byte 47 00 e7 and e7 = 1110 0111. From MPEG Transport stream
it say that there is only adaptation field and no payload. So the problem can be here.

Last edited by MiniComa; 07-15-2012 at 08:03 AM.
 
Old 07-15-2012, 08:22 AM   #5
MiniComa
LQ Newbie
 
Registered: Jun 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
I have taken packets where there are only payload but havent find wanted value.
 
Old 07-15-2012, 01:27 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by MiniComa View Post
EDIT: I was wrong. This is part of payload of one packet (no headers removed at all ):
IP header [45 00 05 40 87 9b 00 00 80] UDP [11] 2b 96 SRC adr [c0 a8 00 65] c0 a8 00 c6 04 0e 04 d2 05 2c 5c 0f 47 00 e7 1c cc 9b 75
15 9f 70 4d 3c 0f 1e cc cc cd 45 49 a4 64 50 e4 aa e4 6....
I know that I send MPEG 4 but cant find value 00 00 01 B6. So this value can be incorrect.

EDIT 3: 45 00 05 40 87 95 00 00 80 11 2b 9c c0 a8 00 65 c0 a8 00 c6 04 0e 04 d2 05 2c ee f3 MPEG TS sync byte 47 00 e7 and e7 = 1110 0111. From MPEG Transport stream
it say that there is only adaptation field and no payload. So the problem can be here.
As you already figured out, 0x47 looks very much like sync byte. Also 0x040e = 1038 and 0x04d2=1234 are probably source and destination ports. 4 byte Transport Stream Header can be decoded as
Code:
47                  sync byte
00 = 0b00000000
       ^^^^
       |||First 5 bits of packet ID (PID) = 0
       ||Normal priority
       |Not start of a PES or PSI
       No transport error

e7 = 231            Second part of Packet ID. Complete PID = 231.

1c = 0b00011100
       ^^^^^
        | |Continuity counter = 0b1100 = 12
        | Adaptation field exist = 01 = payload only
        Not scrambled
So, my analysis differs from yours in that you have payload only, no adaptation field. And we should note that this packet is not start of Packetized elementary stream (PES), because
Code:
<the byte after sync byte, i.e. 0x00> & (1<<6) == 0
Maybe it is PES that starts with 00 00 01 <stream ID> can be used to detect MPEG4. So you probably should try to find that PES starting packets (for which the above condition is no met) and analyze them.

EDIT: This pdf file near page 49 may be useful.

Last edited by firstfire; 07-15-2012 at 02:18 PM. Reason: Typo fixed
 
1 members found this post helpful.
Old 07-16-2012, 05:57 AM   #7
MiniComa
LQ Newbie
 
Registered: Jun 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Firstfire,

You are right. I have made too fast calculations and counted that PID only consists of 5 bits instead of 13. I will try to read pdf file first.

Edit: This pdf is useful but the main idea of what I want to do is go further and identify not only MPEG packets but I, B, P frames. I have found this information in ISO/IEC 14496-2:2001:

vop_start_code: This is the bit string ‘000001B6’ in hexadecimal. It marks the start of a video object plane.

vop_coding_type: The vop_coding_type identifies whether a VOP is an intra-coded VOP (I), predictive-coded
VOP (P), bidirectionally predictive-coded VOP (B) or sprite coded VOP (S). The meaning of vop_coding_type is
defined in Table 6-20.

Table 6-20 -- Meaning of vop_coding_type
vop_coding_type coding method
00 intra-coded (I)
01 predictive-coded (P)
10 bidirectionally-predictive-coded (B)
11 sprite (S)

Did I interpret this information incorrectly?

Last edited by MiniComa; 07-16-2012 at 06:11 AM.
 
Old 07-16-2012, 08:07 AM   #8
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Well, I'm not really familiar with this matters.. but here is a discussion which looks related.
 
Old 12-13-2012, 03:08 AM   #9
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Rep: Reputation: Disabled
Hi i am also working on the same thing of transport stream..im stuck there..i coudnt read the contents of the .ts file which i m having with me ...can u please post the code here so that it ll be helpfull for me to view the code and i can proceed wit tat file...??
 
Old 12-13-2012, 12:33 PM   #10
MiniComa
LQ Newbie
 
Registered: Jun 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hi. Try this code: http://pastebin.com/uqQdyb3P
 
1 members found this post helpful.
Old 12-19-2012, 12:59 AM   #11
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Rep: Reputation: Disabled
Hi i tried that code but it is not working .. i dont see anywer to access a file which we need to process with.
 
Old 12-27-2012, 03:24 AM   #12
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Rep: Reputation: Disabled
Can any one of you please post the code for reading the "xxx.ts" file and printing its hexdump please? i Dont know how to read it
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A packet filter using libipq which uses ether type field to capture the packet can26_manish Programming 2 10-16-2007 05:35 AM
how do i read the data in the packet that i have captured after packet capture? gajaykrishnan Programming 23 04-19-2006 05:09 AM
packet reordering and delay using libipq shinkm Linux - Networking 0 09-08-2004 12:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration