Saturday, March 16, 2013

H.264 CODEC











In some streams,
for SPS there can be 00 00 00 01 67
for PPS there can be 00 00 00 01 68










AV Sync Logic





AV Sync Logic in Android Multimedia Framework

Audio Playback is real time and depends on rate of Sampling frequency. Audio is being used as master or reference for the Audio Video Synchronization logic. AV Sync logic is managed by Awesomeplayer method onVideoEvent. Where , there is drop in video frame - if video is decoded late than a threshold delay from audio. There is resend of video event to read new decoded frame , if the video decoded in advance of audio threshold. Otherwise if the video decoding time lies in audio threshold then render the video frame through Awesomeplayer Renderer. 

AwesomePlayer class is using TimeSource class to get actual rendering time.If there is no audio track present then TimeSource API is using system clock (GetSystemTimeUs).If audio track is available then TimeSource API is implemented by audioPlayer. Audioplayer will provide audio timestamp (latency data) captured from audioFlinger/AudioHAL.

AudioPlayer::AudioSinkCallback() {
…….
                       mInputBuffer->meta_data()->findInt64(kKeyTime, &mPositionTimeMediaUs));
……….
}

int64_t AudioPlayer::getMediaTimeUs() {
……..
          int64_t realTimeOffset = -mLatencyUs + (mNumFramesPlayed * 1000000) / mSampleRate -    mPositionTimeRealUs;
           if (realTimeOffset < 0) {
               realTimeOffset = 0;
              }
           return mPositionTimeMediaUs + realTimeOffset;
}

status_t AwesomePlayer::getPosition(int64_t *positionUs) {
                                        if (mSeeking != NO_SEEK) {
                                        *positionUs = mSeekTimeUs;
                                       } else if (mVideoSource != NULL && (mAudioPlayer == NULL || !(mFlags & VIDEO_AT_EOS))) {
                                       *positionUs = mVideoTimeUs;
                                       } else if (mAudioPlayer != NULL) {
                                        *positionUs = mAudioPlayer->getMediaTimeUs();
                                       } else {
                                          *positionUs = 0;
                                      }
}
 

status_t AwesomePlayer::play_l() {
……………………..
if (mAudioPlayer == NULL) {
       if (mAudioSink != NULL) {
            mAudioPlayer = new AudioPlayer(mAudioSink, this);
            mAudioPlayer->setSource(mAudioSource);
            mTimeSource = mAudioPlayer;
       }
   }
}

void AwesomePlayer::onVideoEvent() {
         status_t err = mVideoSource->read(&mVideoBuffer, &options);
         mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs);
         TimeSource *ts = ((mFlags & AUDIO_AT_EOS) || !(mFlags & AUDIOPLAYER_STARTED)) ? &mSystemTimeSource : mTimeSource;
         int64_t realTimeUs, mediaTimeUs;
         if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL &&                    mAudioPlayer->getMediaTimeMapping(&realTimeUs,
&mediaTimeUs)) {
          mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
          }
         int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
         int64_t latenessUs = nowUs - timeUs;
         if (latenessUs > 40000) { // We're more than 40ms late.
             mVideoBuffer->release(); mVideoBuffer = NULL;
             postVideoEvent_l(); 
             return; 
         }
         if (latenessUs < -10000) { // We're more than 10ms early.
             postVideoEvent_l(10000); return;
         }
         if (mVideoRenderer != NULL)
          mVideoRenderer->render(mVideoBuffer);
}

if (mTimeSource == NULL && mAudioPlayer == NULL) {
     mTimeSource = &mSystemTimeSource;
}
int64_t SystemTimeSource::getRealTimeUs() {
       return GetSystemTimeUs() - mStartTimeUs;
}

int64_t SystemTimeSource::GetSystemTimeUs() {
     struct timeval tv;
     gettimeofday(&tv, NULL);
      return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}

Wednesday, March 13, 2013

Real Interview Questions set 2

           

Programming :

