[pvrparse, tstools] fix for 16x playback H.265.
[vuplus_dvbapp] / lib / dvb / tstools.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 6cd855c..379df9b
@@ -648,11 +648,12 @@ int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
        return -1;
 }
 
-int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int frame_types)
+int eDVBTSTools::findFrame(off_t &_iframe_offset, off_t &_new_offset, size_t &len, int &direction, int frame_types)
 {
-       off_t offset = _offset;
+       off_t offset = _iframe_offset;
        int nr_frames = 0;
-//     eDebug("trying to find iFrame at %llx", offset);
+       int is_mpeg = 0;
+//     eDebug("trying to find iFrame at %lld", offset);
 
        if (!m_use_streaminfo)
        {
@@ -668,7 +669,13 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
 
        while (1)
        {
-               if (m_streaminfo.getStructureEntry(offset, data, (direction == 0) ? 1 : 0))
+               int res = -1;
+               if(direction<0)
+                       res = m_streaminfo.getStructureEntry_prev(offset, data);
+               else
+                       res = m_streaminfo.getStructureEntry_next(offset, data);
+
+               if (res)
                {
                        eDebug("getting structure info for origin offset failed.");
                        return -1;
@@ -681,10 +688,17 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
                        /* data is usually the start code in the lower 8 bit, and the next byte <<8. we extract the picture type from there */
                        /* we know that we aren't recording startcode 0x09 for mpeg2, so this is safe */
                        /* TODO: check frame_types */
-               int is_start = (data & 0xE0FF) == 0x0009; /* H.264 NAL unit access delimiter with I-frame*/
-               is_start |= (data & 0x3800FF) == 0x080000; /* MPEG2 picture start code with I-frame */
-               
-               int is_frame = ((data & 0xFF) == 0x0009) || ((data & 0xFF) == 0x00); /* H.264 UAD or MPEG2 start code */
+               int is_start = 0;
+               if ((data & 0xE0FF) == 0x0009) /* H.264 NAL unit access delimiter with I-frame*/
+                       is_start =1;
+               else if ((data & 0xE07E) == 0x0046) /* H.265 NAL unit access delimiter with I-frame*/
+                       is_start = 1;
+               else if((data & 0x3800FF) == 0x080000) /* MPEG2 picture start code with I-frame */
+               {
+                       is_start =1;
+                       is_mpeg =1;
+               }
+               int is_frame = ((data & 0xFF) == 0x0009) || ((data & 0x7E) == 0x0046) || ((data & 0xFF) == 0x00); /* H.264 UAD or H.265 UAD or MPEG2 start code */
                
                if (is_frame)
                {
@@ -693,7 +707,7 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
                        else
                                ++nr_frames;
                }
-//             eDebug("%08llx@%llx -> %d, %d", data, offset, is_start, nr_frames);
+//             eDebug("%08llx@%llu -> %d, %d (I-frame)", data, offset, is_start, nr_frames);
                if (is_start)
                        break;
 
@@ -702,6 +716,7 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
                else if (direction == +1)
                        direction = 0;
        }
+       off_t iframe_start = offset;
        off_t start = offset;
 
 #if 0
@@ -723,7 +738,7 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
 
                        /* let's find the next frame after the given offset */
        do {
-               if (m_streaminfo.getStructureEntry(offset, data, 1))
+               if (m_streaminfo.getStructureEntry_next(offset, data))
                {
                        eDebug("get next failed");
                        return -1;
@@ -733,9 +748,33 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
                        eDebug("reached eof (while looking for end of iframe)");
                        return -1;
                }
-//             eDebug("%08llx@%llx (next)", data, offset);
-       } while (((data & 0xFF) != 9) && ((data & 0xFF) != 0x00)); /* next frame */
+//             eDebug("%08llx@%llu (next frame)", data, offset);
+       } while (((data & 0xFF) != 9) && ((data & 0x7E) != 0x46) && ((data & 0xFF) != 0x00)); /* next frame */
 
+       if (is_mpeg) //MPEG2 picture start code with I-frame
+       {
+               off_t sequence_offset = start;
+               unsigned long long sequence_data;
+               int is_sequence =0;
+               int frame_length = (nr_frames<0) ? -nr_frames: nr_frames;
+               while(frame_length)
+               {
+//                     --start;
+                       if (m_streaminfo.getStructureEntry_prev(sequence_offset, sequence_data))
+                       {
+                               eDebug("get previous failed");
+                               return -1;
+                       }
+                       if ((sequence_data & 0xFF) == 0xB3) /* sequence start or previous frame */
+                               is_sequence = 1;
+                       if(((sequence_data & 0xFF) == 0x0009) || ((sequence_data & 0xFF) == 0x00)) /* H.264 UAD or MPEG2 start code */
+                               --frame_length;
+//                     eDebug("[findFrame] %08llx@%llu -> %d, %d (sequence)", sequence_data, sequence_offset, is_sequence, frame_length);
+                       if(is_sequence) /* get sequence frame */
+                               break;
+               }
+               start = sequence_offset;
+       }
 #if 0
                        /* align to TS pkt start */
        start = start - (start % 188);
@@ -743,21 +782,23 @@ int eDVBTSTools::findFrame(off_t &_offset, size_t &len, int &direction, int fram
 #endif
 
        len = offset - start;
-       _offset = start;
+       _new_offset = start;
+       _iframe_offset = iframe_start;
        direction = nr_frames;
-//     eDebug("result: offset=%llx, len: %ld", offset, (int)len);
+//     eDebug("result: start=%lld, iframe= %lld, next=%lld, len: %ld",start, iframe_start ,offset, (long int)len);
        return 0;
 }
 
 int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int frame_types)
 {
        int nr_frames, direction;
-//     eDebug("trying to move %d frames at %llx", distance, offset);
+//     eDebug("trying to move %d frames at %llu", distance, offset);
        
        frame_types = frametypeI; /* TODO: intelligent "allow IP frames when not crossing an I-Frame */
 
        off_t new_offset = offset;
        size_t new_len = len;
+       off_t iframe_offset = new_offset;
        int first = 1;
 
        if (distance > 0) {
@@ -771,7 +812,7 @@ int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int
        while (distance > 0)
        {
                int dir = direction;
-               if (findFrame(new_offset, new_len, dir, frame_types))
+               if (findFrame(iframe_offset, new_offset, new_len, dir, frame_types))
                {
 //                     eDebug("findFrame failed!\n");
                        return -1;
@@ -779,7 +820,7 @@ int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int
                
                distance -= abs(dir);
                
-//             eDebug("we moved %d, %d to go frames (now at %llx)", dir, distance, new_offset);
+//             eDebug("we moved %d, %d to go frames (now at %llu)", dir, distance, new_offset);
 
                if (distance >= 0 || direction == 0)
                {
@@ -787,7 +828,7 @@ int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int
                        offset = new_offset;
                        len = new_len;
                        nr_frames += abs(dir);
-               } 
+               }
                else if (first) {
                        first = 0;
                        offset = new_offset;
@@ -795,7 +836,6 @@ int eDVBTSTools::findNextPicture(off_t &offset, size_t &len, int &distance, int
                        nr_frames += abs(dir) + distance; // never jump forward during rewind
                }
        }
-
        distance = (direction < 0) ? -nr_frames : nr_frames;
 //     eDebug("in total, we moved %d frames", nr_frames);