Use FAB subtitler's rather odd DFC -> frame rate mapping.
[libsub.git] / src / stl_binary_writer.cc
index 334a5bb69076a6d55a7998ef26beb747cab62d43..9b98b84cf7de4fe3888045384e3d407d20876cbd 100644 (file)
 
 #include "stl_binary_writer.h"
 #include "subtitle.h"
+#include "iso6937.h"
+#include "stl_util.h"
 #include "compose.hpp"
+#include <boost/locale.hpp>
 #include <list>
 #include <cmath>
 #include <fstream>
@@ -34,6 +37,7 @@ using std::setw;
 using std::setfill;
 using std::max;
 using std::cout;
+using boost::locale::conv::utf_to_utf;
 using namespace sub;
 
 static void
@@ -108,6 +112,7 @@ sub::write_stl_binary (
        assert (editor_contact_details.size() <= 32);
        
        char* buffer = new char[1024];
+       memset (buffer, 0, 1024);
        ofstream output (file_name.string().c_str ());
        STLBinaryTables tables;
 
@@ -121,8 +126,8 @@ sub::write_stl_binary (
        set<int> check_rows;
        
        for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
-               int t = 0;
                for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
+                       int t = 0;
                        for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
                                t += k->text.size ();
                        }
@@ -151,7 +156,7 @@ sub::write_stl_binary (
        /* Code page: 850 */
        put_string (buffer + 0, "850");
        /* Disk format code */
-       put_string (buffer + 3, String::compose ("STL%1.01", rint (frames_per_second)));
+       put_string (buffer + 3, stl_frame_rate_to_dfc (frames_per_second));
        /* Display standard code: open subtitling */
        put_string (buffer + 11, "0");
        /* Character code table: Latin (ISO 6937) */
@@ -173,11 +178,11 @@ sub::write_stl_binary (
        /* Total number of subtitles */
        put_int_as_string (buffer + 243, subtitles.size (), 5);
        /* Total number of subtitle groups */
-       put_string (buffer + 248, "000");
+       put_string (buffer + 248, "001");
        /* Maximum number of displayable characters in any text row */
-       put_int_as_string (buffer + 251, 2, longest);
+       put_int_as_string (buffer + 251, longest, 2);
        /* Maximum number of displayable rows */
-       put_int_as_string (buffer + 253, 2, rows);
+       put_int_as_string (buffer + 253, rows, 2);
        /* Time code status */
        put_string (buffer + 255, "1");
        /* Start-of-programme time code */
@@ -188,7 +193,7 @@ sub::write_stl_binary (
        put_string (buffer + 272, "1");
        /* Disk sequence number */
        put_string (buffer + 273, "1");
-       put_string (buffer + 274, 32, country_of_origin);
+       put_string (buffer + 274, 3, country_of_origin);
        put_string (buffer + 277, 32, publisher);
        put_string (buffer + 309, 32, editor_name);
        put_string (buffer + 341, 32, editor_contact_details);
@@ -198,12 +203,16 @@ sub::write_stl_binary (
        int N = 0;
        for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
 
+               memset (buffer, 0, 1024);
+
                /* Subtitle group number */
-               put_int_as_int (buffer + 0, 0, 1);
+               put_int_as_int (buffer + 0, 1, 1);
                /* Subtitle number */
                put_int_as_int (buffer + 1, N, 2);
-               /* Extension block number */
-               put_int_as_int (buffer + 3, 0, 1);
+               /* Extension block number.  Use 0xff here to indicate that it is the last TTI
+                  block in this subtitle "set", as we only ever use one.
+               */
+               put_int_as_int (buffer + 3, 255, 1);
                /* Cumulative status */
                put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
                /* Time code in */
@@ -247,7 +256,7 @@ sub::write_stl_binary (
                                        italic = false;
                                }
 
-                               text += k->text;
+                               text += utf16_to_iso6937 (utf_to_utf<wchar_t> (k->text));
                        }
 
                        text += "\x8A";