1. Write a program to allocate 2-D memory.
Ans:
Allocating 2D memory can be taken as two dimensional array , where row can be allocated first then column using double pointer.

int **p;
p = malloc( rows * sizeof(int*));

for(int i = 0; i{
*(p+i) = malloc(column * sizeof(int));
}

One can also assign some character pattern to this two dimensional memory.

2. Write a program to draw circle with 1 at circumference in a NxN matrix = {0}.
Ans:
1. Divide the entire Matrix in four equal part.
2. In first quadrant , Arc can be drawn and replicate for other 3 quadrant.
3. To draw an Arc by 1 over the 1st Quad Matrix of N/2 x N/2, Assign the value 1 to all the point which should lie on circumference of the circle.
4. Centre of Circle = {N/2,N/2}
5. Radius = {N/2}
6. In 1st quad starting from first row i.e.  x = 0 , the y co-ordinates to lie on circle circumference should come as per formula Sqr(x-a) + Sqr(y-b) = Sqr(radius)
so y would be    Sqroot{|Sqr(N/2) - Sqr (|x - N/2|)|} for x vary from 0 to N/2 to form an Arc.
7. Assign the array value to 1 for obtained (x,y).

3. Write a program to implement itoa().
4. Write a multi-threaded parallel program to solve the equation :
 10a + 10b + 10c + 10d.
Assuming there are four cores.
5. What is the O/P of

a) int main() {
           printf("%x", main());
           return 0;
}

b) int main() {
int a = 11;
char *str = "nvidia";
char arr[10] = "talk";
strcpy(str, arr);
}

c) if(malloc(0) != NULL) {
      printf("good");
     } else {
       printf("bad");
     }

d) int a = -20;
    unsigned int b = 6;
    if(a>b)
      printf("unsigned");
     else
      printf("signed");
e)  int main() {
      fork();
      fork();
      fork();
      printf("hello fork");
     }
and Explain.

6. Explain the high level flow of Android Video playback.
7. What do you mean by Data tunneling.
8. What are the different System Signals
    SIGABRT - Abnormal termination
    SIGFPE - erroneous Arithmetic operation
    SIGILL  - illegal instruction.
    SIGINT - interactive attention signal
    SIGSEV - An invalid access to storage
   SIGTERM - termination request   etc
can refer
http://en.wikipedia.org/wiki/Unix_signal
9 . Which mp4 atom is responsible sync for I - frame.
10. Explain RTSP streaming.
11. Explain state transition of OMX Component.
12. Explain the AV Sync logic.
13. Try to explain the high level logic of Audio recording.
14. find out the average of two number efficiently
     a>>1 + b>>1
15. Lets say input is N = 1000 0000 0000 0000 and M = 10101
copy M into N from position i = 2 to j = 6th of N.
O/P should be N = 1000 0000 0101 0100
16. Lets say there is an integer a , extract the position of different value c from the set (0,1,2,3)
as bits in a and save the position value in b. Write a macro for this
c = Extract_bit(a,b);

#define Extract_bit(a,b)   .................................
17. What are the video artifacts have you observed. Can you explain the reason behind them.
18. What are the Green frames. What does it signifies. Why is it green by the way?
19. What does the Grey frames indicate.
20. Write a program to calculate the amount of loan repayment year , where you have taken loan of amount P with rate r% annual. You do payment of amount A every year.

puzzles:

1. In a Cricket Match , there are two balls remained , and last pair is on the pitch with seven runs remained to win. In what there will be win for batting team.

2.  There is long number having total digit count to be 1996 like
  abcdef ........1996  each two digit pair in the number is either divisible by 17 or 23.
find out the last digit of this number.

3. Number of zeroes in 100!

Sunday, March 10, 2013

Virtual Memory and Paging



Memory layout for a Program:


Real Interview Questions set 1



C/C++/OS concept


