At present, I am trying audio decoding for the first time using avcodec_decode_audio4(), which is always returning error.
Note: My overall intention is to merge multiple AMR_NB encoded files into one audio file, and eventually mux this final audio file with another video file into .mp4 container.
Here's the code:
if(avformat_open_input(&sound, "/tmp/aud0", NULL, NULL)!=0) {
printf("Error in opening input file aud0\n");
exit(1);
} else {
printf("Opened aud0 into FormatContext\n");
}
if(avformat_find_stream_info(sound, NULL)<0){
printf("Error in finding stream info\n");
avformat_close_input(&sound);
exit(1);
}
int aud_stream_idx=-1;
for(int count=0; count<sound->nb_streams; count++) {
if(sound->streams[count]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
aud_stream_idx = count;
printf("Audio stream index %d found\n", count);
break;
}
}
if(aud_stream_idx==-1) {
printf("Audio stream not found in the aud0 file\n");
avformat_close_input(&sound);
exit(1);
}
AVCodecContext *audioCC = sound->streams[aud_stream_idx]->codec;
printf("Audio codec_id=%d, codec_name=%s\n", audioCC->codec_id, audioCC->codec_name);
AVCodec *audioC = avcodec_find_decoder(audioCC->codec_id);
if (audioC == NULL) { printf("Couldn't find decoder\n");
avformat_close_input(&sound);
exit(1);
} else {
printf("Found decoder name %s\n", audioCC->codec_name);
}
if(avcodec_open2(audioCC, audioC, NULL)<0) {
printf("Couldn't open decoder\n");
avformat_close_input(&sound);
exit(1);
} else {
printf("Found decoder name %s\n", audioCC->codec_name);
printf("Found bitrate %d\n", audioCC->bit_rate);
printf("Found sample_rate %d\n", audioCC->sample_rate);
printf("Found sample_fmt %d\n", audioCC->sample_fmt); }
/* decode until eof */
av_init_packet(&avpkt);
avpkt.data=NULL;
avpkt.size=0;
if(av_read_frame(sound, &avpkt)<0) {
printf("Couldn't read encoded audio packet\n");
av_free_packet(&avpkt);
avformat_close_input(&sound);
exit(1);
} else {
printf("Read encoded audio packet\n");
printf("avpkt.pts = %d\n", avpkt.pts);
printf("avpkt.dts = %d\n", avpkt.dts);
printf("avpkt.duration = %d\n", avpkt.duration);
printf("avpkt.stream_index = %d\n", avpkt.stream_index);
printf("avpkt.data = %x\n", avpkt.data);
printf("avpkt.data[0] = %02x\n", avpkt.data[0]);
printf("avpkt.data[0]>>>3&0x0f = %02x\n", avpkt.data[0]>>3 & 0x0f);
}
fprintf(stderr, "avpkt.size=%d\n", avpkt.size);
while (avpkt.size > 0) {
int got_frame = 0;
if (!decoded_frame) {
if (!(decoded_frame = avcodec_alloc_frame())) {
fprintf(stderr, "out of memory\n");
exit(1);
} } else
avcodec_get_frame_defaults(decoded_frame);
len = avcodec_decode_audio4(audioCC, decoded_frame, &got_frame, &avpkt);
if (len < 0) {
fprintf(stderr, "Error %d while decoding\n", len);
exit(1);
}
Here is the output I see (I think demuxing is working fine):
Opened aud0 into FormatContext
Audio stream index 0 found
Audio codec_id=73728, codec_name=
Found decoder name
Found decoder name
Found bitrate 5200
Found sample_rate 8000
Found sample_fmt 3
Read encoded audio packet
avpkt.pts = 0
avpkt.dts = 0
avpkt.duration = 160
avpkt.stream_index = 0
avpkt.data = 91e00680
avpkt.data[0] = 04
avpkt.data[0]>>>3&0x0f = 00
avpkt.size=13
[amrnb # 0x7fce9300bc00] Corrupt bitstream
Error -1094995529 while decoding
This audio was created by recording voice on an Android device.
I also tried a .m4a generated by QuickTime, but no luck.
I feel I am missing some crucial step (like not initializing some CodecContext field, or not reading into AVPacket properly). In any case, if someone has any input or similar example, please let me know.
To help others understand ffmpeg error codes, this may be helpful. Error codes from ffmpeg (error.h from avutil) :
http://ffmpeg.org/doxygen/trunk/error_8h_source.html
It turns out the value you specified is :
#define AVERROR_INVALIDDATA FFERRTAG( 'I','N','D','A')
The -1094995529 becomes -0x41444E49 and when you look at those letters, in ACSII, 0x41 = 'A', 0x44 = 'D', 0x4E = 'N, and 0x49 = 'I'. Due to the macro/etc things are reversed, so ADNI becomes INDA, which you can see from the #define snippet, is the AVERROR_INVALIDDATA defined FFERRTAG( 'I','N','D','A').
The rest of the error codes are in that file and I've pasted them below here :
#define AVERROR_BSF_NOT_FOUND FFERRTAG(0xF8,'B','S','F') ///< Bitstream filter not found
#define AVERROR_BUG FFERRTAG( 'B','U','G','!') ///< Internal bug, also see AVERROR_BUG2
#define AVERROR_BUFFER_TOO_SMALL FFERRTAG( 'B','U','F','S') ///< Buffer too small
#define AVERROR_DECODER_NOT_FOUND FFERRTAG(0xF8,'D','E','C') ///< Decoder not found
#define AVERROR_DEMUXER_NOT_FOUND FFERRTAG(0xF8,'D','E','M') ///< Demuxer not found
#define AVERROR_ENCODER_NOT_FOUND FFERRTAG(0xF8,'E','N','C') ///< Encoder not found
#define AVERROR_EOF FFERRTAG( 'E','O','F',' ') ///< End of file
#define AVERROR_EXIT FFERRTAG( 'E','X','I','T') ///< Immediate exit was requested; the called function should not be restarted
#define AVERROR_EXTERNAL FFERRTAG( 'E','X','T',' ') ///< Generic error in an external library
#define AVERROR_FILTER_NOT_FOUND FFERRTAG(0xF8,'F','I','L') ///< Filter not found
#define AVERROR_INVALIDDATA FFERRTAG( 'I','N','D','A') ///< Invalid data found when processing input
#define AVERROR_MUXER_NOT_FOUND FFERRTAG(0xF8,'M','U','X') ///< Muxer not found
#define AVERROR_OPTION_NOT_FOUND FFERRTAG(0xF8,'O','P','T') ///< Option not found
#define AVERROR_PATCHWELCOME FFERRTAG( 'P','A','W','E') ///< Not yet implemented in FFmpeg, patches welcome
#define AVERROR_PROTOCOL_NOT_FOUND FFERRTAG(0xF8,'P','R','O') ///< Protocol not found
#define AVERROR_STREAM_NOT_FOUND FFERRTAG(0xF8,'S','T','R') ///< Stream not found
#define AVERROR_BUG2 FFERRTAG( 'B','U','G',' ')
#define AVERROR_UNKNOWN FFERRTAG( 'U','N','K','N') ///< Unknown error, typically from an external library
#define AVERROR_EXPERIMENTAL (-0x2bb2afa8) ///< Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
#define AVERROR_INPUT_CHANGED (-0x636e6701) ///< Input changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_OUTPUT_CHANGED)
#define AVERROR_OUTPUT_CHANGED (-0x636e6702) ///< Output changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_INPUT_CHANGED)
#define AVERROR_HTTP_BAD_REQUEST FFERRTAG(0xF8,'4','0','0')
#define AVERROR_HTTP_UNAUTHORIZED FFERRTAG(0xF8,'4','0','1')
#define AVERROR_HTTP_FORBIDDEN FFERRTAG(0xF8,'4','0','3')
#define AVERROR_HTTP_NOT_FOUND FFERRTAG(0xF8,'4','0','4')
#define AVERROR_HTTP_OTHER_4XX FFERRTAG(0xF8,'4','X','X')
#define AVERROR_HTTP_SERVER_ERROR FFERRTAG(0xF8,'5','X','X')
first you need to call av_read_frame until you get to EOF. So you need av_read_frame in the loop.
Let me explain how decoding works:
you open formatcontext, codeccontext and decoder. that is the easy part.
in a loop you read packets form one stream. so you need
while(av_read_frame(sound, &avpkt))
in this loop you want to decode the data from the packet. but care, you need to check from which stream the data in the packet is. so its always better to check if the packet contains data from the audiostream
if (avpkt.stream_index == aud_stream_idx)
now you can decode and process the audio.
exmaple:
AVFrame * decoded_frame = av_frame_alloc();
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = NULL;
avpkt.size = 0;
while(av_read_frame(sound, &avpkt))
{
if (avpkt.stream_index == aud_stream_idx)
{
int got_frame;
int len = avcodec_decode_audio4(audioCC, decoded_frame, &got_frame, &avpkt);
}
}
Thats all the the magic :)
PS: to find a stream just call
av_find_best_stream(sound, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)
PPS: did not tested code. just codec here in window :D
PPPS: read the examples. this helped me a lot :)
Related
After reading https://www.quora.com/How-can-I-bypass-the-OS-buffering-during-I-O-in-Linux I want to try to access data on the serial port with the O_DIRECT option, but the only way I can seem to do that is by adding the GNU_SOURCE define but when I tried to execute the program, nothing at all is printed on the screen.
If I remove "#define _GNU_SOURCE" and compile, then the system gives me an error on O_DIRECT.
If I remove the define and the O_DIRECT flag, then incorrect (possibly outdated) data is always read, but the data is printed on the screen.
I still want to use the O_DIRECT flag and be able to see the data, so I feel I need an alternative command to printf and friends, but I don't know how to continue.
I attached the code below:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#define TIMEOUT 5
int main(){
char inb[3]; //our byte buffer
int nread=0; //number bytes read from port
int n; //counter
int iosz=128; //Lets get 128 bytes
int fd=open("/dev/ttyS0", O_NOCTTY | O_RDONLY | O_SYNC | O_DIRECT); //Open port
tcflush(fd,TCIOFLUSH);
for(n=0;n<iosz;n++){
int s=time(NULL); //Start timer for 5 seconds
while (time(NULL)-s < TIMEOUT && nread < 1){
inb[0]='A'; //Fill buffer with bad data
inb[1]='B';
inb[2]='C';
nread=read(fd,(char*)inb,1); //Read ONE byte
tcflush(fd,TCIOFLUSH);
if (nread < 0 || time(NULL)-s >= TIMEOUT){
close(fd); //Exit if read error or timeout
return -1;
}
}
printf("%x:%d ",inb[0] & 0xFF,nread); //Print byte as we receive it
}
close(fd); //program ends so close and exit
printf("\n"); //Print byte as we receive it
return 0;
}
First off, I'm no expert on this topic, just curious about it, so take this answer with a pinch of salt.
I don't know if what you're trying to do here (if I'm not looking at it the wrong way it seems to be to bypass the kernel and read directly from the port to userspace) was ever a possibility (you can find some examples, like this one but I could not find anything properly documented) but with recent kernels you should be getting an error running your code, but you're not catching it.
If you add these lines after declaring your port:
...
int fd=open("/dev/ttyS0", O_NOCTTY | O_RDONLY | O_SYNC | O_DIRECT );
if (fd == -1) {
fprintf(stderr, "Error %d opening SERIALPORT : %s\n", errno, strerror(errno));
return 1;
}
tcflush(fd,TCIOFLUSH);
....
When you try to run you'll get: Error 22 opening SERIALPORT : Invalid argument
In my humble and limited understanding, you should be able to get the same effect changing the settings on termios to raw, something like this should do:
struct termios t;
tcgetattr(fd, &t); /* get current port state */
cfmakeraw(&t); /* set port state to raw */
tcsetattr(fd, TCSAFLUSH, &t); /* set updated port state */
There are many good sources for termios, but the only place I could find taht also refers to O_DIRECT (for files) is this one.
I am developing a project where I have to read the holding registers data. I check everything using this http://www.freemodbus.com/ and it is working and get a proper response. While trying with the developed program I am not getting the proper response.
Software Response: 0x01 0x03 0x04 0x1a 0xa0 0x42 0x48 0xcd 0x9f
Arduino response: 0x01 0x04 0x83 0x43 0xff 0xff 0xff 0xff 0xff
note that in Arduino response there is no 0x03 after 0x01 I don't know why it is happening can anyone please help me with this.
please find attached Arduino code below.
static union
{
unsigned long a;
byte b[4];
float f;
}vr;
void readregister(unsigned int address)
{
byte rxbuf[]={0,0,0,0,0,0,0,0,0,0,0};
byte data[] = {0x01,0x03,0x00,0xab,0x00,0x02,0xb5,0xeb};
Serial3.flush();
for(int i=0;i<8;i++)
{
Serial3.write(data[i]);
}
delay(250);
while(Serial3.available()>0)
{
for(int v=0; v<=10;v++)
{
rxbuf[v]=Serial3.read();
Serial.println(rxbuf[v],HEX);
}
}
Serial3.flush();
vr.b[3]=rxbuf[3];
vr.b[2]=rxbuf[2];
vr.b[1]=rxbuf[5];
vr.b[0]=rxbuf[4];
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial3.begin(9600,SERIAL_8E1);
}
void loop() {
// put your main code here, to run repeatedly:
readregister(99);
Serial.print("\n");
delay(3000);
}
Yes, I think you're getting an Error code 4 back from the device. See http://www.simplymodbus.ca/exceptions.htm
On this line, where you prep your request:
byte data[] = {0x01,0x03,0x00,0xab,0x00,0x02,0xb5,0xeb};
You're saying, device 1, function 3, 0x00 is space, then register address 0xAB. Not sure about the extra zero then, then length 2, and then CRC. So I vote that either the CRC is wrong, or the extra zero is wrong.
Also, note that modbus fails on ocassion for no reason, even when you do everything right, so retry after a few seconds and see what happens
I'm trying to use USART 0 on the SAM4SD16C processor.
I got some help to start, from this website : SAM4S Xplained USART
Here's the code I'm using (which compiles):
#define COM_Handler USART0_Handler
#define USART_SERIAL USART0
#define USART_SERIAL_ID ID_USART0
#define USART_SERIAL_BAUDRATE 115200
#define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT US_MR_NBSTOP_1_BIT
#define PINS_USART0_PIO PIOA
#define PINS_USART0_ID ID_USART0
#define PINS_USART0_TYPE PIO_PERIPH_A
#define PINS_USART0_ATTR PIO_DEFAULT
#define PINS_USART0_MASK (PIO_PA5A_RXD0| PIO_PA6A_TXD0)
uint32_t received_byte;
uint32_t dw_status ;
int main(void)
{
sysclk_init();
board_init();
sysclk_enable_peripheral_clock(USART_SERIAL_ID);
pio_configure(PINS_USART0_PIO, PINS_USART0_TYPE, PINS_USART0_MASK, PINS_USART0_ATTR);
const sam_usart_opt_t usart_console_settings = {
USART_SERIAL_BAUDRATE,
USART_SERIAL_CHAR_LENGTH,
USART_SERIAL_PARITY,
USART_SERIAL_STOP_BIT,
US_MR_CHMODE_NORMAL
};
usart_init_rs485(USART_SERIAL, &usart_console_settings, sysclk_get_peripheral_hz());
usart_enable_tx(USART_SERIAL);
usart_enable_rx(USART_SERIAL);
usart_enable_interrupt(USART_SERIAL, US_IER_RXRDY);
NVIC_EnableIRQ(USART0_IRQn);
while (1)
{
while(US_CSR_TXRDY != 0 );
do
{
usart_write(USART_SERIAL, 1);
}while(US_CSR_RXRDY==0);
/*dw_status = usart_get_status(USART_SERIAL);
if(dw_status & US_CSR_RXRDY){
usart_read(USART_SERIAL, &received_byte);
}*/
}
}
When I debug the program, it stays in the while(US_CSR_TXRDY != 0) loop...
I wrote this line because I've seen somewhere in asf library that I should check if TX is ready to send before sending anything..
Once this will be resolved, I'll try to receive on the same board what I'm transmitting by connecting RX and TX together.
I only begin working on this processor and I'm not pretty familiar with it...
Thank you for your help
US_CSR_TXRDY is a constant, so while(US_CSR_TXRDY != 0 ) is a infinite loop. I think it should be while( (USART0->US_CSR & US_CSR_TXRDY) != 0 ).
I've been reading a lot of questions and answers regarding nrf24l01 and Arduinos while I was trying to figure out my problem. I'm fairly sure no one had this issue yet but I might be wrong. Here is my problem:
If I upload the sending code to the UNO and the receiving code to the NANO I keep getting errors. And nothing gets transmitted. However, if I do the opposite and I upload the sending code to the NANO and the receiving code to the UNO, everything is fine... I've been scratching my head for a couple days without any idea and I would like to get input from other people because I ran out of ideas...
I tried different nrf24l01 modules (I got about 20) to see if one was fried. Still the same thing. Tried changing to different pins... still the same thing. Changed the code to get it simpler and simpler to narrow down.. still the same thing. Maybe it requires more power to scan than send a package and the 3.3v from the Nano is not enough? I highly doubt..
I'm really curious if you guys can figure this one out. I think I provided a lot of info. If you need more feel free to ask.
Here is my set-up:
Arduino Nano:
And here is the log from the printDetails() function:
STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xb01dfacece 0xb01dfacece
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xb01dfacece
RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x73
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX
Arduino Uno:
STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xb01dfacece 0xb01dfacece
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xb01dfacece
RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x73
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX
Here is the Receiver code:
#include <SPI.h>
#include "RF24.h"
#include "nRF24L01.h"
#include "printf.h"
RF24 myRadio (7,8);
const uint64_t pipe = 0xB01DFACECEL;
struct package
{
int id=0;
int code = 0;
char text[100] = "";
};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(115200);
printf_begin();
delay(1000);
myRadio.begin();
myRadio.setChannel(115);
myRadio.openReadingPipe(1,pipe);
myRadio.printDetails();
myRadio.startListening();
Serial.println("Set-Up Done");
delay(1000);
}
void loop() {
if(myRadio.available())
{
while(myRadio.available())
{
myRadio.read(&data, sizeof(data));
Serial.print("\nPackage");
Serial.println(data.id);
Serial.println(data.code);
Serial.println(data.text);
}
}
delay(500);
}
Here is the Sending code:
#include <SPI.h>
#include "RF24.h"
#include "nRF24L01.h"
#include "printf.h"
RF24 myRadio (7,8);
const uint64_t pipe = 0xB01DFACECEL;
struct package
{
int id=1;
int code = 2;
char text[100] = "text";
};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(9600);
printf_begin();
delay(1000);
myRadio.begin();
myRadio.setChannel(115);
myRadio.openWritingPipe(pipe);
myRadio.setRetries(15,15);
myRadio.printDetails();
myRadio.stopListening();
Serial.println("Set-Up Done");
delay(1000);
}
void loop() {
if(!myRadio.write(&data, sizeof(data)))
{
Serial.println("error!!");
myRadio.printDetails();
}
Serial.print("\nPackage");
Serial.println(data.id);
Serial.println(data.code);
Serial.println(data.text);
data.id += 1;
data.code += 1;
delay(3000);
}
Try putting a 10uf capacitor across the +3.3v and gnd of each of the nRF24L01 module.
These modules need a lot of sending power which the capacitor helps with. I hope it works for you.
Try putting a 10uf capacitor across the +3.3v and gnd of each of the nRF24L01 module.
If that dosen't work, run the nrf24l01 off its own supply, 3.3v but have seen these peak at 1A! sending bursts. and the poor Nano's 3.3V comes from the FT232 chip, only 50mA is available.
I was wondering if there is a way to copy a packet using iptables/netfilter, change it and deliver both to the application.
Basically, I want to capture a packet from a flow and redirect it to some queue, then I want to copy it, issue the verdict for it(I know how to do this part in C),then I need to change something in the copied version, AND issue the verdict for that "modified" packet too.
Basically I want the app to receive both the unmodified and the modified version.
Is this possible?
Thanks in advance for any help.
Your mission can be achieved with libipq library. The tutorial in following like focus on copying & modifying a packet in userspace.
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.205.2605&rep=rep1&type=pdf
You need to know C to work on it. Alternatively "Scapy" - a python based packet maipulation tool can be used.
#include <linux/netfilter.h>
#include <libipq.h>
/*
* Used to open packet ; Insert a iptables rule to get packet here
* iptables -I 1 [INPUT|OUTPUT|FORWARD] <packet header match> -j QUEUE
*/
#include <linux/netfilter.h>
#include <libipq.h>
#include <stdio.h>
#define BUFSIZE 2048
static void die(struct ipq_handle *h)
{
ipq_destroy_handle(h);
exit(1);
}
int main(int argc, char **argv)
{
int status;
unsigned char buf[BUFSIZE];
struct ipq_handle *h;
h = ipq_create_handle(0, NFPROTO_IPV4);
if (!h)
die(h);
status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
if (status < 0)
die(h);
do{
status = ipq_read(h, buf, BUFSIZE, 0);
if (status < 0)
die(h);
if (ipq_message_type(buf) == IPQM_PACKET){
ipq_packet_msg_t *m = ipq_get_packet(buf);
status = ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL);
}
} while (1);
ipq_destroy_handle(h);
return 0;
}