mirror of
https://github.com/VDR4Arch/vdr.git
synced 2023-10-10 13:36:52 +02:00
Fixed detecting frames in case the Picture Start Code or Access Unit Delimiter extends over TS packet boundaries (cont'd)
This commit is contained in:
parent
098d21117e
commit
72d342ce02
3
HISTORY
3
HISTORY
@ -6634,6 +6634,9 @@ Video Disk Recorder Revision History
|
|||||||
channel is being received with.
|
channel is being received with.
|
||||||
- Fixed detecting frames in case the Picture Start Code or Access Unit Delimiter
|
- Fixed detecting frames in case the Picture Start Code or Access Unit Delimiter
|
||||||
extends over TS packet boundaries (reported by Johan Andersson).
|
extends over TS packet boundaries (reported by Johan Andersson).
|
||||||
|
In order to fix this, the semantics of cFrameDetector had to be changed a little.
|
||||||
|
See cRecorder::Action() and cIndexFileGenerator::Action() on how to use the new
|
||||||
|
cFrameDetector::NewPayload() function.
|
||||||
- The frame detector now only starts collecting PTS values after it has seen the
|
- The frame detector now only starts collecting PTS values after it has seen the
|
||||||
first I-frame, otherwise it might get MaxPtsValues values and stop analyzing
|
first I-frame, otherwise it might get MaxPtsValues values and stop analyzing
|
||||||
even though the incoming data is still garbage (reported by Derek Kelly).
|
even though the incoming data is still garbage (reported by Derek Kelly).
|
||||||
|
12
recorder.c
12
recorder.c
@ -4,7 +4,7 @@
|
|||||||
* See the main source file 'vdr.c' for copyright information and
|
* See the main source file 'vdr.c' for copyright information and
|
||||||
* how to reach the author.
|
* how to reach the author.
|
||||||
*
|
*
|
||||||
* $Id: recorder.c 2.9 2010/12/27 11:35:46 kls Exp $
|
* $Id: recorder.c 2.10 2011/06/12 13:35:20 kls Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "recorder.h"
|
#include "recorder.h"
|
||||||
@ -31,7 +31,7 @@ cRecorder::cRecorder(const char *FileName, const cChannel *Channel, int Priority
|
|||||||
|
|
||||||
SpinUpDisk(FileName);
|
SpinUpDisk(FileName);
|
||||||
|
|
||||||
ringBuffer = new cRingBufferLinear(RECORDERBUFSIZE, MIN_TS_PACKETS_FOR_FRAME_DETECTOR * TS_SIZE, true, "Recorder");
|
ringBuffer = new cRingBufferLinear(RECORDERBUFSIZE, TS_SIZE, true, "Recorder");
|
||||||
ringBuffer->SetTimeouts(0, 100);
|
ringBuffer->SetTimeouts(0, 100);
|
||||||
|
|
||||||
int Pid = Channel->Vpid();
|
int Pid = Channel->Vpid();
|
||||||
@ -119,6 +119,8 @@ void cRecorder::Action(void)
|
|||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
bool InfoWritten = false;
|
bool InfoWritten = false;
|
||||||
bool FirstIframeSeen = false;
|
bool FirstIframeSeen = false;
|
||||||
|
int FileNumber = 0;
|
||||||
|
off_t FrameOffset = -1;
|
||||||
while (Running()) {
|
while (Running()) {
|
||||||
int r;
|
int r;
|
||||||
uchar *b = ringBuffer->Get(r);
|
uchar *b = ringBuffer->Get(r);
|
||||||
@ -139,12 +141,16 @@ void cRecorder::Action(void)
|
|||||||
}
|
}
|
||||||
InfoWritten = true;
|
InfoWritten = true;
|
||||||
}
|
}
|
||||||
|
if (frameDetector->NewPayload()) {
|
||||||
|
FileNumber = fileName->Number();
|
||||||
|
FrameOffset = fileSize;
|
||||||
|
}
|
||||||
if (FirstIframeSeen || frameDetector->IndependentFrame()) {
|
if (FirstIframeSeen || frameDetector->IndependentFrame()) {
|
||||||
FirstIframeSeen = true; // start recording with the first I-frame
|
FirstIframeSeen = true; // start recording with the first I-frame
|
||||||
if (!NextFile())
|
if (!NextFile())
|
||||||
break;
|
break;
|
||||||
if (index && frameDetector->NewFrame())
|
if (index && frameDetector->NewFrame())
|
||||||
index->Write(frameDetector->IndependentFrame(), fileName->Number(), fileSize);
|
index->Write(frameDetector->IndependentFrame(), FileNumber, FrameOffset);
|
||||||
if (frameDetector->IndependentFrame()) {
|
if (frameDetector->IndependentFrame()) {
|
||||||
recordFile->Write(patPmtGenerator.GetPat(), TS_SIZE);
|
recordFile->Write(patPmtGenerator.GetPat(), TS_SIZE);
|
||||||
fileSize += TS_SIZE;
|
fileSize += TS_SIZE;
|
||||||
|
15
recording.c
15
recording.c
@ -4,7 +4,7 @@
|
|||||||
* See the main source file 'vdr.c' for copyright information and
|
* See the main source file 'vdr.c' for copyright information and
|
||||||
* how to reach the author.
|
* how to reach the author.
|
||||||
*
|
*
|
||||||
* $Id: recording.c 2.30 2011/04/17 13:53:11 kls Exp $
|
* $Id: recording.c 2.31 2011/06/12 13:04:28 kls Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "recording.h"
|
#include "recording.h"
|
||||||
@ -1403,11 +1403,12 @@ void cIndexFileGenerator::Action(void)
|
|||||||
bool Rewind = false;
|
bool Rewind = false;
|
||||||
cFileName FileName(recordingName, false);
|
cFileName FileName(recordingName, false);
|
||||||
cUnbufferedFile *ReplayFile = FileName.Open();
|
cUnbufferedFile *ReplayFile = FileName.Open();
|
||||||
cRingBufferLinear Buffer(IFG_BUFFER_SIZE, MIN_TS_PACKETS_FOR_FRAME_DETECTOR * TS_SIZE);
|
cRingBufferLinear Buffer(IFG_BUFFER_SIZE, TS_SIZE);
|
||||||
cPatPmtParser PatPmtParser;
|
cPatPmtParser PatPmtParser;
|
||||||
cFrameDetector FrameDetector;
|
cFrameDetector FrameDetector;
|
||||||
cIndexFile IndexFile(recordingName, true);
|
cIndexFile IndexFile(recordingName, true);
|
||||||
int BufferChunks = KILOBYTE(1); // no need to read a lot at the beginning when parsing PAT/PMT
|
int BufferChunks = KILOBYTE(1); // no need to read a lot at the beginning when parsing PAT/PMT
|
||||||
|
int FileNumber = 0;
|
||||||
off_t FileSize = 0;
|
off_t FileSize = 0;
|
||||||
off_t FrameOffset = -1;
|
off_t FrameOffset = -1;
|
||||||
Skins.QueueMessage(mtInfo, tr("Regenerating index file"));
|
Skins.QueueMessage(mtInfo, tr("Regenerating index file"));
|
||||||
@ -1424,12 +1425,18 @@ void cIndexFileGenerator::Action(void)
|
|||||||
if (Data) {
|
if (Data) {
|
||||||
if (FrameDetector.Synced()) {
|
if (FrameDetector.Synced()) {
|
||||||
// Step 3 - generate the index:
|
// Step 3 - generate the index:
|
||||||
if (TsPid(Data) == PATPID)
|
if (FrameOffset < 0 && TsPid(Data) == PATPID) {
|
||||||
|
FileNumber = FileName.Number();
|
||||||
FrameOffset = FileSize; // the PAT/PMT is at the beginning of an I-frame
|
FrameOffset = FileSize; // the PAT/PMT is at the beginning of an I-frame
|
||||||
|
}
|
||||||
int Processed = FrameDetector.Analyze(Data, Length);
|
int Processed = FrameDetector.Analyze(Data, Length);
|
||||||
if (Processed > 0) {
|
if (Processed > 0) {
|
||||||
|
if (FrameDetector.NewPayload() && FrameOffset < 0) {
|
||||||
|
FileNumber = FileName.Number();
|
||||||
|
FrameOffset = FileSize;
|
||||||
|
}
|
||||||
if (FrameDetector.NewFrame()) {
|
if (FrameDetector.NewFrame()) {
|
||||||
IndexFile.Write(FrameDetector.IndependentFrame(), FileName.Number(), FrameOffset >= 0 ? FrameOffset : FileSize);
|
IndexFile.Write(FrameDetector.IndependentFrame(), FileNumber, FrameOffset);
|
||||||
FrameOffset = -1;
|
FrameOffset = -1;
|
||||||
}
|
}
|
||||||
FileSize += Processed;
|
FileSize += Processed;
|
||||||
|
71
remux.c
71
remux.c
@ -4,7 +4,7 @@
|
|||||||
* See the main source file 'vdr.c' for copyright information and
|
* See the main source file 'vdr.c' for copyright information and
|
||||||
* how to reach the author.
|
* how to reach the author.
|
||||||
*
|
*
|
||||||
* $Id: remux.c 2.55 2011/06/11 11:35:18 kls Exp $
|
* $Id: remux.c 2.56 2011/06/12 13:51:59 kls Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "remux.h"
|
#include "remux.h"
|
||||||
@ -781,7 +781,8 @@ cFrameDetector::cFrameDetector(int Pid, int Type)
|
|||||||
{
|
{
|
||||||
SetPid(Pid, Type);
|
SetPid(Pid, Type);
|
||||||
synced = false;
|
synced = false;
|
||||||
newFrame = independentFrame = false;
|
newPayload = newFrame = independentFrame = false;
|
||||||
|
frameTypeOffset = -1;
|
||||||
numPtsValues = 0;
|
numPtsValues = 0;
|
||||||
numFrames = 0;
|
numFrames = 0;
|
||||||
numIFrames = 0;
|
numIFrames = 0;
|
||||||
@ -808,7 +809,8 @@ void cFrameDetector::SetPid(int Pid, int Type)
|
|||||||
|
|
||||||
void cFrameDetector::Reset(void)
|
void cFrameDetector::Reset(void)
|
||||||
{
|
{
|
||||||
newFrame = independentFrame = false;
|
newPayload = newFrame = independentFrame = false;
|
||||||
|
frameTypeOffset = -1;
|
||||||
payloadUnitOfFrame = 0;
|
payloadUnitOfFrame = 0;
|
||||||
scanning = false;
|
scanning = false;
|
||||||
scanner = EMPTY_SCANNER;
|
scanner = EMPTY_SCANNER;
|
||||||
@ -816,9 +818,8 @@ void cFrameDetector::Reset(void)
|
|||||||
|
|
||||||
int cFrameDetector::Analyze(const uchar *Data, int Length)
|
int cFrameDetector::Analyze(const uchar *Data, int Length)
|
||||||
{
|
{
|
||||||
int SeenPayloadStart = false;
|
|
||||||
int Processed = 0;
|
int Processed = 0;
|
||||||
newFrame = independentFrame = false;
|
newPayload = newFrame = independentFrame = false;
|
||||||
while (Length >= TS_SIZE) {
|
while (Length >= TS_SIZE) {
|
||||||
if (Data[0] != TS_SYNC_BYTE) {
|
if (Data[0] != TS_SYNC_BYTE) {
|
||||||
int Skipped = 1;
|
int Skipped = 1;
|
||||||
@ -831,11 +832,8 @@ int cFrameDetector::Analyze(const uchar *Data, int Length)
|
|||||||
int Pid = TsPid(Data);
|
int Pid = TsPid(Data);
|
||||||
if (Pid == pid) {
|
if (Pid == pid) {
|
||||||
if (TsPayloadStart(Data)) {
|
if (TsPayloadStart(Data)) {
|
||||||
SeenPayloadStart = true;
|
|
||||||
if (synced && Processed)
|
if (synced && Processed)
|
||||||
return Processed;
|
return Processed; // flush everything before this new payload
|
||||||
if (Length < MIN_TS_PACKETS_FOR_FRAME_DETECTOR * TS_SIZE)
|
|
||||||
return Processed; // need more data, in case the frame type is not stored in the first TS packet
|
|
||||||
if (framesPerSecond <= 0.0) {
|
if (framesPerSecond <= 0.0) {
|
||||||
// frame rate unknown, so collect a sequence of PTS values:
|
// frame rate unknown, so collect a sequence of PTS values:
|
||||||
if (numPtsValues < MaxPtsValues && numIFrames < 2) { // collect a sequence containing at least two I-frames
|
if (numPtsValues < MaxPtsValues && numIFrames < 2) { // collect a sequence containing at least two I-frames
|
||||||
@ -900,35 +898,41 @@ int cFrameDetector::Analyze(const uchar *Data, int Length)
|
|||||||
if (scanning) {
|
if (scanning) {
|
||||||
int PayloadOffset = TsPayloadOffset(Data);
|
int PayloadOffset = TsPayloadOffset(Data);
|
||||||
if (TsPayloadStart(Data)) {
|
if (TsPayloadStart(Data)) {
|
||||||
|
if (synced && Processed)
|
||||||
|
return Processed; // flush everything before this new payload
|
||||||
|
newPayload = true;
|
||||||
|
scanner = EMPTY_SCANNER;
|
||||||
PayloadOffset += PesPayloadOffset(Data + PayloadOffset);
|
PayloadOffset += PesPayloadOffset(Data + PayloadOffset);
|
||||||
if (!framesPerPayloadUnit)
|
if (!framesPerPayloadUnit)
|
||||||
framesPerPayloadUnit = framesInPayloadUnit;
|
framesPerPayloadUnit = framesInPayloadUnit;
|
||||||
if (DebugFrames && !synced)
|
if (DebugFrames && !synced)
|
||||||
dbgframes("/");
|
dbgframes("/");
|
||||||
}
|
}
|
||||||
int PacketsForFrameDetector = MIN_TS_PACKETS_FOR_FRAME_DETECTOR;
|
|
||||||
for (int i = PayloadOffset; scanning && i < TS_SIZE; i++) {
|
for (int i = PayloadOffset; scanning && i < TS_SIZE; i++) {
|
||||||
scanner <<= 8;
|
if (frameTypeOffset < 0) {
|
||||||
scanner |= Data[i];
|
scanner <<= 8;
|
||||||
if (i == TS_SIZE - 1) { // it was the last byte in this TS packet
|
scanner |= Data[i];
|
||||||
if (SeenPayloadStart && --PacketsForFrameDetector > 0) {
|
|
||||||
Data += TS_SIZE;
|
|
||||||
Length -= TS_SIZE;
|
|
||||||
Processed += TS_SIZE;
|
|
||||||
if (Length < TS_SIZE)
|
|
||||||
return Processed;
|
|
||||||
i = TsPayloadOffset(Data) - 1; // one before the first payload byte of the next TS packet
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
frameTypeOffset += PayloadOffset;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 0x01: // MPEG 1 video
|
case 0x01: // MPEG 1 video
|
||||||
case 0x02: // MPEG 2 video
|
case 0x02: // MPEG 2 video
|
||||||
if (scanner == 0x00000100) { // Picture Start Code
|
if (scanner == 0x00000100) { // Picture Start Code
|
||||||
|
if (frameTypeOffset < 0) {
|
||||||
|
frameTypeOffset = i + 2;
|
||||||
|
if (frameTypeOffset >= TS_SIZE) { // the byte to check is in the next TS packet
|
||||||
|
frameTypeOffset -= TS_SIZE;
|
||||||
|
if (!synced)
|
||||||
|
dbgframes("%d>", frameTypeOffset);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
scanner = EMPTY_SCANNER;
|
scanner = EMPTY_SCANNER;
|
||||||
if (synced && !SeenPayloadStart && Processed)
|
|
||||||
return Processed; // flush everything before this new frame
|
|
||||||
newFrame = true;
|
newFrame = true;
|
||||||
independentFrame = ((Data[i + 2] >> 3) & 0x07) == 1; // I-Frame
|
uchar FrameType = (Data[frameTypeOffset] >> 3) & 0x07;
|
||||||
|
frameTypeOffset = -1;
|
||||||
|
independentFrame = FrameType == 1; // I-Frame
|
||||||
if (synced) {
|
if (synced) {
|
||||||
if (framesPerPayloadUnit <= 1)
|
if (framesPerPayloadUnit <= 1)
|
||||||
scanning = false;
|
scanning = false;
|
||||||
@ -939,7 +943,7 @@ int cFrameDetector::Analyze(const uchar *Data, int Length)
|
|||||||
numIFrames++;
|
numIFrames++;
|
||||||
if (numIFrames == 1)
|
if (numIFrames == 1)
|
||||||
numFrames++;
|
numFrames++;
|
||||||
dbgframes("%d ", (Data[i + 2] >> 3) & 0x07);
|
dbgframes("%u ", FrameType);
|
||||||
}
|
}
|
||||||
if (synced)
|
if (synced)
|
||||||
return Processed + TS_SIZE; // flag this new frame
|
return Processed + TS_SIZE; // flag this new frame
|
||||||
@ -947,11 +951,20 @@ int cFrameDetector::Analyze(const uchar *Data, int Length)
|
|||||||
break;
|
break;
|
||||||
case 0x1B: // MPEG 4 video
|
case 0x1B: // MPEG 4 video
|
||||||
if (scanner == 0x00000109) { // Access Unit Delimiter
|
if (scanner == 0x00000109) { // Access Unit Delimiter
|
||||||
|
if (frameTypeOffset < 0) {
|
||||||
|
frameTypeOffset = i + 1;
|
||||||
|
if (frameTypeOffset >= TS_SIZE) { // the byte to check is in the next TS packet
|
||||||
|
frameTypeOffset -= TS_SIZE;
|
||||||
|
if (!synced)
|
||||||
|
dbgframes("%d>", frameTypeOffset);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
scanner = EMPTY_SCANNER;
|
scanner = EMPTY_SCANNER;
|
||||||
if (synced && !SeenPayloadStart && Processed)
|
|
||||||
return Processed; // flush everything before this new frame
|
|
||||||
newFrame = true;
|
newFrame = true;
|
||||||
independentFrame = Data[i + 1] == 0x10;
|
uchar FrameType = Data[frameTypeOffset];
|
||||||
|
frameTypeOffset = -1;
|
||||||
|
independentFrame = FrameType == 0x10;
|
||||||
if (synced) {
|
if (synced) {
|
||||||
if (framesPerPayloadUnit < 0) {
|
if (framesPerPayloadUnit < 0) {
|
||||||
payloadUnitOfFrame = (payloadUnitOfFrame + 1) % -framesPerPayloadUnit;
|
payloadUnitOfFrame = (payloadUnitOfFrame + 1) % -framesPerPayloadUnit;
|
||||||
@ -969,7 +982,7 @@ int cFrameDetector::Analyze(const uchar *Data, int Length)
|
|||||||
numIFrames++;
|
numIFrames++;
|
||||||
if (numIFrames == 1)
|
if (numIFrames == 1)
|
||||||
numFrames++;
|
numFrames++;
|
||||||
dbgframes("%02X ", Data[i + 1]);
|
dbgframes("%02X ", FrameType);
|
||||||
}
|
}
|
||||||
if (synced)
|
if (synced)
|
||||||
return Processed + TS_SIZE; // flag this new frame
|
return Processed + TS_SIZE; // flag this new frame
|
||||||
|
15
remux.h
15
remux.h
@ -4,7 +4,7 @@
|
|||||||
* See the main source file 'vdr.c' for copyright information and
|
* See the main source file 'vdr.c' for copyright information and
|
||||||
* how to reach the author.
|
* how to reach the author.
|
||||||
*
|
*
|
||||||
* $Id: remux.h 2.29 2011/05/21 09:53:54 kls Exp $
|
* $Id: remux.h 2.30 2011/06/12 12:49:17 kls Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __REMUX_H
|
#ifndef __REMUX_H
|
||||||
@ -297,7 +297,7 @@ public:
|
|||||||
~cTsToPes();
|
~cTsToPes();
|
||||||
void PutTs(const uchar *Data, int Length);
|
void PutTs(const uchar *Data, int Length);
|
||||||
///< Puts the payload data of the single TS packet at Data into the converter.
|
///< Puts the payload data of the single TS packet at Data into the converter.
|
||||||
///< Length is always 188.
|
///< Length is always TS_SIZE.
|
||||||
///< If the given TS packet starts a new PES payload packet, the converter
|
///< If the given TS packet starts a new PES payload packet, the converter
|
||||||
///< will be automatically reset. Any packets before the first one that starts
|
///< will be automatically reset. Any packets before the first one that starts
|
||||||
///< a new PES payload packet will be ignored.
|
///< a new PES payload packet will be ignored.
|
||||||
@ -336,16 +336,16 @@ void PesDump(const char *Name, const u_char *Data, int Length);
|
|||||||
|
|
||||||
// Frame detector:
|
// Frame detector:
|
||||||
|
|
||||||
#define MIN_TS_PACKETS_FOR_FRAME_DETECTOR 2
|
|
||||||
|
|
||||||
class cFrameDetector {
|
class cFrameDetector {
|
||||||
private:
|
private:
|
||||||
enum { MaxPtsValues = 150 };
|
enum { MaxPtsValues = 150 };
|
||||||
int pid;
|
int pid;
|
||||||
int type;
|
int type;
|
||||||
bool synced;
|
bool synced;
|
||||||
|
bool newPayload;
|
||||||
bool newFrame;
|
bool newFrame;
|
||||||
bool independentFrame;
|
bool independentFrame;
|
||||||
|
int frameTypeOffset;
|
||||||
uint32_t ptsValues[MaxPtsValues]; // 32 bit is enough - we only need the delta
|
uint32_t ptsValues[MaxPtsValues]; // 32 bit is enough - we only need the delta
|
||||||
int numPtsValues;
|
int numPtsValues;
|
||||||
int numFrames;
|
int numFrames;
|
||||||
@ -371,12 +371,17 @@ public:
|
|||||||
///< the frame detector for actual work.
|
///< the frame detector for actual work.
|
||||||
int Analyze(const uchar *Data, int Length);
|
int Analyze(const uchar *Data, int Length);
|
||||||
///< Analyzes the TS packets pointed to by Data. Length is the number of
|
///< Analyzes the TS packets pointed to by Data. Length is the number of
|
||||||
///< bytes Data points to, and must be a multiple of 188.
|
///< bytes Data points to, and must be a multiple of TS_SIZE.
|
||||||
///< Returns the number of bytes that have been analyzed.
|
///< Returns the number of bytes that have been analyzed.
|
||||||
///< If the return value is 0, the data was not sufficient for analyzing and
|
///< If the return value is 0, the data was not sufficient for analyzing and
|
||||||
///< Analyze() needs to be called again with more actual data.
|
///< Analyze() needs to be called again with more actual data.
|
||||||
bool Synced(void) { return synced; }
|
bool Synced(void) { return synced; }
|
||||||
///< Returns true if the frame detector has synced on the data stream.
|
///< Returns true if the frame detector has synced on the data stream.
|
||||||
|
bool NewPayload(void) { return newPayload; }
|
||||||
|
///< Returns true if the data given to the last call to Analyze() started a
|
||||||
|
///< new payload. The caller should remember the current file offset in
|
||||||
|
///< order to be able to generate an index entry later, when NewFrame()
|
||||||
|
///< returns true.
|
||||||
bool NewFrame(void) { return newFrame; }
|
bool NewFrame(void) { return newFrame; }
|
||||||
///< Returns true if the data given to the last call to Analyze() started a
|
///< Returns true if the data given to the last call to Analyze() started a
|
||||||
///< new frame.
|
///< new frame.
|
||||||
|
Loading…
Reference in New Issue
Block a user