Q1.  Brief me about yourself , skill and experience?
Q2 . Brief me about your experience related work?
Q3 . Why do you want to join Qualcomm?
Q4.  How much do you rate yourself in C,C++ , OS and Multimedia out of 10?
Q5. What are the storage classes? Tell the scope and life of each one?
Q6. What is the difference between static and global variable?
Q7. What is the use of volatile variable ? Give one example.
Q8.  What is the difference between Macro , Inline function and Template? Explain the advantages and disadvantages.
Q9.  Are you aware of RAM , ROM and Cache memory? Brief.
Q10. What is Segmentation fault. what are different segmentation fault?
Q11 . What do you mean by Paging . Why do we need it?
Q12 . What is fragmentation and How can you prevent this?
Q13 . What is virtual memory concept? Why do we need it.
Q14. What is difference between struct in C and C++?
Q15. What is function pointer? When do we use it? Give one example.
Q16. What is the difference between process and thread?
Q17. What is context switching? How does Scheduling happens?
Q18. What is Critical Section? How will you protect it?
Q19. What is difference between Mutex, Semaphore and Binary Semaphore?
Q20. What are different IPC mechanism? Explain each in short.
Q21. What do you mean shared memory among process? How will you implement it.

Q22. What is virtual function. How does pure virtual function different from it.
Q23 What is Abstract Class? How can we access the members of it.
Q24 What is friend function.
Q25. If A is base class and D is derived class . can we access the member of D using A's object .yes or no And how?
Q26. Explain ISR .Top/bottom halves mechanism
Q27. Difference between softirq , tasklet and workqueues.
Q28. What is Event Callbacks . Give one example.
Q29. What is a buffer . how does it gets allocation and maped to physical mem.
Q30. Explain System calls , how does it go from user space to kernel space.
Q31. What is Spin lock and how does it differ from mutex & semaphore.
Q32. Comparing int and unsignedint. what will be typecast.
Q33. What is Q-format. Where do we use it?
Q34. What is diff between floating and fixed point reference?
Q35. What is Static variable and function. Where do we use this. Explain Factory object model.

Programming Questions:
Q1.  Implement malloc of your own and explain in general how does it work.
Q2.  Implement memcpy ,memmove and memset.
Q3.  Reverse a given String in place
Q4.  Reverse a given single linked list and double linked list in place.
Q5.  Write a small program to find the endiannes of system. 
Q6.  Write a program to find palindrome word in a given string lets say My name is Nitin.
Q7.  Write a program to transpose a matrix 4x4.
Q8.  Write a program for matrix multiplication.
Q9 . Write a program to find the intersection of two linked list.
Q10. Implement Stack and Queue.
Q11. Write a program for Run Length Encoding in a given string.
Q12. Write a program to find a substring in a given string.
Q13. Write a program to enter the element in a sorted linked list.
Q14. Write a program to find loop in a linked list.
Q15. Write a program to replace s in given string by m  and m by s - "sy nase im sohit mingh"
Q16.  Determine whether linked list is circular or not.
Q17. Find the mid of linked list.
Q18. Give the ISR method signature and Write a small interrupt handler?
Q19. what does malloc(0) returns . is it NULL?
Q20. Write a multithreading program to print even and odd numbers till 100 using  two threads T1 / T2.
Where T1 will print  1 3 5 7 .... and T2 will print 2 4 6 ...

Bitwise questions:

Q1.  Swap Bit at mth and nth position from Right.
Q2. Toggle Bit at 6th position from Right.
Q3. Count number of 1's in given integer.
Q4. Swap bits from 0th to 2nd positon to 5th to 7th.

puzzles :

1. There is a long string of 15cm size . You need to cut them in minimum way and keep in such a way that you can generate any length string till 15 cm by tying them.

Use binary funda 1 2 4 8

- | -- | ---- | --------  , so there are only 3 cuts and you can generate any length string till 15 cm size.

2.  There are three boxes wrongly labeled as Apple, Orange and  Apple&Orange.You need to take out of one item from any of the one box and then tell the box label correctly in once chance.

[A]       [O]    [A&O] wrongly labeled

take out  one item from [A&O] box , if its apple then its Apple box , and [O] is wrongly labeled so it would be A&O , [A] would be Orange box . or similarly if its Orange out of the A&O box.

