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)
{
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;
/* 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_start = 0;
+ if ((data & 0xE0FF) == 0x0009) /* H.264 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 & 0xFF) == 0x00); /* H.264 UAD or MPEG2 start code */
if (is_frame)
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;
else if (direction == +1)
direction = 0;
}
+ off_t iframe_start = offset;
off_t start = offset;
#if 0
/* 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;
eDebug("reached eof (while looking for end of iframe)");
return -1;
}
-// eDebug("%08llx@%llx (next)", data, offset);
+// eDebug("%08llx@%llu (next frame)", data, offset);
} while (((data & 0xFF) != 9) && ((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);
#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) {
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;
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)
{
offset = new_offset;
len = new_len;
nr_frames += abs(dir);
- }
+ }
else if (first) {
first = 0;
offset = new_offset;
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);