Fix for distortions at file split points [was: Re: [ANNOUNCE] VDR developer version 1.7.19]

Message ID 4E3E9AF8.6070203@tvdr.de
State New
Headers

Commit Message

Klaus Schmidinger Aug. 7, 2011, 2:02 p.m. UTC
  On 20.06.2011 00:22, Udo Richter wrote:
> Am 19.06.2011 22:57, schrieb Klaus Schmidinger:
>> On 19.06.2011 12:41, Klaus Schmidinger wrote:
>>> - Fixed detecting frames in case the Picture Start Code or Access Unit
>>> Delimiter
>>> extends over TS packet boundaries (reported by Johan Andersson).
>>
>> I'm afraid this change causes a short distortion in video and audio
>> when VDR switches from one .ts file to the next during replay.
>> I have yet to investigate and fix this, just wanted to warn people
>> who make important recordings with this new version - which, of course,
>> nobody should do, since it is a developer version ;-)
>
> Thanks for the hint, reverted to 1.7.18 for now. Or, of course, would
> have if I were using a developer version, which nobody would do. ;)
>
> Can you provide a diff that reverts just that changeset so we can use
> all the other goodies of 1.7.19 for now? Eh, could, if we would use
> developer builds, of course. ;)

Please try the attached patch with VDR 1.7.19.

cRecorder::Action() now buffers TS packets in case the frame type is
not yet known when a new payload starts. This adds no overhead for channels
that broadcast the frame type within the first TS packet of a payload; it only
kicks in if that information is not in the first TS packet.

For testing you may want to set MaxVideoFileSize (in setup.conf) to
a lower value, so that recordings are split into many files.

Since I don't know of any channel I can receive that doesn't encode
the picture type in the first TS packet of a new payload, I was only
able to test whether the distortion problem is gone for "normal"
channels ("unnormal" being those that don't encode the picture type
in the first TS packet ;-). Therefore the actual buffering functionality
is untested.

If you get a "frame type not in first packet of payload - buffering"
or any other of the new log messages, please let me know.

Klaus
  

Patch

--- recorder.c	2011/06/12 14:16:45	2.11
+++ recorder.c	2011/08/07 13:36:05
@@ -10,7 +10,7 @@ 
 #include "recorder.h"
 #include "shutdown.h"
 
-#define RECORDERBUFSIZE  MEGABYTE(5)
+#define RECORDERBUFSIZE  (MEGABYTE(5) / TS_SIZE * TS_SIZE) // multiple of TS_SIZE
 
 // The maximum time we wait before assuming that a recorded video data stream
 // is broken:
@@ -88,7 +88,7 @@ 
 
 bool cRecorder::NextFile(void)
 {
-  if (recordFile && frameDetector->IndependentFrame()) { // every file shall start with an independent frame
+  if (recordFile) {
      if (fileSize > MEGABYTE(off_t(Setup.MaxVideoFileSize)) || RunningLowOnDiskSpace()) {
         recordFile = fileName->NextFile();
         fileSize = 0;
@@ -119,8 +119,11 @@ 
   time_t t = time(NULL);
   bool InfoWritten = false;
   bool FirstIframeSeen = false;
-  int FileNumber = 0;
-  off_t FrameOffset = -1;
+#define BUFFERSIZE MEGABYTE(1)
+  bool Buffering = false;
+  int BufferIndex = 0;
+  int MaxBufferIndex = 0;
+  uchar *Buffer = NULL;
   while (Running()) {
         int r;
         uchar *b = ringBuffer->Get(r);
@@ -141,16 +144,37 @@ 
                        }
                     InfoWritten = true;
                     }
-                 if (frameDetector->NewPayload()) {
-                    FileNumber = fileName->Number();
-                    FrameOffset = fileSize;
+                 if (frameDetector->NewPayload()) { // We're at the first TS packet of a new payload...
+                    if (Buffering)
+                       esyslog("ERROR: encountered new payload while buffering - dropping some data!");
+                    if (!frameDetector->NewFrame()) { // ...but the frame type is yet unknown, so we need to buffer packets until we see the frame type
+                       if (!Buffer) {
+                          dsyslog("frame type not in first packet of payload - buffering");
+                          if (!(Buffer = MALLOC(uchar, BUFFERSIZE))) {
+                             esyslog("ERROR: can't allocate frame type buffer");
+                             break;
+                             }
+                          }
+                       BufferIndex = 0;
+                       Buffering = true;
+                       }
                     }
-                 if (FirstIframeSeen || frameDetector->IndependentFrame()) {
+                 else if (frameDetector->NewFrame()) // now we know the frame type, so stop buffering
+                    Buffering = false;
+                 if (Buffering) {
+                    if (BufferIndex + Count <= BUFFERSIZE) {
+                       memcpy(Buffer + BufferIndex, b, Count);
+                       BufferIndex += Count;
+                       }
+                    else
+                       esyslog("ERROR: too many bytes for frame type buffer (%d > %d) - dropped %d bytes", BufferIndex + Count, int(BUFFERSIZE), Count);
+                    }
+                 else if (FirstIframeSeen || frameDetector->IndependentFrame()) {
                     FirstIframeSeen = true; // start recording with the first I-frame
-                    if (!NextFile())
+                    if (frameDetector->IndependentFrame() && !NextFile()) // every file shall start with an independent frame
                        break;
                     if (index && frameDetector->NewFrame())
-                       index->Write(frameDetector->IndependentFrame(), FileNumber, FrameOffset);
+                       index->Write(frameDetector->IndependentFrame(), fileName->Number(), fileSize);
                     if (frameDetector->IndependentFrame()) {
                        recordFile->Write(patPmtGenerator.GetPat(), TS_SIZE);
                        fileSize += TS_SIZE;
@@ -160,6 +184,12 @@ 
                              fileSize += TS_SIZE;
                              }
                        }
+                    if (BufferIndex) {
+                       recordFile->Write(Buffer, BufferIndex); // if an error occurs here, the next write below will catch and report it
+                       if (BufferIndex > MaxBufferIndex)
+                          MaxBufferIndex = BufferIndex;
+                       BufferIndex = 0;
+                       }
                     if (recordFile->Write(b, Count) < 0) {
                        LOG_ERROR_STR(fileName->Name());
                        break;
@@ -177,4 +207,8 @@ 
            t = time(NULL);
            }
         }
+  if (Buffer) {
+     free(Buffer);
+     dsyslog("frame type buffer used %d bytes", MaxBufferIndex);
+     }
 }