@@ -119,51 +119,87 @@ void writeDVDraw(const unsigned char *data1, int length1,
119119 struct cc_subtitle * sub )
120120{
121121 /* these are only used by DVD raw mode: */
122- static int loopcount = 1 ; /* loop 1: 5 elements, loop 2: 8 elements,
123- loop 3: 11 elements, rest: 15 elements */
124- static int datacount = 0 ; /* counts within loop */
122+ static int loopcount = 1 ; /* loop 1: 5 elements, loop 2: 8 elements,
123+ loop 3: 11 elements, rest: 15 elements */
124+ static int datacount = 0 ; /* counts within loop */
125+ static int waiting_for_field2 = 0 ; /* track if we're waiting for field 2 data */
125126
126- if (datacount == 0 )
127+ /* printdata() is called separately for field 1 and field 2 data.
128+ * Field 1: data1 set, data2 NULL
129+ * Field 2: data1 NULL, data2 set
130+ * We need to combine them into the DVD raw format: ff [d1] fe [d2]
131+ */
132+
133+ /* If we have data1 only (field 1), write ff + data1 and wait for field 2 */
134+ if (data1 && length1 && (!data2 || !length2 ))
127135 {
128- writeraw (DVD_HEADER , sizeof (DVD_HEADER ), NULL , sub );
129- if (loopcount == 1 )
130- writeraw (lc1 , sizeof (lc1 ), NULL , sub );
131- if (loopcount == 2 )
132- writeraw (lc2 , sizeof (lc2 ), NULL , sub );
133- if (loopcount == 3 )
134- {
135- writeraw (lc3 , sizeof (lc3 ), NULL , sub );
136- if (data2 && length2 )
137- writeraw (data2 , length2 , NULL , sub );
138- }
139- if (loopcount > 3 )
136+ if (datacount == 0 )
140137 {
141- writeraw (lc4 , sizeof (lc4 ), NULL , sub );
142- if (data2 && length2 )
143- writeraw (data2 , length2 , NULL , sub );
138+ writeraw (DVD_HEADER , sizeof (DVD_HEADER ), NULL , sub );
139+ if (loopcount == 1 )
140+ writeraw (lc1 , sizeof (lc1 ), NULL , sub );
141+ else if (loopcount == 2 )
142+ writeraw (lc2 , sizeof (lc2 ), NULL , sub );
143+ else if (loopcount == 3 )
144+ writeraw (lc3 , sizeof (lc3 ), NULL , sub );
145+ else
146+ writeraw (lc4 , sizeof (lc4 ), NULL , sub );
144147 }
145- }
146- datacount ++ ;
147- writeraw (lc5 , sizeof (lc5 ), NULL , sub );
148- if (data1 && length1 )
148+ writeraw (lc5 , sizeof (lc5 ), NULL , sub ); /* ff */
149149 writeraw (data1 , length1 , NULL , sub );
150- if (((loopcount == 1 ) && (datacount < 5 )) || ((loopcount == 2 ) && (datacount < 8 )) || ((loopcount == 3 ) && (datacount < 11 )) ||
151- ((loopcount > 3 ) && (datacount < 15 )))
150+ waiting_for_field2 = 1 ;
151+ return ;
152+ }
153+
154+ /* If we have data2 only (field 2), write fe + data2 */
155+ if ((!data1 || !length1 ) && data2 && length2 )
152156 {
153- writeraw (lc6 , sizeof (lc6 ), NULL , sub );
154- if (data2 && length2 )
155- writeraw (data2 , length2 , NULL , sub );
157+ writeraw (lc6 , sizeof (lc6 ), NULL , sub ); /* fe */
158+ writeraw (data2 , length2 , NULL , sub );
159+ waiting_for_field2 = 0 ;
160+ datacount ++ ;
161+
162+ /* Check if we've completed a loop */
163+ int max_count = (loopcount == 1 ) ? 5 : (loopcount == 2 ) ? 8
164+ : (loopcount == 3 ) ? 11
165+ : 15 ;
166+ if (datacount >= max_count )
167+ {
168+ loopcount ++ ;
169+ datacount = 0 ;
170+ }
171+ return ;
156172 }
157- else
173+
174+ /* If we have both data1 and data2 (legacy behavior, just in case) */
175+ if (data1 && length1 && data2 && length2 )
158176 {
159- if (loopcount == 1 )
177+ if (datacount == 0 )
178+ {
179+ writeraw (DVD_HEADER , sizeof (DVD_HEADER ), NULL , sub );
180+ if (loopcount == 1 )
181+ writeraw (lc1 , sizeof (lc1 ), NULL , sub );
182+ else if (loopcount == 2 )
183+ writeraw (lc2 , sizeof (lc2 ), NULL , sub );
184+ else if (loopcount == 3 )
185+ writeraw (lc3 , sizeof (lc3 ), NULL , sub );
186+ else
187+ writeraw (lc4 , sizeof (lc4 ), NULL , sub );
188+ }
189+ writeraw (lc5 , sizeof (lc5 ), NULL , sub ); /* ff */
190+ writeraw (data1 , length1 , NULL , sub );
191+ writeraw (lc6 , sizeof (lc6 ), NULL , sub ); /* fe */
192+ writeraw (data2 , length2 , NULL , sub );
193+ datacount ++ ;
194+
195+ int max_count = (loopcount == 1 ) ? 5 : (loopcount == 2 ) ? 8
196+ : (loopcount == 3 ) ? 11
197+ : 15 ;
198+ if (datacount >= max_count )
160199 {
161- writeraw (lc6 , sizeof (lc6 ), NULL , sub );
162- if (data2 && length2 )
163- writeraw (data2 , length2 , NULL , sub );
200+ loopcount ++ ;
201+ datacount = 0 ;
164202 }
165- loopcount ++ ;
166- datacount = 0 ;
167203 }
168204}
169205
0 commit comments