3. There are 10 cartoons of Cigar of 10 cigars in each of 10 grams each. In one of the cartoon 10 Cigars are packed of 11 gm each by mistake.  Find out the cartoon having 11 gm Cigars by measuring once.

place cartoon in a row , from left : pick one cigar from 1st cartoon  two from 2nd , three from 3rd and so on till 10 from 10th last one.

Weight them all , Ideally each Cigar weighs 10gm , so total weigh should be 10+20+30+40+50+...+100 =  550 gms

Now , If there is 11 gm Cigar in 1st cartoon from left then the above sum would have come as 551
if its in 2nd Cartoon from left then sum would have come as 552
3rd .....553
4th .....554
5th......555   .................if its in 10th then 560 gms


 ALL THE BEST
----------------------------------------------------- Thank You-------------------------------------------------------

Sunday, February 10, 2013

Strings Questions






 Problem 1 : Write a program to Run Length Encoding of a given Input String. 
    Sample Input : "aaaadddddaaaaaaauuuuiiiii"
    Sample Output: "a4d5a7u4i5"

Solution : 

#include
#include

char* runLenEnc(char *str) {
      int i,j=0,len = 0,count = 1;
      char *str1;
      for (i =0; str[i] != '\0' ; i++) len++;
      printf("length = %d\n" , len);
      str1 = (char*) malloc(2*len*sizeof(char));
      for ( i =0 ; str[i]!= '\0' ; i++) {
           str1[j++] = str[i];
           while (str[i] == str[i+1]) {
             count++;
             i++;
           }
           if (count > 9) {
               int temp, numChar =0;
               temp = count;
               while(temp) {
                     numChar++;
                     temp /=10;
               }
               printf("numChar = %d, str[%d] = %c\n", numChar, j-1, str[j-1]);
               j+=(numChar-1);
               while (count) {
                     str1[j--] = count%10 + '0';
                     count /=10;
               }
               j+=(numChar+1);
           } else {
               str1[j++] = count + '0';
           }
           count = 1;
      }
   
      str1[j] = '\0';
      return str1;
}
          
int main() {
   char strIn[] = "aaaadddddaaaaaaauuuuiiiii";
   char *strOut;
   strOut = runLenEnc (strIn);
   printf("Run Length Encoding O/P : %s", strOut);
   getch();
}  



/* write a program to reverse the string in place */ 

 #include<iostream>
 #include<string.h>
 using namespace std;
 void reverse(string& str) {
    int i,j;
    char temp;
    int len =0;
    for(i=0; str[i] !='\0';i++)
        len++;
    for(i=0,j=len-1;i<j;i++, j--){
         temp = str[i];
         str[i] = str[j];
         str[j] = temp;
     }
 }
 int main() {
     string str;
     cout<<"Enter the string: ";
     if(!getline(cin, str))
         getline(cin,str);
     reverse(str);
     cout<<str<<endl;
     return 0;
 }


/* recursive way to print the string in reverse order */

------
----
--

Monday, November 12, 2012

memcpy() and memmove()




                                                    memcpy() and memmove()


memcpy does a mindless copy, regardless of whether there are shared bytes between the source and destination: 

 void *memcpy (void *dst, const void *src, size_t n)

 { 

   char *a = dst;

   const char *b = src; 

   while (n--)


    *a++ = *b++;



       }

   return dst;


 }



memmove does backward copy  and takes care of overwritten value of shared bytes:


void *memmove(void *dst, const void *src, size_t n) 

{ 

 char *a = dst; 

 const char *b = src;



 if (a <= b || b >= (a + n)) {

/* No overlap, use memcpy logic or can use memcpy itself(copy forward) */ 

 while (n--) 

 *a++ = *b++;


}


else { 

 /* Overlap! Copy backward to fix */ 

 a = a + n - 1; 

 b = b + n - 1;




while (n--) 

 *a-- = *b--; 

 } 

 return dst;