1 
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5 #define SCHRO_ARITH_DEFINE_INLINE
6 #include <schroedinger/schro.h>
7 #include <schroedinger/schrocuda.h>
8 #include <schroedinger/schrogpuframe.h>
9 #include <schroedinger/opengl/schroopengl.h>
10 #include <schroedinger/opengl/schroopenglframe.h>
11 #include <schroedinger/opengl/schroopenglmotion.h>
12 #include <schroedinger/schroorc.h>
13 #include <string.h>
14 #include <stdio.h>
15 
16 
17 #if 0
18 /* Used for testing bitstream */
19 #define MARKER() do{ \
20   SCHRO_ASSERT(schro_unpack_decode_uint(unpack) == 1234567); \
21 } while(0)
22 #else
23 #define MARKER()
24 #endif
25 
26 #define SCHRO_SKIP_TIME_CONSTANT 0.1
27 
28 typedef struct _SchroPictureSubbandContext SchroPictureSubbandContext;
29 
30 struct _SchroPictureSubbandContext
31 {
32   int component;
33   int index;
34   int position;
35   int broken;
36 
37   SchroFrameData *frame_data;
38   SchroFrameData *parent_frame_data;
39 
40   int quant_index;
41   int is_intra;
42   int subband_length;
43   SchroArith *arith;
44   SchroUnpack unpack;
45   int vert_codeblocks;
46   int horiz_codeblocks;
47   int have_zero_flags;
48   int have_quant_offset;
49 
50   int ymin;
51   int ymax;
52   int xmin;
53   int xmax;
54 
55   int quant_factor;
56   int quant_offset;
57 };
58 
59 enum
60 {
61   SCHRO_DECODER_STAGE_INIT = 0,
62   SCHRO_DECODER_STAGE_REFERENCES,
63   SCHRO_DECODER_STAGE_MOTION_DECODE,
64   SCHRO_DECODER_STAGE_MOTION_RENDER,
65   SCHRO_DECODER_STAGE_RESIDUAL_DECODE,
66   SCHRO_DECODER_STAGE_WAVELET_TRANSFORM,
67   SCHRO_DECODER_STAGE_COMBINE,
68   SCHRO_DECODER_STAGE_UPSAMPLE,
69   SCHRO_DECODER_STAGE_DONE
70 };
71 
72 
73 int _schro_decode_prediction_only;
74 int _schro_telemetry;
75 
76 static void schro_decoder_x_decode_motion (SchroAsyncStage * stage);
77 static void schro_decoder_x_render_motion (SchroAsyncStage * stage);
78 static void schro_decoder_x_decode_residual (SchroAsyncStage * stage);
79 static void schro_decoder_x_wavelet_transform (SchroAsyncStage * stage);
80 static void schro_decoder_x_combine (SchroAsyncStage * stage);
81 static void schro_decoder_x_upsample (SchroAsyncStage * stage);
82 
83 static void schro_decoder_reference_add (SchroDecoderInstance * instance,
84     SchroPicture * picture);
85 static SchroPicture *schro_decoder_reference_get (SchroDecoderInstance *
86     instance, SchroPictureNumber frame_number);
87 static void schro_decoder_reference_retire (SchroDecoderInstance * instance,
88     SchroPictureNumber frame_number);
89 static void schro_decoder_decode_subband (SchroPicture * picture,
90     SchroPictureSubbandContext * ctx);
91 static int schro_decoder_async_schedule (SchroDecoder * decoder,
92     SchroExecDomain domain);
93 static void schro_decoder_picture_complete (SchroAsyncStage * stage);
94 
95 static void schro_decoder_error (SchroDecoder * decoder, const char *s);
96 
97 static void schro_decoder_set_rob_size (SchroDecoderInstance * instance);
98 
99 static int schro_decoder_frame_is_twofield (SchroDecoderInstance * instance,
100     SchroFrame * frame);
101 
102 static void schro_picturequeue_rob_insert (SchroQueue * queue,
103     SchroPicture * picture, unsigned windowsize);
104 static int schro_picture_n_before_m (SchroPictureNumber n,
105     SchroPictureNumber m);
106 
107 static void schro_decoder_telemetry (SchroPicture * picture,
108     SchroFrame * output_picture);
109 
110 /* API */
111 
112 static SchroDecoderInstance *
schro_decoder_instance_new(SchroDecoder * decoder)113 schro_decoder_instance_new (SchroDecoder * decoder)
114 {
115   SchroDecoderInstance *instance;
116 
117   instance = schro_malloc0 (sizeof (SchroDecoderInstance));
118 
119   instance->decoder = decoder;
120 
121   instance->reference_queue = schro_queue_new (SCHRO_LIMIT_REFERENCE_FRAMES,
122       (SchroQueueFreeFunc) schro_picture_unref);
123   /* the output queue shouldn't be too large: the output queue may be
124    * a precious resource, no point holding on to loads without reason */
125   instance->output_queue =
126       schro_queue_new (4, (SchroQueueFreeFunc) schro_frame_unref);
127 
128   instance->reorder_queue =
129       schro_queue_new (5, (SchroQueueFreeFunc) schro_picture_unref);
130 
131   schro_decoder_set_rob_size (instance);
132 
133   return instance;
134 }
135 
136 static void
schro_decoder_instance_free(SchroDecoderInstance * instance)137 schro_decoder_instance_free (SchroDecoderInstance * instance)
138 {
139   schro_queue_free (instance->output_queue);
140   schro_queue_free (instance->reference_queue);
141   schro_queue_free (instance->reorder_queue);
142 
143   if (instance->sequence_header_buffer) {
144     schro_buffer_unref (instance->sequence_header_buffer);
145     instance->sequence_header_buffer = NULL;
146   }
147 
148   /* do not call instance_free(instance->next).
149    * It must be handled by the caller if required */
150 
151   schro_free (instance);
152 }
153 
154 /**
155  * schro_decoder_new:
156  *
157  * Creates a new decoder object.  The decoder object should be freed
158  * using @schro_decoder_free() when it is no longer needed.
159  *
160  * Returns: a new decoder object
161  */
162 SchroDecoder *
schro_decoder_new(void)163 schro_decoder_new (void)
164 {
165   SchroDecoder *decoder;
166 
167   decoder = schro_malloc0 (sizeof (SchroDecoder));
168 
169   schro_tables_init ();
170 
171   decoder->skip_value = 1.0;
172   decoder->skip_ratio = 1.0;
173 
174   decoder->input_buflist = schro_buflist_new ();
175   decoder->sps = schro_parse_sync_new ();
176 
177   decoder->cpu_domain = schro_memory_domain_new_local ();
178 #ifdef HAVE_CUDA
179   decoder->cuda_domain = schro_memory_domain_new_cuda ();
180 #endif
181 #ifdef HAVE_OPENGL
182   decoder->opengl_domain = schro_memory_domain_new_opengl ();
183 #endif
184 
185   decoder->async = schro_async_new (0,
186       (SchroAsyncScheduleFunc) schro_decoder_async_schedule,
187       (SchroAsyncCompleteFunc) schro_decoder_picture_complete, decoder);
188 
189 #ifdef HAVE_CUDA
190   schro_async_add_exec_domain (decoder->async, SCHRO_EXEC_DOMAIN_CUDA);
191   decoder->use_cuda = TRUE;
192 #endif
193 
194 #ifdef HAVE_OPENGL
195   decoder->opengl = schro_opengl_new ();
196 
197   if (schro_opengl_is_usable (decoder->opengl)) {
198     schro_async_add_exec_domain (decoder->async, SCHRO_EXEC_DOMAIN_OPENGL);
199 
200     decoder->use_opengl = TRUE;
201   } else {
202     schro_opengl_free (decoder->opengl);
203 
204     decoder->opengl = NULL;
205     decoder->use_opengl = FALSE;
206   }
207 #endif
208 
209   decoder->instance = schro_decoder_instance_new (decoder);
210 
211   return decoder;
212 }
213 
214 /**
215  * schro_decoder_free:
216  * @decoder: decoder object
217  *
218  * Frees a decoder object.
219  */
220 void
schro_decoder_free(SchroDecoder * decoder)221 schro_decoder_free (SchroDecoder * decoder)
222 {
223   if (decoder->async) {
224     schro_async_free (decoder->async);
225   }
226 
227   do {
228     SchroDecoderInstance *next = decoder->instance->next;
229     schro_decoder_instance_free (decoder->instance);
230     decoder->instance = next;
231   } while (decoder->instance);
232 
233   schro_buflist_free (decoder->input_buflist);
234   schro_parse_sync_free (decoder->sps);
235 
236   if (decoder->error_message)
237     schro_free (decoder->error_message);
238 
239 #ifdef HAVE_OPENGL
240   if (decoder->opengl)
241     schro_opengl_free (decoder->opengl);
242 #endif
243 
244   if (decoder->cpu_domain)
245     schro_memory_domain_free (decoder->cpu_domain);
246   if (decoder->cuda_domain)
247     schro_memory_domain_free (decoder->cuda_domain);
248   if (decoder->opengl_domain)
249     schro_memory_domain_free (decoder->opengl_domain);
250 
251 
252   schro_free (decoder);
253 }
254 
255 /**
256  * schro_decoder_begin_sequence:
257  *
258  * Prepare the @decoder to accept a new sequence.
259  *
260  * Returns SCHRO_DECODER_OK
261  *         SCHRO_DECODER_ERROR: if oldest sequence hasn't been marked with
262  *                              end of sequence
263  */
264 int
schro_decoder_begin_sequence(SchroDecoder * decoder)265 schro_decoder_begin_sequence (SchroDecoder * decoder)
266 {
267   SchroDecoderInstance *instance = decoder->instance;
268   /* find newest sequence */
269   while (instance->next)
270     instance = instance->next;
271 
272   if (!instance->flushing || !instance->end_of_stream) {
273     return SCHRO_DECODER_ERROR;
274   }
275 
276   schro_async_lock (decoder->async);
277   instance->next = schro_decoder_instance_new (decoder);
278   schro_async_unlock (decoder->async);
279 
280   return SCHRO_DECODER_OK;
281 }
282 
283 /**
284  * schro_decoder_end_sequence:
285  *
286  * When decoder_wait() returns SCHRO_DECODER_EOS, the sequence
287  * that terminated must be cleaned up before the state machine
288  * will advance.
289  *
290  * Returns SCHRO_DECODER_OK:
291  *         SCHRO_DECODER_ERROR: if oldest sequence has not completed.
292  *         SCHRO_DECODER_ERROR: if the decoder has not been prepared
293  *                              for a new sequence.
294  */
295 int
schro_decoder_end_sequence(SchroDecoder * decoder)296 schro_decoder_end_sequence (SchroDecoder * decoder)
297 {
298   /* oldest sequence */
299   SchroDecoderInstance *instance = decoder->instance;
300   SchroDecoderInstance *old;
301 
302   if (!instance->flushing || !instance->end_of_stream ||
303       instance->reorder_queue->n > 0) {
304     /* can not guarantee no threads are using the instance */
305     return SCHRO_DECODER_ERROR;
306   }
307 
308   if (!instance->next) {
309     /* decoder has no new instance waiting */
310     return SCHRO_DECODER_ERROR;
311   }
312 
313   schro_async_lock (decoder->async);
314   old = instance->next;
315   schro_decoder_instance_free (instance);
316   decoder->instance = old;
317   schro_async_unlock (decoder->async);
318 
319   return SCHRO_DECODER_OK;
320 }
321 
322 /**
323  * schro_picture_new:
324  * @decoder: a decoder object
325  *
326  * Creates a new picture for @decoder.
327  *
328  * Internal API.
329  *
330  * Returns: a new picture
331  */
332 SchroPicture *
schro_picture_new(SchroDecoderInstance * instance)333 schro_picture_new (SchroDecoderInstance * instance)
334 {
335   SchroDecoder *decoder = instance->decoder;
336   SchroPicture *picture;
337   SchroFrameFormat frame_format;
338   SchroVideoFormat *video_format = &instance->video_format;
339   int picture_width, picture_height;
340   int iwt_width, iwt_height;
341   int picture_chroma_width, picture_chroma_height;
342 
343   picture = schro_malloc0 (sizeof (SchroPicture));
344   picture->refcount = 1;
345 
346   picture->decoder_instance = instance;
347 
348   picture->params.video_format = video_format;
349 
350   if (instance->bit_depth > 8) {
351     frame_format = schro_params_get_frame_format (32,
352         video_format->chroma_format);
353   } else {
354     frame_format = schro_params_get_frame_format (16,
355         video_format->chroma_format);
356   }
357   schro_video_format_get_picture_chroma_size (video_format,
358       &picture_chroma_width, &picture_chroma_height);
359 
360   picture_width = video_format->width;
361   picture_height = schro_video_format_get_picture_height (video_format);
362 
363   schro_video_format_get_iwt_alloc_size (video_format, &iwt_width,
364       &iwt_height, SCHRO_LIMIT_TRANSFORM_DEPTH);
365 
366   if (decoder->use_cuda) {
367     picture->transform_frame = schro_frame_new_and_alloc (decoder->cpu_domain,
368         frame_format, iwt_width, iwt_height);
369 #if 0
370     /* These get allocated later, while in the CUDA thread */
371     picture->mc_tmp_frame = schro_frame_new_and_alloc (decoder->cuda_domain,
372         frame_format, frame_width, frame_height);
373     picture->frame = schro_frame_new_and_alloc (decoder->cuda_domain,
374         frame_format, frame_width, frame_height);
375     picture->planar_output_frame =
376         schro_frame_new_and_alloc (decoder->cuda_domain, frame_format,
377         video_format->width, video_format->height);
378 #endif
379   } else if (decoder->use_opengl) {
380     picture->transform_frame = schro_frame_new_and_alloc (decoder->cpu_domain,
381         frame_format, iwt_width, iwt_height);
382     picture->planar_output_frame =
383         schro_frame_new_and_alloc (decoder->cpu_domain,
384         schro_params_get_frame_format (8, video_format->chroma_format),
385         video_format->width, video_format->height);
386   } else {
387     picture->mc_tmp_frame = schro_frame_new_and_alloc (decoder->cpu_domain,
388         frame_format, picture_width, picture_height);
389     picture->frame = schro_frame_new_and_alloc (decoder->cpu_domain,
390         frame_format, iwt_width, iwt_height);
391     picture->transform_frame = schro_frame_ref (picture->frame);
392   }
393 
394   SCHRO_DEBUG ("planar output frame %dx%d",
395       video_format->width, video_format->height);
396 
397   return picture;
398 }
399 
400 SchroPicture *
schro_picture_ref(SchroPicture * picture)401 schro_picture_ref (SchroPicture * picture)
402 {
403   picture->refcount++;
404   return picture;
405 }
406 
407 void
schro_picture_unref(SchroPicture * picture)408 schro_picture_unref (SchroPicture * picture)
409 {
410   SCHRO_ASSERT (picture->refcount > 0);
411   picture->refcount--;
412   if (picture->refcount == 0) {
413     int i;
414     int component;
415 
416     SCHRO_DEBUG ("freeing picture %p", picture);
417     for (component = 0; component < 3; component++) {
418       for (i = 0; i < SCHRO_LIMIT_SUBBANDS; i++) {
419         if (picture->subband_buffer[component][i]) {
420           schro_buffer_unref (picture->subband_buffer[component][i]);
421           picture->subband_buffer[component][i] = NULL;
422         }
423       }
424     }
425     for (i = 0; i < 9; i++) {
426       if (picture->motion_buffers[i]) {
427         schro_buffer_unref (picture->motion_buffers[i]);
428         picture->motion_buffers[i] = NULL;
429       }
430     }
431     if (picture->lowdelay_buffer)
432       schro_buffer_unref (picture->lowdelay_buffer);
433 
434     if (picture->transform_frame)
435       schro_frame_unref (picture->transform_frame);
436     if (picture->frame)
437       schro_frame_unref (picture->frame);
438     if (picture->mc_tmp_frame)
439       schro_frame_unref (picture->mc_tmp_frame);
440     if (picture->planar_output_frame)
441       schro_frame_unref (picture->planar_output_frame);
442     if (picture->output_picture)
443       schro_frame_unref (picture->output_picture);
444     if (picture->motion)
445       schro_motion_free (picture->motion);
446     if (picture->input_buffer)
447       schro_buffer_unref (picture->input_buffer);
448     if (picture->upsampled_frame)
449       schro_upsampled_frame_free (picture->upsampled_frame);
450     if (picture->ref0)
451       schro_picture_unref (picture->ref0);
452     if (picture->ref1)
453       schro_picture_unref (picture->ref1);
454     if (picture->ref_output_frame)
455       schro_frame_unref (picture->ref_output_frame);
456 
457     if (picture->tag)
458       schro_tag_free (picture->tag);
459 
460     schro_free (picture);
461   }
462 }
463 
464 /**
465  * schro_decoder_reset:
466  * @decoder: a decoder object
467  *
468  * Resets the internal state of the decoder.  This function should be
469  * called after a discontinuity of the stream, for example, as the
470  * result of a seek.
471  */
472 void
schro_decoder_reset(SchroDecoder * decoder)473 schro_decoder_reset (SchroDecoder * decoder)
474 {
475   schro_async_stop (decoder->async);
476 
477   schro_buflist_free (decoder->input_buflist);
478   decoder->input_buflist = schro_buflist_new ();
479 
480   schro_parse_sync_free (decoder->sps);
481   decoder->sps = schro_parse_sync_new ();
482 
483   schro_decoder_instance_free (decoder->instance);
484   decoder->instance = schro_decoder_instance_new (decoder);
485 
486   decoder->error = FALSE;
487 
488   schro_async_start (decoder->async);
489 }
490 
491 /**
492  * schro_decoder_get_video_format:
493  * @decoder: a decoder object
494  *
495  * Returns a structure containing information on the video format being
496  * decoded by the decoder.  This structure should be freed using free()
497  * when it is no longer needed.
498  *
499  * Returns: a video format structure
500  */
501 /* xxx: this is nolonger api/client thread safe with respect to reset */
502 SchroVideoFormat *
schro_decoder_get_video_format(SchroDecoder * decoder)503 schro_decoder_get_video_format (SchroDecoder * decoder)
504 {
505   SchroVideoFormat *format;
506 
507   /* FIXME check that decoder is in the right state */
508 
509   format = malloc (sizeof (SchroVideoFormat));
510   memcpy (format, &decoder->instance->video_format, sizeof (SchroVideoFormat));
511 
512   return format;
513 }
514 
515 /**
516  * schro_decoder_get_picture_number:
517  * @decoder: a decoder object
518  *
519  * Returns the picture number of the next picture that will be returned
520  * by @schro_decoder_pull().
521  *
522  * Returns: a picture number
523  */
524 SchroPictureNumber
schro_decoder_get_picture_number(SchroDecoder * decoder)525 schro_decoder_get_picture_number (SchroDecoder * decoder)
526 {
527   SchroDecoderInstance *instance = decoder->instance;
528   SchroPicture *picture = NULL;
529 
530   if (instance->reorder_queue->n >= instance->reorder_queue_size ||
531       instance->flushing) {
532     picture = schro_queue_peek (instance->reorder_queue);
533   }
534   if (picture)
535     return picture->picture_number;
536   return SCHRO_PICTURE_NUMBER_INVALID;
537 }
538 
539 /**
540  * schro_decoder_get_picture_tag:
541  * @decoder: a decoder object
542  *
543  * Returns any tag associated with the next picture to be returned
544  * by @schro_decoder_pull().  Ownership of the tag is transfered to
545  * the caller.
546  *
547  * Returns: a tag represented by void* or NULL
548  */
549 SchroTag *
schro_decoder_get_picture_tag(SchroDecoder * decoder)550 schro_decoder_get_picture_tag (SchroDecoder * decoder)
551 {
552   SchroDecoderInstance *instance = decoder->instance;
553   SchroPicture *picture = NULL;
554 
555   if (instance->reorder_queue->n >= instance->reorder_queue_size ||
556       instance->flushing) {
557     picture = schro_queue_peek (instance->reorder_queue);
558   }
559   if (picture) {
560     SchroTag *tag = picture->tag;
561     picture->tag = NULL;
562     return tag;
563   }
564   return NULL;
565 }
566 
567 /**
568  * schro_decoder_add_output_picture:
569  * @decoder: a decoder object
570  * @frame: the frame to add to the picture queue
571  *
572  * Adds a frame provided by the application to the picture queue.
573  * Frames in the picture queue will be used for decoding images, and
574  * are eventually returned to the application by schro_decoder_pull().
575  *
576  * The caller loses its reference to @frame after calling this
577  * function.
578  */
579 void
schro_decoder_add_output_picture(SchroDecoder * decoder,SchroFrame * frame)580 schro_decoder_add_output_picture (SchroDecoder * decoder, SchroFrame * frame)
581 {
582   schro_async_lock (decoder->async);
583   schro_queue_add (decoder->instance->output_queue, frame, 0);
584   schro_async_signal_scheduler (decoder->async);
585   schro_async_unlock (decoder->async);
586 }
587 
588 /**
589  * schro_decoder_set_earliest_frame:
590  * @decoder: a decoder object
591  * @earliest_frame: the earliest frame that the application is interested in
592  *
593  * The application can tell the decoder the earliest frame it is
594  * interested in by calling this function.  Subsequent calls to
595  * schro_decoder_pull() will only return pictures with picture
596  * numbers greater than or equal to this number.  The decoder will
597  * avoid decoding pictures that will not be displayed or used as
598  * reference pictures.
599  *
600  * This feature can be used for frame-accurate seeking.
601  *
602  * This function can be called at any time during decoding.  Calling
603  * this function with a picture number less than the current earliest
604  * frame setting is invalid.
605  */
606 void
schro_decoder_set_earliest_frame(SchroDecoder * decoder,SchroPictureNumber earliest_frame)607 schro_decoder_set_earliest_frame (SchroDecoder * decoder,
608     SchroPictureNumber earliest_frame)
609 {
610   decoder->earliest_frame = earliest_frame;
611 }
612 
613 /**
614  * schro_decoder_set_skip_ratio:
615  * @decoder: a decoder object
616  * @ratio: skip ratio.
617  *
618  * Sets the skip ratio of the decoder.  The skip ratio is used by the
619  * decoder to skip decoding of some pictures.  Reference pictures are
620  * always decoded.
621  *
622  * A picture is skipped when the running average of the proportion of
623  * pictures skipped is less than the skip ratio.  Reference frames are
624  * always decoded and contribute to the running average.  Thus, the
625  * actual ratio of skipped pictures may be larger than the requested
626  * skip ratio.
627  *
628  * The decoder indicates a skipped picture in the pictures returned
629  * by @schro_decoder_pull() by a frame that has a width and height of
630  * 0.
631  *
632  * The default skip ratio is 1.0, indicating that all pictures should
633  * be decoded.  A skip ratio of 0.0 indicates that no pictures should
634  * be decoded, although as mentioned above, some pictures will be
635  * decoded anyway.  Values outside the range of 0.0 to 1.0 are quietly
636  * clamped to that range.
637  *
638  * This function may be called at any time during decoding.
639  */
640 void
schro_decoder_set_skip_ratio(SchroDecoder * decoder,double ratio)641 schro_decoder_set_skip_ratio (SchroDecoder * decoder, double ratio)
642 {
643   if (ratio > 1.0)
644     ratio = 1.0;
645   if (ratio < 0.0)
646     ratio = 0.0;
647   decoder->skip_ratio = ratio;
648 }
649 
650 void
schro_decoder_set_picture_order(SchroDecoder * decoder,int order)651 schro_decoder_set_picture_order (SchroDecoder * decoder, int order)
652 {
653   SchroDecoderInstance *instance;
654 
655   decoder->coded_order = order == SCHRO_DECODER_PICTURE_ORDER_CODED;
656 
657   /* propagate to all instances */
658   for (instance = decoder->instance; instance; instance = instance->next) {
659     if (instance->have_sequence_header) {
660       SCHRO_ERROR ("Don't call this function after decoding has commenced");
661     }
662     schro_decoder_set_rob_size (instance);
663   }
664 }
665 
666 static int
schro_decoder_pull_is_ready_locked(SchroDecoder * decoder)667 schro_decoder_pull_is_ready_locked (SchroDecoder * decoder)
668 {
669   SchroDecoderInstance *instance = decoder->instance;
670   SchroPicture *picture = NULL;
671 
672   /* Not possible to pull from the RoB if not full */
673   /* NB, schro's RoB implementation can be larger than the spec */
674   if (instance->reorder_queue->n >= instance->reorder_queue_size ||
675       instance->flushing) {
676     picture = schro_queue_peek (instance->reorder_queue);
677   }
678 
679   if (!picture || !picture->stages[SCHRO_DECODER_STAGE_DONE].is_done) {
680     /* nothing avaliable, give up */
681     return FALSE;
682   }
683   if (!schro_decoder_frame_is_twofield (instance, picture->output_picture)) {
684     /* one picture is avaliable, sufficient for:
685      *  - frame_coding
686      *  - field_coding and user supplies fields */
687     return TRUE;
688   }
689   /* interlaced_coding with twofiled output: must be two pictures avaliable */
690   if (instance->flushing) {
691     /* except with broken streams and flushing.  there may only be 1,
692      * in which case, we are as ready as we'll ever be, and leave it
693      * up to _pull to fix missing field issue */
694     if (instance->reorder_queue->n == 1)
695       return TRUE;
696   }
697   SCHRO_ASSERT (instance->reorder_queue->n >= 2);
698 
699   /* don't check if the second field is the pair to the first, since the
700    * RoB is full, it would cause a deadlock to block on there being a
701    * valid pair at the front of the RoB. */
702   picture = instance->reorder_queue->elements[1].data;
703   /* is second field ready ? */
704   return picture->stages[SCHRO_DECODER_STAGE_DONE].is_done;
705 }
706 
707 /**
708  * schro_decoder_pull:
709  * @decoder: a decoder object
710  *
711  * Removes the next picture from the picture queue and returns a frame
712  * containing the image.
713  *
714  * The application provides the frames that pictures are decoded into,
715  * and the same frames are returned from this function.  However, the
716  * order of frames returned may be different than the order that the
717  * application provides the frames to the decoder.
718  *
719  * An exception to this is that skipped frames are indicated by a
720  * frame having a height and width equal to 0.  This frame is created
721  * using @schro_frame_new(), and is not one of the frames provided by
722  * the application.
723  *
724  * Frames should be freed using @schro_frame_unref() when no longer
725  * needed.  The frame must not be reused by the application, since it
726  * may contain a reference frame still in use by the decoder.
727  *
728  * Returns: the next picture
729  */
730 SchroFrame *
schro_decoder_pull(SchroDecoder * decoder)731 schro_decoder_pull (SchroDecoder * decoder)
732 {
733   SchroDecoderInstance *instance = decoder->instance;
734   SchroPicture *picture = NULL;
735   SchroPictureNumber picture_number;
736   SchroFrame *frame;
737 
738   schro_async_lock (decoder->async);
739 
740   if (schro_decoder_pull_is_ready_locked (decoder)) {
741     picture = schro_queue_pull (instance->reorder_queue);
742   }
743 
744   if (!picture) {
745     return NULL;
746   }
747 
748   /* XXX would be nice to warn if expected picture not present */
749   frame = schro_frame_ref (picture->output_picture);
750   picture_number = picture->picture_number;
751   schro_picture_unref (picture);
752   picture = NULL;
753 
754   if (schro_decoder_frame_is_twofield (instance, frame))
755     do {
756       /* only consider the 2nd field if it can reference
757        * picture->output_picture, ie frame is twofields */
758       if (picture_number & 1) {
759         /* The following is voilated:
760          * - 10.4p3 earliest field in each frame shall have an even picture number
761          * Then the head of the reorder_queue can't be the pair to picture */
762         break;
763       }
764 
765       picture = schro_queue_peek (decoder->instance->reorder_queue);
766       if (!picture) {
767         if (instance->flushing) {
768           /* when flushing, a broken stream might not have a pair of
769            * pictures: discard the frame, (otherwise at a broken sequence
770            * boundry, could cause two frames to be emitted rather than one) */
771           schro_frame_unref (frame);
772           frame = NULL;
773           break;
774         }
775         SCHRO_ASSERT (picture);
776       }
777       if (picture_number + 1 != picture->picture_number) {
778         /* The second field in the frame can only be a pair to the first if
779          * they have consecutive picture numbers */
780         break;
781       }
782 
783       /* second field is the pair to the first: discard it */
784       picture = schro_queue_pull (decoder->instance->reorder_queue);
785       picture_number = picture->picture_number;
786       schro_picture_unref (picture);
787       picture = NULL;
788     } while (0);
789 
790   instance->last_picture_number = picture_number;
791   instance->last_picture_number_valid = TRUE;
792 
793   schro_async_unlock (decoder->async);
794 
795   return frame;
796 }
797 
798 /**
799  * schro_decoder_push_ready:
800  * @decoder: a decoder object
801  *
802  * This function is used by the application to determine if it should push
803  * more data to the decoder.
804  *
805  * Returns: TRUE if the decoder is ready for more data
806  */
807 int
schro_decoder_push_ready(SchroDecoder * decoder)808 schro_decoder_push_ready (SchroDecoder * decoder)
809 {
810   int ret;
811   SchroDecoderInstance *instance = decoder->instance;
812   /* by definition, all instances prior to the last are flushing, just
813    * check if the final sequence not full */
814   while (instance->next)
815     instance = instance->next;
816 
817   schro_async_lock (decoder->async);
818   /* may not push more data if the decoder is flushing:
819    *   This occurs when an EOS is pushed and no new sequence is prepared */
820   ret = !instance->flushing
821       && !schro_queue_is_full (decoder->instance->reorder_queue);
822   schro_async_unlock (decoder->async);
823 
824   return ret;
825 }
826 
827 static int
schro_decoder_need_output_frame_locked(SchroDecoder * decoder)828 schro_decoder_need_output_frame_locked (SchroDecoder * decoder)
829 {
830   /* output frames are only required to satisfy completion of the
831    * oldest sequence. Do not iterate over instances */
832   SchroDecoderInstance *instance = decoder->instance;
833   int num_frames_in_hand = instance->output_queue->n;
834   int i;
835   if (schro_queue_is_full (instance->output_queue)) {
836     return 0;
837   }
838   if (instance->video_format.interlaced_coding) {
839     for (i = 0; i < instance->output_queue->n; i++) {
840       SchroFrame *output_frame = instance->output_queue->elements[i].data;
841       if (schro_decoder_frame_is_twofield (instance, output_frame)) {
842         /* this frame counts for two pictures */
843         num_frames_in_hand++;
844       }
845     }
846   }
847   for (i = 0; i < instance->reorder_queue->n; i++) {
848     SchroPicture *picture = instance->reorder_queue->elements[i].data;
849     if (!picture->output_picture)
850       num_frames_in_hand--;
851   }
852   return num_frames_in_hand < 0;
853 }
854 
855 int
schro_decoder_need_output_frame(SchroDecoder * decoder)856 schro_decoder_need_output_frame (SchroDecoder * decoder)
857 {
858   int ret;
859 
860   schro_async_lock (decoder->async);
861   ret = schro_decoder_need_output_frame_locked (decoder);
862   schro_async_unlock (decoder->async);
863 
864   return ret;
865 }
866 
867 static int
schro_decoder_get_status_locked(SchroDecoder * decoder)868 schro_decoder_get_status_locked (SchroDecoder * decoder)
869 {
870   SchroDecoderInstance *instance = decoder->instance;
871   /* NB, this function should be `pure' (no sideeffects),
872    * since it is called in a context which should not update state */
873   if (!instance) {
874     /* Under normal operation, this shouldn't be possible:
875      *  - only if the user uses the raw api in the wrong order */
876     schro_decoder_error (decoder, "Missing decoder instance");
877     return SCHRO_DECODER_ERROR;
878   }
879   if (instance->first_sequence_header) {
880     return SCHRO_DECODER_FIRST_ACCESS_UNIT;
881   }
882   if (schro_decoder_pull_is_ready_locked (decoder)) {
883     return SCHRO_DECODER_OK;
884   }
885   if (decoder->error) {
886     return SCHRO_DECODER_ERROR;
887   }
888   if (instance->have_sequence_header &&
889       schro_decoder_need_output_frame_locked (decoder)) {
890     return SCHRO_DECODER_NEED_FRAME;
891   }
892   if (!schro_queue_is_full (instance->reorder_queue) && !instance->flushing) {
893     return SCHRO_DECODER_NEED_BITS;
894   }
895 
896   if (instance->flushing && !instance->reorder_queue->n) {
897     if (instance->end_of_stream) {
898       return SCHRO_DECODER_EOS;
899     } else {
900       return SCHRO_DECODER_STALLED;
901     }
902   }
903 
904   return SCHRO_DECODER_WAIT;
905 }
906 
907 #if 0
908 static int
909 schro_decoder_get_status (SchroDecoder * decoder)
910 {
911   int ret;
912 
913   schro_async_lock (decoder->async);
914   ret = schro_decoder_get_status_locked (decoder);
915   schro_async_unlock (decoder->async);
916 
917   return ret;
918 }
919 #endif
920 
921 static void
schro_decoder_dump(SchroDecoder * decoder)922 schro_decoder_dump (SchroDecoder * decoder)
923 {
924   SchroDecoderInstance *instance = decoder->instance;
925   int i;
926 
927   SCHRO_ERROR ("index, picture_number, busy, state, needed_state, working");
928   for (i = 0; i < instance->reorder_queue->n; i++) {
929     SchroPicture *picture = instance->reorder_queue->elements[i].data;
930     int done = 0, needed = 0, j;
931     for (j = 0; j <= SCHRO_DECODER_STAGE_DONE; j++) {
932       done |= picture->stages[j].is_done << j;
933       needed |= picture->stages[j].is_needed << j;
934     }
935 
936     SCHRO_ERROR ("%d: %d %d %04x %04x -",
937         i, picture->picture_number, picture->busy, done, needed);
938   }
939   if (instance->reorder_queue->n >= instance->reorder_queue_size ||
940       instance->flushing) {
941     SCHRO_ERROR ("next_picture_number %d",
942         schro_decoder_get_picture_number (decoder));
943   } else {
944     SCHRO_ERROR ("reorder_queue too empty to determine next_picture_number: "
945         "needs: %d pictures",
946         instance->reorder_queue_size - instance->reorder_queue->n);
947   }
948 }
949 
950 /**
951  * schro_decoder_wait:
952  * @decoder: a decoder object
953  *
954  * Waits until the decoder requires the application to do something,
955  * e.g., push more data or remove a frame from the picture queue,
956  * and then returns the decoder status.
957  *
958  * Returns: decoder status
959  */
960 int
schro_decoder_wait(SchroDecoder * decoder)961 schro_decoder_wait (SchroDecoder * decoder)
962 {
963   int ret;
964 
965   schro_async_lock (decoder->async);
966   while (1) {
967     ret = schro_decoder_get_status_locked (decoder);
968     switch (ret) {
969       default:
970         goto cleanup;
971 
972       case SCHRO_DECODER_FIRST_ACCESS_UNIT:
973         /* clean up state for next iteration */
974         decoder->instance->first_sequence_header = FALSE;
975         goto cleanup;
976 
977       case SCHRO_DECODER_WAIT:
978         break;
979     }
980 
981     ret = schro_async_wait_locked (decoder->async);
982     if (!ret) {
983       SCHRO_ERROR ("doh!");
984       schro_decoder_dump (decoder);
985       /* hack */
986       schro_async_signal_scheduler (decoder->async);
987       //SCHRO_ASSERT(0);
988     }
989   }
990 cleanup:
991   schro_async_unlock (decoder->async);
992 
993   return ret;
994 }
995 
996 /**
997  * schro_decoder_autoparse_wait:
998  * @decoder: a decoder object
999  *
1000  * Waits until the decoder requires the application to do something,
1001  * e.g., push more data or remove a frame from the picture queue,
1002  * and then returns the decoder status.
1003  *
1004  * When using @schro_decoder_autoparse_push, this function should
1005  * be used instead of @schro_decoder_wait.
1006  *
1007  * Returns: decoder status
1008  */
1009 int
schro_decoder_autoparse_wait(SchroDecoder * decoder)1010 schro_decoder_autoparse_wait (SchroDecoder * decoder)
1011 {
1012   while (1) {
1013     int ret = schro_decoder_wait (decoder);
1014     switch (ret) {
1015       default:
1016         return ret;
1017       case SCHRO_DECODER_NEED_BITS:
1018         /* see if there is anything sitting in the input_buflist
1019          * before requesting more data */
1020         /* xxx, it would be good if this were moved to an idle thread */
1021         ret = schro_decoder_autoparse_push (decoder, NULL);
1022         if (ret == SCHRO_DECODER_NEED_BITS) {
1023           return ret;
1024         }
1025         break;
1026       case SCHRO_DECODER_EOS:
1027         /* delete the decoder instance -- it is finished with
1028          * This is safe since for the status to be EOS
1029          * there must be:
1030          *  - no work left in the decoder queue
1031          *  - no pictures left to retrieve
1032          * Therefore there are no threads running in this instance.
1033          */
1034         ret = schro_decoder_end_sequence (decoder);
1035         if (ret == SCHRO_DECODER_ERROR) {
1036           return SCHRO_DECODER_EOS;
1037         }
1038         /* try again to discover the next state: EOS never gets passed
1039          * to the user */
1040         continue;
1041     }
1042   }
1043 }
1044 
1045 int
schro_decoder_push_end_of_stream(SchroDecoder * decoder)1046 schro_decoder_push_end_of_stream (SchroDecoder * decoder)
1047 {
1048   SchroDecoderInstance *instance = decoder->instance;
1049   /* find newest sequence */
1050   while (instance->next)
1051     instance = instance->next;
1052 
1053   instance->flushing = TRUE;
1054   instance->end_of_stream = TRUE;
1055 
1056   return SCHRO_DECODER_EOS;
1057 }
1058 
1059 /**
1060  * schro_decoder_autoparse_push_end_of_sequence:
1061  *
1062  * Signals that the most recent sequence is to terminates: decoding
1063  * of all pictures still in progress will continue.
1064  *
1065  * The decoder state is prepared to immediately handle data for
1066  * a new sequence.
1067  *
1068  * Returns: SCHRO_DECODER_EOS
1069  */
1070 int
schro_decoder_autoparse_push_end_of_sequence(SchroDecoder * decoder)1071 schro_decoder_autoparse_push_end_of_sequence (SchroDecoder * decoder)
1072 {
1073   schro_decoder_push_end_of_stream (decoder);
1074   schro_decoder_begin_sequence (decoder);
1075   return SCHRO_DECODER_EOS;
1076 }
1077 
1078 /**
1079  * schro_decoder_set_flushing:
1080  *
1081  * This function is depricated and has no effect.
1082  * If control over the picture order is required, consult
1083  * schro_decoder_set_picture_order()
1084  */
1085 int
schro_decoder_set_flushing(SchroDecoder * decoder,int value)1086 schro_decoder_set_flushing (SchroDecoder * decoder, int value)
1087 {
1088   return SCHRO_DECODER_OK;
1089 }
1090 
1091 int
schro_decoder_push(SchroDecoder * decoder,SchroBuffer * buffer)1092 schro_decoder_push (SchroDecoder * decoder, SchroBuffer * buffer)
1093 {
1094   SchroDecoderInstance *instance = decoder->instance;
1095   SchroUnpack unpack;
1096   int parse_code;
1097 
1098   if (!instance) {
1099     /* this should not be possible with the autoparse api:
1100      *  - decoder is created with an instance
1101      *  - instances are created eachtime an EOS is inserted
1102      *  - instances are deleted when EOS is pulled.
1103      * => Next instance is created before previous one is destroyed
1104      * Assumptions:
1105      *  - Only examined case where wait() and push() are called
1106      *    from same thread
1107      */
1108     return SCHRO_DECODER_ERROR;
1109   }
1110 
1111   /* find newest sequence */
1112   while (instance->next)
1113     instance = instance->next;
1114   /* instance is now the next instance to send data to */
1115 
1116   instance->flushing = FALSE;
1117 
1118   /* Buffers may have associated (tagged) private data.
1119    * This data is to be associated with the next(/this) picture data unit */
1120   if (buffer->tag) {
1121     if (decoder->next_picture_tag) {
1122       schro_tag_free (decoder->next_picture_tag);
1123     }
1124     decoder->next_picture_tag = buffer->tag;
1125   }
1126   buffer->tag = NULL;
1127 
1128   schro_unpack_init_with_data (&unpack, buffer->data, buffer->length, 1);
1129   parse_code = schro_decoder_decode_parse_header (&unpack);
1130 
1131   if (parse_code == -1) {
1132     schro_buffer_unref (buffer);
1133     return SCHRO_DECODER_ERROR;
1134   }
1135 
1136   if (parse_code == SCHRO_PARSE_CODE_SEQUENCE_HEADER) {
1137     int ret;
1138 
1139     SCHRO_INFO ("decoding sequence header");
1140     if (!instance->have_sequence_header) {
1141       schro_decoder_parse_sequence_header (instance, &unpack);
1142       /* safe to resize RoB, since nothing can be inflight in this instance */
1143       schro_decoder_set_rob_size (instance);
1144       instance->have_sequence_header = TRUE;
1145       instance->first_sequence_header = TRUE;
1146       instance->sequence_header_buffer = schro_buffer_dup (buffer);
1147       instance->bit_depth = schro_video_format_get_bit_depth (&instance->video_format);
1148 
1149       ret = SCHRO_DECODER_FIRST_ACCESS_UNIT;
1150     } else {
1151       if (schro_decoder_compare_sequence_header_buffer (buffer,
1152               instance->sequence_header_buffer)) {
1153         ret = SCHRO_DECODER_OK;
1154       } else {
1155         schro_decoder_error (decoder, "sequence header changed");
1156         ret = SCHRO_DECODER_ERROR;
1157       }
1158     }
1159 
1160     schro_buffer_unref (buffer);
1161     return ret;
1162   }
1163 
1164   if (parse_code == SCHRO_PARSE_CODE_AUXILIARY_DATA) {
1165     int code;
1166 
1167     code = schro_unpack_decode_bits (&unpack, 8);
1168 
1169     if (code == SCHRO_AUX_DATA_MD5_CHECKSUM) {
1170       int i;
1171       for (i = 0; i < 16; i++) {
1172         instance->md5_checksum[i] = schro_unpack_decode_bits (&unpack, 8);
1173       }
1174       instance->has_md5 = TRUE;
1175     }
1176 
1177     if (code == SCHRO_AUX_DATA_ENCODER_STRING) {
1178       char s[20];
1179       int i;
1180 
1181       for (i = 0; i < 20; i++) {
1182         s[i] = schro_unpack_decode_bits (&unpack, 8);
1183       }
1184       if (memcmp ("Schroedinger 1.0.", s, 17) == 0) {
1185         if (s[17] >= '0' && s[17] <= '7' && (s[18] == '.'
1186                 || s[18] == (char) 0xff)) {
1187           SCHRO_WARNING ("turning on codeblock quantiser compatibility mode");
1188           instance->compat_quant_offset = TRUE;
1189         }
1190       }
1191     }
1192 
1193     schro_buffer_unref (buffer);
1194     return SCHRO_DECODER_OK;
1195   }
1196 
1197   if (SCHRO_PARSE_CODE_IS_PADDING (parse_code)) {
1198     schro_buffer_unref (buffer);
1199     return SCHRO_DECODER_OK;
1200   }
1201 
1202   if (SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE (parse_code)) {
1203     SCHRO_DEBUG ("decoding end sequence");
1204     schro_buffer_unref (buffer);
1205     instance->end_of_stream = TRUE;
1206     instance->flushing = TRUE;
1207     return SCHRO_DECODER_EOS;
1208   }
1209 
1210   if (SCHRO_PARSE_CODE_IS_PICTURE (parse_code)) {
1211     if (!instance->have_sequence_header) {
1212       SCHRO_INFO ("no sequence header -- dropping picture");
1213       if (decoder->next_picture_tag) {
1214         schro_tag_free (decoder->next_picture_tag);
1215       }
1216       decoder->next_picture_tag = NULL;
1217       schro_buffer_unref (buffer);
1218       return SCHRO_DECODER_OK;
1219     }
1220 
1221     return schro_decoder_iterate_picture (instance, buffer, &unpack,
1222         parse_code);
1223   }
1224 
1225   schro_buffer_unref (buffer);
1226   return SCHRO_DECODER_ERROR;
1227 }
1228 
1229 /**
1230  * schro_decoder_autoparse_push:
1231  *
1232  * Appends @buffer@ to the internal input queue, extracting work
1233  * units as necessary.  There are no alignment constraints on the
1234  * contents of @buffer@; it is not required to be data unit aligned,
1235  * nor contain a single whole data unit.
1236  *
1237  * Returns: if decoder ready for more data, but insufficient input buffer:
1238  *          SCHRO_DECODER_NEED_BITS
1239  *          otherwise SCHRO_DECODER_OK
1240  */
1241 int
schro_decoder_autoparse_push(SchroDecoder * decoder,SchroBuffer * buffer)1242 schro_decoder_autoparse_push (SchroDecoder * decoder, SchroBuffer * buffer)
1243 {
1244   /* 1. buffer is added to the decoder input queue.
1245    * 2. The synchronizer extracts (if possible) a data unit for parsing */
1246   if (buffer)
1247     schro_buflist_append (decoder->input_buflist, buffer);
1248 
1249   while (schro_decoder_push_ready (decoder)) {
1250     buffer = schro_parse_sync (decoder->sps, decoder->input_buflist);
1251     if (!buffer)
1252       return SCHRO_DECODER_NEED_BITS;
1253 
1254     switch (schro_decoder_push (decoder, buffer)) {
1255       default:
1256         break;
1257       case SCHRO_DECODER_EOS:
1258         /* Prepare decoder for immediately handling the next sequence */
1259         schro_decoder_begin_sequence (decoder);
1260         break;
1261     }
1262   }
1263 
1264   return SCHRO_DECODER_OK;
1265 }
1266 
1267 int
schro_decoder_iterate_picture(SchroDecoderInstance * instance,SchroBuffer * buffer,SchroUnpack * unpack,int parse_code)1268 schro_decoder_iterate_picture (SchroDecoderInstance * instance,
1269     SchroBuffer * buffer, SchroUnpack * unpack, int parse_code)
1270 {
1271   SchroDecoder *decoder = instance->decoder;
1272   SchroPicture *picture;
1273   SchroParams *params;
1274 
1275   picture = schro_picture_new (instance);
1276   params = &picture->params;
1277 
1278   picture->input_buffer = buffer;
1279 
1280   picture->tag = decoder->next_picture_tag;
1281   decoder->next_picture_tag = NULL;
1282 
1283   params->num_refs = SCHRO_PARSE_CODE_NUM_REFS (parse_code);
1284   params->is_lowdelay = SCHRO_PARSE_CODE_IS_LOW_DELAY (parse_code);
1285   params->is_noarith = !SCHRO_PARSE_CODE_USING_AC (parse_code);
1286   picture->is_ref = SCHRO_PARSE_CODE_IS_REFERENCE (parse_code);
1287 
1288   if (instance->has_md5) {
1289     picture->has_md5 = TRUE;
1290     memcpy (picture->md5_checksum, instance->md5_checksum, 16);
1291     instance->has_md5 = FALSE;
1292   }
1293 
1294   schro_decoder_parse_picture_header (picture, unpack);
1295 
1296   SCHRO_DEBUG ("picturenumber: %u", picture->picture_number);
1297 
1298   if (picture->is_ref) {
1299     /* xxx: why is this locked? -- no decoding should actually consult the
1300      * reference buffer surely? */
1301     schro_async_lock (instance->decoder->async);
1302     schro_decoder_reference_retire (instance, picture->retired_picture_number);
1303     schro_decoder_reference_add (instance, picture);
1304     schro_async_unlock (instance->decoder->async);
1305   }
1306   schro_decoder_parse_picture (picture, unpack);
1307 
1308   if (picture->error) {
1309     SCHRO_WARNING ("skipping because of error");
1310     picture->skip = TRUE;
1311   }
1312 
1313   if (instance->last_picture_number_valid) {
1314     if (schro_picture_n_before_m (picture->picture_number,
1315             instance->last_picture_number)) {
1316       /* picture logically occurs before a picture that has already been
1317        * emitted. The stream must have jumped backwards. It is important
1318        * to detect this, otherwise pictures can get stuck in the reorder
1319        * buffer.  NB, it isn't required to detect a forwards jump, since
1320        * this will cause the reorder buffer to flush naturally */
1321       /* xxx: future improvements would be to also note if a sequence header
1322        * occured just before this picture, and to inject that into the new
1323        * instance */
1324       SCHRO_WARNING ("stream jumped backwards, %u before %u, treating as EOS",
1325           picture->picture_number, instance->last_picture_number);
1326       schro_picture_unref (picture);
1327       schro_decoder_push_end_of_stream (decoder);
1328       return SCHRO_DECODER_EOS;
1329     }
1330   }
1331 
1332   /* todo: prune pictures that are inaccessible in the first gop */
1333 
1334   if (!instance->video_format.interlaced_coding &&
1335       !picture->is_ref && picture->picture_number < decoder->earliest_frame) {
1336     picture->skip = TRUE;
1337     SCHRO_INFO ("skipping frame %d (early)", picture->picture_number);
1338   }
1339 
1340   if (!instance->video_format.interlaced_coding &&
1341       !picture->is_ref && decoder->skip_value > decoder->skip_ratio) {
1342     decoder->skip_value = (1 - SCHRO_SKIP_TIME_CONSTANT) * decoder->skip_value;
1343     SCHRO_INFO ("skipping frame %d", picture->picture_number);
1344     SCHRO_DEBUG ("skip value %g ratio %g", decoder->skip_value,
1345         decoder->skip_ratio);
1346 
1347     picture->skip = TRUE;
1348   } else {
1349     decoder->skip_value = (1 - SCHRO_SKIP_TIME_CONSTANT) * decoder->skip_value +
1350         SCHRO_SKIP_TIME_CONSTANT;
1351   }
1352   SCHRO_DEBUG ("skip value %g ratio %g", decoder->skip_value,
1353       decoder->skip_ratio);
1354 
1355   if (picture->skip) {
1356     picture->output_picture = schro_frame_new ();
1357     if (picture->is_ref) {
1358       SchroFrameFormat frame_format;
1359       SchroFrame *ref;
1360 
1361       frame_format = schro_params_get_frame_format (8,
1362           params->video_format->chroma_format);
1363       ref =
1364           schro_frame_new_and_alloc_full (decoder->cpu_domain, frame_format,
1365           instance->video_format.width,
1366           schro_video_format_get_picture_height (&instance->video_format), 32,
1367           TRUE);
1368       schro_frame_clear (ref);
1369       picture->upsampled_frame = schro_upsampled_frame_new (ref);
1370     }
1371 
1372     SCHRO_DEBUG ("adding %d to queue (skipped)", picture->picture_number);
1373 
1374     picture->stages[SCHRO_DECODER_STAGE_DONE].is_done = TRUE;
1375     picture->stages[SCHRO_DECODER_STAGE_DONE].is_needed = TRUE;
1376   }
1377 
1378   schro_async_lock (decoder->async);
1379   SCHRO_DEBUG ("adding %d to queue", picture->picture_number);
1380   schro_picturequeue_rob_insert (instance->reorder_queue, picture,
1381       instance->reorder_queue_size);
1382   schro_async_signal_scheduler (decoder->async);
1383   schro_async_unlock (decoder->async);
1384 
1385   return SCHRO_DECODER_OK;
1386 }
1387 
1388 void
schro_decoder_parse_picture(SchroPicture * picture,SchroUnpack * unpack)1389 schro_decoder_parse_picture (SchroPicture * picture, SchroUnpack * unpack)
1390 {
1391   SchroParams *params = &picture->params;
1392 
1393   if (params->num_refs > 0) {
1394     SCHRO_DEBUG ("inter");
1395 
1396     schro_async_lock (picture->decoder_instance->decoder->async);
1397     picture->ref0 =
1398         schro_decoder_reference_get (picture->decoder_instance,
1399         picture->reference1);
1400     if (picture->ref0 == NULL) {
1401       SCHRO_WARNING ("ref0 not found");
1402       picture->error = TRUE;
1403       schro_async_unlock (picture->decoder_instance->decoder->async);
1404       return;
1405     }
1406     schro_picture_ref (picture->ref0);
1407 
1408     picture->ref1 = NULL;
1409     if (params->num_refs > 1) {
1410       picture->ref1 =
1411           schro_decoder_reference_get (picture->decoder_instance,
1412           picture->reference2);
1413       if (picture->ref1 == NULL) {
1414         SCHRO_WARNING ("ref1 not found");
1415         picture->error = TRUE;
1416         schro_async_unlock (picture->decoder_instance->decoder->async);
1417         return;
1418       }
1419       schro_picture_ref (picture->ref1);
1420     }
1421     schro_async_unlock (picture->decoder_instance->decoder->async);
1422 
1423     schro_unpack_byte_sync (unpack);
1424     schro_decoder_parse_picture_prediction_parameters (picture, unpack);
1425 
1426     if (!picture->error) {
1427       schro_params_calculate_mc_sizes (params);
1428     }
1429 
1430     schro_unpack_byte_sync (unpack);
1431     schro_decoder_parse_block_data (picture, unpack);
1432   }
1433 
1434   schro_unpack_byte_sync (unpack);
1435   picture->zero_residual = FALSE;
1436   if (params->num_refs > 0) {
1437     picture->zero_residual = schro_unpack_decode_bit (unpack);
1438 
1439     SCHRO_DEBUG ("zero residual %d", picture->zero_residual);
1440   }
1441 
1442   if (!picture->zero_residual) {
1443     schro_decoder_parse_transform_parameters (picture, unpack);
1444     schro_params_calculate_iwt_sizes (params);
1445 
1446     schro_unpack_byte_sync (unpack);
1447     if (params->is_lowdelay) {
1448       schro_decoder_parse_lowdelay_transform_data (picture, unpack);
1449     } else {
1450       schro_decoder_parse_transform_data (picture, unpack);
1451 
1452       if (picture->decoder_instance->decoder->use_opengl) {
1453 #ifdef HAVE_OPENGL
1454         schro_decoder_init_subband_frame_data (picture);
1455 #else
1456         SCHRO_ASSERT (0);
1457 #endif
1458       } else {
1459         schro_decoder_init_subband_frame_data_interleaved (picture);
1460       }
1461     }
1462   }
1463 
1464   if (picture->error)
1465     return;
1466 
1467   picture->stages[SCHRO_DECODER_STAGE_REFERENCES].is_needed = TRUE;
1468   picture->stages[SCHRO_DECODER_STAGE_MOTION_DECODE].is_needed = TRUE;
1469   picture->stages[SCHRO_DECODER_STAGE_MOTION_RENDER].is_needed = TRUE;
1470   picture->stages[SCHRO_DECODER_STAGE_RESIDUAL_DECODE].is_needed = TRUE;
1471   picture->stages[SCHRO_DECODER_STAGE_WAVELET_TRANSFORM].is_needed = TRUE;
1472   picture->stages[SCHRO_DECODER_STAGE_COMBINE].is_needed = TRUE;
1473 }
1474 
1475 /**
1476  * schro_decoder_assign_output_picture:
1477  * @picture that needs consideration of its output_picture
1478  * @robpos position of picture in reorder buffer
1479  *
1480  * Assign an output picture to @picture, taking into account field_coding
1481  * and the sharing of a frame between fields.
1482  */
1483 static void
schro_decoder_assign_output_picture(SchroPicture * picture,int robpos)1484 schro_decoder_assign_output_picture (SchroPicture * picture, int robpos)
1485 {
1486   SchroDecoderInstance *instance = picture->decoder_instance;
1487 
1488   if (picture->output_picture)
1489     return;
1490 
1491   if (instance->video_format.interlaced_coding)
1492     do {
1493       SchroPicture *pair_picture;
1494       int pair_robpos;
1495       /* field coding: one frame is shared between two field pictures.
1496        * has the pair to this field already got a frame allocated?
1497        *  - If so, take a reference to the already allocated frame.
1498        *  - 10.4p3 earliest field in each frame shall have an even picture number
1499        *  => for even pictures, pair picture_number+1, for odd pictures, -1.
1500        * The action of the RoB guarantees that the pair will eventually be
1501        * sitting next to each other in the RoB. */
1502       /* find which RoB position the pair to this picture should reside: */
1503       pair_robpos = robpos + (picture->picture_number & 1 ? -1 : 1);
1504       if (pair_robpos < 0 || pair_robpos >= instance->reorder_queue->n)
1505         break;
1506       pair_picture = instance->reorder_queue->elements[pair_robpos].data;
1507       if (pair_picture->picture_number != (picture->picture_number ^ 1))
1508         break;
1509       /* picture_pair and picture are not only next to each other
1510        * in the RoB, but also belong to the same frame. */
1511       if (!pair_picture->output_picture)
1512         break;
1513       /* only reference the pairs output picture if its frame sized,
1514        * ie, the user supplied a frame and not a field */
1515       if (!schro_decoder_frame_is_twofield (instance,
1516               pair_picture->output_picture))
1517         break;
1518 
1519       /* pair_picture->output_picture is full frame */
1520       picture->output_picture = schro_frame_ref (pair_picture->output_picture);
1521     } while (0);
1522 
1523   if (!picture->output_picture) {
1524     /* either frame coding, or first field to be decoded of a frame */
1525     picture->output_picture = schro_queue_pull (instance->output_queue);
1526     /* NB, there may not be anything in the output_queue */
1527   }
1528 }
1529 
1530 void
schro_decoder_picture_complete(SchroAsyncStage * stage)1531 schro_decoder_picture_complete (SchroAsyncStage * stage)
1532 {
1533   SchroPicture *picture = (SchroPicture *) stage->priv;
1534 
1535   SCHRO_DEBUG ("picture complete");
1536 
1537   stage->is_done = TRUE;
1538   if (stage == picture->stages + SCHRO_DECODER_STAGE_COMBINE) {
1539     picture->stages[SCHRO_DECODER_STAGE_DONE].is_done = TRUE;
1540   }
1541   picture->busy = FALSE;
1542 
1543   schro_picture_unref (picture);
1544 }
1545 
1546 static int
schro_decoder_async_schedule(SchroDecoder * decoder,SchroExecDomain exec_domain)1547 schro_decoder_async_schedule (SchroDecoder * decoder,
1548     SchroExecDomain exec_domain)
1549 {
1550   int i;
1551   int render_ok;
1552   int decode_ok;
1553 
1554   SCHRO_DEBUG ("schedule");
1555 
1556   if (decoder->use_cuda || decoder->use_opengl) {
1557     if (exec_domain == SCHRO_EXEC_DOMAIN_CUDA ||
1558         exec_domain == SCHRO_EXEC_DOMAIN_OPENGL) {
1559       decode_ok = FALSE;
1560       render_ok = TRUE;
1561     } else {
1562       decode_ok = TRUE;
1563       render_ok = FALSE;
1564     }
1565   } else {
1566     decode_ok = TRUE;
1567     render_ok = TRUE;
1568   }
1569 
1570   for (i = 0; i < decoder->instance->reorder_queue->n; i++) {
1571     SchroPicture *picture = decoder->instance->reorder_queue->elements[i].data;
1572     void *func = NULL;
1573     int stage = 0;
1574 
1575     if (picture->busy)
1576       continue;
1577 
1578     SCHRO_DEBUG ("picture %d", picture->picture_number);
1579 
1580 #define TODO(stage) \
1581     (picture->stages[(stage)].is_needed && \
1582      !picture->stages[(stage)].is_done)
1583     if (TODO (SCHRO_DECODER_STAGE_REFERENCES)) {
1584       int j;
1585       int refs_ready = TRUE;
1586 
1587       for (j = 0; j < picture->params.num_refs; j++) {
1588         SchroPicture *refpic;
1589 
1590         refpic = (j == 0) ? picture->ref0 : picture->ref1;
1591 
1592         if (refpic->busy || !(refpic->stages[SCHRO_DECODER_STAGE_DONE].is_done)) {
1593           refs_ready = FALSE;
1594           continue;
1595         }
1596 
1597         if (
1598 #ifdef HAVE_CUDA
1599             1 &&
1600 #else
1601             picture->params.mv_precision > 0 &&
1602 #endif
1603             !(refpic->stages[SCHRO_DECODER_STAGE_UPSAMPLE].is_done)) {
1604           if (!render_ok) {
1605             refs_ready = FALSE;
1606             continue;
1607           }
1608           refpic->busy = TRUE;
1609 
1610           func = schro_decoder_x_upsample;
1611           schro_picture_ref (refpic);
1612           refpic->stages[SCHRO_DECODER_STAGE_UPSAMPLE].task_func = func;
1613           refpic->stages[SCHRO_DECODER_STAGE_UPSAMPLE].priv = refpic;
1614           schro_async_run_stage_locked (decoder->async,
1615               refpic->stages + SCHRO_DECODER_STAGE_UPSAMPLE);
1616 
1617           return TRUE;
1618         }
1619       }
1620       if (refs_ready) {
1621         picture->stages[SCHRO_DECODER_STAGE_REFERENCES].is_done = TRUE;
1622       }
1623     }
1624 
1625 
1626     if (TODO (SCHRO_DECODER_STAGE_RESIDUAL_DECODE) && decode_ok) {
1627       func = schro_decoder_x_decode_residual;
1628       stage = SCHRO_DECODER_STAGE_RESIDUAL_DECODE;
1629     } else if (TODO (SCHRO_DECODER_STAGE_WAVELET_TRANSFORM) &&
1630         picture->stages[SCHRO_DECODER_STAGE_RESIDUAL_DECODE].is_done
1631         && render_ok) {
1632       func = schro_decoder_x_wavelet_transform;
1633       stage = SCHRO_DECODER_STAGE_WAVELET_TRANSFORM;
1634     } else if (TODO (SCHRO_DECODER_STAGE_MOTION_DECODE) &&
1635         picture->stages[SCHRO_DECODER_STAGE_REFERENCES].is_done && decode_ok) {
1636       func = schro_decoder_x_decode_motion;
1637       stage = SCHRO_DECODER_STAGE_MOTION_DECODE;
1638     } else if (TODO (SCHRO_DECODER_STAGE_MOTION_RENDER) &&
1639         picture->stages[SCHRO_DECODER_STAGE_MOTION_DECODE].is_done &&
1640         picture->stages[SCHRO_DECODER_STAGE_WAVELET_TRANSFORM].is_done &&
1641         render_ok) {
1642       func = schro_decoder_x_render_motion;
1643       stage = SCHRO_DECODER_STAGE_MOTION_RENDER;
1644     } else if (TODO (SCHRO_DECODER_STAGE_COMBINE) &&
1645         picture->stages[SCHRO_DECODER_STAGE_WAVELET_TRANSFORM].is_done &&
1646         picture->stages[SCHRO_DECODER_STAGE_MOTION_RENDER].is_done
1647         && render_ok) {
1648       /* Eagerly unreference the ref picures.  Otherwise they are kept
1649        * until the picture dependency chain terminates (worst case, Ponly
1650        * coding = infinite dependency chain = -ENOMEM) */
1651       if (picture->ref0) {
1652         schro_picture_unref (picture->ref0);
1653         picture->ref0 = NULL;
1654       }
1655       if (picture->ref1) {
1656         schro_picture_unref (picture->ref1);
1657         picture->ref1 = NULL;
1658       }
1659       if (!picture->output_picture) {
1660         schro_decoder_assign_output_picture (picture, i);
1661       }
1662       if (picture->output_picture) {
1663         func = schro_decoder_x_combine;
1664         stage = SCHRO_DECODER_STAGE_COMBINE;
1665       }
1666     }
1667 
1668     if (func) {
1669       picture->busy = TRUE;
1670 
1671       schro_picture_ref (picture);
1672 
1673       picture->stages[stage].task_func = func;
1674       picture->stages[stage].priv = picture;
1675       schro_async_run_stage_locked (decoder->async, picture->stages + stage);
1676 
1677       return TRUE;
1678     }
1679   }
1680 
1681   return FALSE;
1682 }
1683 
1684 void
schro_decoder_x_decode_motion(SchroAsyncStage * stage)1685 schro_decoder_x_decode_motion (SchroAsyncStage * stage)
1686 {
1687   SchroPicture *picture = (SchroPicture *) stage->priv;
1688   SchroParams *params = &picture->params;
1689 
1690   if (params->num_refs > 0) {
1691     picture->motion = schro_motion_new (params, picture->ref0->upsampled_frame,
1692         picture->ref1 ? picture->ref1->upsampled_frame : NULL);
1693     schro_decoder_decode_block_data (picture);
1694   }
1695 }
1696 
1697 void
schro_decoder_x_render_motion(SchroAsyncStage * stage)1698 schro_decoder_x_render_motion (SchroAsyncStage * stage)
1699 {
1700   SchroPicture *picture = (SchroPicture *) stage->priv;
1701   SchroParams *params = &picture->params;
1702   SchroDecoder *decoder = picture->decoder_instance->decoder;
1703 
1704   if (1) {
1705     SchroFrameFormat frame_format;
1706 
1707     frame_format = schro_params_get_frame_format (8,
1708         params->video_format->chroma_format);
1709 
1710     if (decoder->use_cuda) {
1711 #ifdef HAVE_CUDA
1712       picture->ref_output_frame =
1713           schro_frame_new_and_alloc (decoder->cuda_domain, frame_format,
1714           picture->decoder_instance->video_format.width,
1715           schro_video_format_get_picture_height (&picture->
1716               decoder_instance->video_format));
1717 #else
1718       SCHRO_ASSERT (0);
1719 #endif
1720     } else if (decoder->use_opengl) {
1721 #ifdef HAVE_OPENGL
1722       picture->ref_output_frame =
1723           schro_opengl_frame_new (decoder->opengl, decoder->opengl_domain,
1724           frame_format, picture->decoder_instance->video_format.width,
1725           schro_video_format_get_picture_height (&picture->decoder_instance->video_format));
1726 #else
1727       SCHRO_ASSERT (0);
1728 #endif
1729     } else {
1730       if (params->num_refs > 0 || picture->is_ref || picture->has_md5) {
1731         picture->ref_output_frame =
1732             schro_frame_new_and_alloc_full (decoder->cpu_domain,
1733             frame_format, picture->decoder_instance->video_format.width,
1734             schro_video_format_get_picture_height (&picture->
1735                 decoder_instance->video_format), 32, TRUE);
1736       }
1737     }
1738   }
1739 
1740   if (params->num_refs > 0) {
1741     SCHRO_DEBUG ("motion render with %p and %p", picture->ref0, picture->ref1);
1742     if (decoder->use_cuda) {
1743 #ifdef HAVE_CUDA
1744       int frame_width;
1745       int frame_height;
1746       int frame_format;
1747       SchroVideoFormat *video_format = params->video_format;
1748 
1749       frame_format = schro_params_get_frame_format (16,
1750           video_format->chroma_format);
1751       frame_width = ROUND_UP_POW2 (video_format->width,
1752           SCHRO_LIMIT_TRANSFORM_DEPTH +
1753           SCHRO_CHROMA_FORMAT_H_SHIFT (video_format->chroma_format));
1754       frame_height = ROUND_UP_POW2 (video_format->height,
1755           SCHRO_LIMIT_TRANSFORM_DEPTH +
1756           SCHRO_CHROMA_FORMAT_V_SHIFT (video_format->chroma_format));
1757 
1758       picture->mc_tmp_frame = schro_frame_new_and_alloc (decoder->cuda_domain,
1759           frame_format, frame_width, frame_height);
1760       schro_motion_render_cuda (picture->motion, picture->mc_tmp_frame);
1761 #else
1762       SCHRO_ASSERT (0);
1763 #endif
1764     } else if (decoder->use_opengl) {
1765 #ifdef HAVE_OPENGL
1766       SchroFrameFormat frame_format;
1767       int picture_width;
1768       int picture_height;
1769 
1770       frame_format = schro_params_get_frame_format (16,
1771           params->video_format->chroma_format);
1772       picture_width = params->video_format->width;
1773       picture_height =
1774           schro_video_format_get_picture_height (params->video_format);
1775 
1776       picture->mc_tmp_frame = schro_opengl_frame_new (decoder->opengl,
1777           decoder->opengl_domain, frame_format, picture_width, picture_height);
1778 
1779       schro_opengl_motion_render (picture->motion, picture->mc_tmp_frame);
1780 #else
1781       SCHRO_ASSERT (0);
1782 #endif
1783     } else {
1784       schro_motion_render (picture->motion, picture->mc_tmp_frame,
1785           picture->frame, TRUE, picture->ref_output_frame);
1786     }
1787   } else {
1788     if (params->num_refs > 0 || picture->is_ref || picture->has_md5) {
1789       schro_frame_convert (picture->ref_output_frame, picture->frame);
1790     }
1791   }
1792 }
1793 
1794 void
schro_decoder_x_decode_residual(SchroAsyncStage * stage)1795 schro_decoder_x_decode_residual (SchroAsyncStage * stage)
1796 {
1797   SchroPicture *picture = (SchroPicture *) stage->priv;
1798   SchroParams *params = &picture->params;
1799 
1800   if (!picture->zero_residual) {
1801     if (params->is_lowdelay) {
1802       schro_decoder_decode_lowdelay_transform_data (picture);
1803     } else {
1804       schro_decoder_decode_transform_data (picture);
1805     }
1806   }
1807 }
1808 
1809 void
schro_decoder_inverse_iwt_transform(SchroFrame * frame,SchroParams * params)1810 schro_decoder_inverse_iwt_transform (SchroFrame * frame, SchroParams * params)
1811 {
1812   int width;
1813   int height;
1814   int level;
1815   int component;
1816   int16_t *tmp;
1817 
1818   tmp = schro_malloc (sizeof (int32_t) * (params->iwt_luma_width + 16));
1819 
1820   for (component = 0; component < 3; component++) {
1821     SchroFrameData *comp = &frame->components[component];
1822 
1823     if (component == 0) {
1824       width = params->iwt_luma_width;
1825       height = params->iwt_luma_height;
1826     } else {
1827       width = params->iwt_chroma_width;
1828       height = params->iwt_chroma_height;
1829     }
1830 
1831     for (level = params->transform_depth - 1; level >= 0; level--) {
1832       SchroFrameData fd_dest;
1833       SchroFrameData fd_src;
1834 
1835       fd_src.format = frame->format;
1836       fd_src.data = comp->data;
1837       fd_src.width = width >> level;
1838       fd_src.height = height >> level;
1839       fd_src.stride = comp->stride << level;
1840 
1841       fd_dest.format = frame->format;
1842       fd_dest.data = comp->data;
1843       fd_dest.width = width >> level;
1844       fd_dest.height = height >> level;
1845       fd_dest.stride = comp->stride << level;
1846 
1847       schro_wavelet_inverse_transform_2d (&fd_dest, &fd_src,
1848           params->wavelet_filter_index, tmp);
1849     }
1850   }
1851 
1852   schro_free (tmp);
1853 }
1854 
1855 void
schro_decoder_x_wavelet_transform(SchroAsyncStage * stage)1856 schro_decoder_x_wavelet_transform (SchroAsyncStage * stage)
1857 {
1858   SchroPicture *picture = (SchroPicture *) stage->priv;
1859   SchroDecoder *decoder = picture->decoder_instance->decoder;
1860 
1861   if (!picture->zero_residual) {
1862     if (decoder->use_cuda) {
1863       picture->frame = schro_frame_clone (decoder->cuda_domain,
1864           picture->transform_frame);
1865 #ifdef HAVE_CUDA
1866       schro_frame_inverse_iwt_transform_cuda (picture->frame,
1867           picture->transform_frame, &picture->params);
1868 #else
1869       SCHRO_ASSERT (0);
1870 #endif
1871     } else if (decoder->use_opengl) {
1872 #ifdef HAVE_OPENGL
1873       picture->frame
1874           = schro_opengl_frame_clone_and_push (decoder->opengl,
1875           decoder->opengl_domain, picture->transform_frame);
1876 
1877       schro_opengl_frame_inverse_iwt_transform (picture->frame,
1878           &picture->params);
1879 #else
1880       SCHRO_ASSERT (0);
1881 #endif
1882     } else {
1883       schro_decoder_inverse_iwt_transform (picture->frame, &picture->params);
1884     }
1885   }
1886 }
1887 
1888 void
schro_decoder_x_combine(SchroAsyncStage * stage)1889 schro_decoder_x_combine (SchroAsyncStage * stage)
1890 {
1891   SchroPicture *picture = (SchroPicture *) stage->priv;
1892   SchroParams *params = &picture->params;
1893   SchroDecoder *decoder = picture->decoder_instance->decoder;
1894 #if defined(HAVE_CUDA) || defined(HAVE_OPENGL)
1895   SchroFrame *combined_frame;
1896   SchroFrame *output_frame;
1897 #endif
1898   /*> output_picture: a view of picture->output_picture, that is modified
1899    * when field coding.  Be very careful referencing this, no reference to
1900    * output_picture may leave this function */
1901   SchroFrame output_picture = *picture->output_picture;
1902 
1903 #if defined(HAVE_CUDA) || defined(HAVE_OPENGL)
1904   if (picture->zero_residual) {
1905     combined_frame = picture->mc_tmp_frame;
1906   } else {
1907     if (params->num_refs > 0) {
1908       if (decoder->use_cuda) {
1909 #ifdef HAVE_CUDA
1910         schro_gpuframe_add (picture->frame, picture->mc_tmp_frame);
1911 #else
1912         SCHRO_ASSERT (0);
1913 #endif
1914       } else if (decoder->use_opengl) {
1915 #ifdef HAVE_OPENGL
1916         schro_opengl_frame_add (picture->frame, picture->mc_tmp_frame);
1917 #else
1918         SCHRO_ASSERT (0);
1919 #endif
1920       } else {
1921         //schro_frame_add (picture->frame, picture->mc_tmp_frame);
1922       }
1923     }
1924     combined_frame = picture->frame;
1925   }
1926 
1927   if (_schro_decode_prediction_only) {
1928     if (params->num_refs > 0 && !picture->is_ref) {
1929       output_frame = picture->mc_tmp_frame;
1930     } else {
1931       output_frame = combined_frame;
1932     }
1933   } else {
1934     output_frame = combined_frame;
1935   }
1936 #endif
1937 
1938   if (picture->decoder_instance->video_format.interlaced_coding &&
1939       schro_decoder_frame_is_twofield (picture->decoder_instance,
1940           &output_picture)) {
1941     /* output_picture is a frame, however the [planar_]output_frame only
1942      * contains a field.  Create a view of the output_picture that corresponds
1943      * to the correct field */
1944     /* 10.4p3 earliest field in each frame shall have an even picture number:
1945      *   PicNum&1 | top_field_first=1 | top_field_first=0
1946      *   ---------+-------------------+------------------
1947      *        0   |       top         |       bot
1948      *        1   |       bot         |       top*/
1949     if ((picture->picture_number & 1) ==
1950         picture->decoder_instance->video_format.top_field_first) {
1951       /* to access bot_field, data pointers for first line need moving */
1952       output_picture.components[0].data =
1953           SCHRO_FRAME_DATA_GET_LINE (&output_picture.components[0], 1);
1954       output_picture.components[1].data =
1955           SCHRO_FRAME_DATA_GET_LINE (&output_picture.components[1], 1);
1956       output_picture.components[2].data =
1957           SCHRO_FRAME_DATA_GET_LINE (&output_picture.components[2], 1);
1958     }
1959     /* skip every other line and half heights */
1960     output_picture.components[0].stride *= 2;
1961     output_picture.components[1].stride *= 2;
1962     output_picture.components[2].stride *= 2;
1963     output_picture.components[0].height /= 2;
1964     output_picture.components[1].height /= 2;
1965     output_picture.components[2].height /= 2;
1966     output_picture.height /= 2;
1967   }
1968 
1969   if (SCHRO_FRAME_IS_PACKED (picture->output_picture->format)) {
1970     if (decoder->use_cuda) {
1971 #ifdef HAVE_CUDA
1972       SchroFrame *cuda_output_frame;
1973       SchroFrame *planar_output_frame;
1974       planar_output_frame = schro_frame_new_and_alloc (decoder->cpu_domain,
1975           schro_params_get_frame_format (8,
1976               picture->decoder_instance->video_format.chroma_format),
1977           picture->decoder_instance->video_format.width,
1978           picture->decoder_instance->video_format.height);
1979       cuda_output_frame =
1980           schro_frame_clone (decoder->cuda_domain, &output_picture);
1981       schro_gpuframe_convert (planar_output_frame, output_frame);
1982       schro_gpuframe_convert (cuda_output_frame, planar_output_frame);
1983       schro_gpuframe_to_cpu (&output_picture, cuda_output_frame);
1984       schro_frame_unref (cuda_output_frame);
1985       cuda_output_frame = NULL;
1986       schro_frame_unref (planar_output_frame);
1987 #else
1988       SCHRO_ASSERT (0);
1989 #endif
1990     } else if (decoder->use_opengl) {
1991 #ifdef HAVE_OPENGL
1992       SchroFrame *tmp_opengl_output_frame;
1993 
1994       tmp_opengl_output_frame = schro_opengl_frame_new (decoder->opengl,
1995           decoder->opengl_domain, picture->planar_output_frame->format,
1996           picture->planar_output_frame->width,
1997           picture->planar_output_frame->height);
1998 
1999       schro_opengl_frame_convert (tmp_opengl_output_frame, output_frame);
2000       schro_opengl_frame_pull (picture->planar_output_frame,
2001           tmp_opengl_output_frame);
2002       schro_frame_unref (tmp_opengl_output_frame);
2003       tmp_opengl_output_frame = NULL;
2004 
2005       schro_frame_convert (&output_picture, picture->planar_output_frame);
2006 #else
2007       SCHRO_ASSERT (0);
2008 #endif
2009     } else {
2010       if (params->num_refs > 0 || picture->is_ref) {
2011         schro_frame_convert (&output_picture, picture->ref_output_frame);
2012       } else {
2013         int shift;
2014         shift = picture->decoder_instance->bit_depth -
2015           schro_frame_get_bit_depth (&output_picture);
2016         if (shift != 0) {
2017           schro_frame_shift_right (picture->frame, shift);
2018         }
2019         schro_frame_convert (&output_picture, picture->frame);
2020       }
2021     }
2022   } else {
2023     if (decoder->use_cuda) {
2024 #ifdef HAVE_CUDA
2025       SchroFrame *cuda_output_frame;
2026       cuda_output_frame = schro_frame_clone (decoder->cuda_domain,
2027           &output_picture);
2028       schro_gpuframe_convert (cuda_output_frame, output_frame);
2029       schro_gpuframe_to_cpu (&output_picture, cuda_output_frame);
2030       schro_frame_unref (cuda_output_frame);
2031       cuda_output_frame = NULL;
2032 #else
2033       SCHRO_ASSERT (0);
2034 #endif
2035     } else if (decoder->use_opengl) {
2036 #ifdef HAVE_OPENGL
2037       SchroFrame *tmp_opengl_output_frame;
2038 
2039       tmp_opengl_output_frame = schro_opengl_frame_new (decoder->opengl,
2040           decoder->opengl_domain, output_picture.format,
2041           output_picture.width, output_picture.height);
2042 
2043       schro_opengl_frame_convert (tmp_opengl_output_frame, output_frame);
2044       schro_opengl_frame_pull (&output_picture, tmp_opengl_output_frame);
2045       schro_frame_unref (tmp_opengl_output_frame);
2046       tmp_opengl_output_frame = NULL;
2047 #else
2048       SCHRO_ASSERT (0);
2049 #endif
2050     } else {
2051       if (params->num_refs > 0 || picture->is_ref) {
2052         schro_frame_convert (&output_picture, picture->ref_output_frame);
2053       } else {
2054         int shift;
2055         shift = picture->decoder_instance->bit_depth -
2056           schro_frame_get_bit_depth (&output_picture);
2057         if (shift != 0) {
2058           schro_frame_shift_right (picture->frame, shift);
2059         }
2060         schro_frame_convert (&output_picture, picture->frame);
2061       }
2062       if (_schro_telemetry) {
2063         schro_decoder_telemetry (picture, &output_picture);
2064       }
2065     }
2066   }
2067 
2068   if (picture->is_ref) {
2069     SchroFrame *ref;
2070 
2071     ref = schro_frame_ref (picture->ref_output_frame);
2072     if (decoder->use_cuda) {
2073 #ifdef HAVE_CUDA
2074       schro_gpuframe_convert (ref, combined_frame);
2075 #else
2076       SCHRO_ASSERT (0);
2077 #endif
2078     } else if (decoder->use_opengl) {
2079 #ifdef HAVE_OPENGL
2080       schro_opengl_frame_convert (ref, combined_frame);
2081 #else
2082       SCHRO_ASSERT (0);
2083 #endif
2084     } else {
2085       schro_frame_mc_edgeextend (ref);
2086     }
2087     picture->upsampled_frame = schro_upsampled_frame_new (ref);
2088   }
2089 
2090   if (picture->has_md5) {
2091     uint32_t state[4];
2092 
2093     schro_frame_md5 (picture->ref_output_frame, state);
2094     if (memcmp (state, picture->md5_checksum, 16) != 0) {
2095       char a[33];
2096       char b[33];
2097       int i;
2098 
2099       for (i = 0; i < 16; i++) {
2100         sprintf (a + 2 * i, "%02x", ((uint8_t *) state)[i]);
2101         sprintf (b + 2 * i, "%02x", picture->md5_checksum[i]);
2102       }
2103       a[32] = 0;
2104       b[32] = 0;
2105       SCHRO_ERROR ("MD5 checksum mismatch (%s should be %s)", a, b);
2106     }
2107   }
2108 
2109   /* eagerly unreference any storage that is nolonger required */
2110   if (picture->mc_tmp_frame) {
2111     schro_frame_unref (picture->mc_tmp_frame);
2112     picture->mc_tmp_frame = NULL;
2113   }
2114   schro_frame_unref (picture->transform_frame);
2115   picture->transform_frame = NULL;
2116   schro_frame_unref (picture->frame);
2117   picture->frame = NULL;
2118 }
2119 
2120 void
schro_decoder_x_upsample(SchroAsyncStage * stage)2121 schro_decoder_x_upsample (SchroAsyncStage * stage)
2122 {
2123   SchroPicture *picture = (SchroPicture *) stage->priv;
2124   SchroDecoder *decoder = picture->decoder_instance->decoder;
2125 
2126   if (decoder->use_cuda) {
2127 #ifdef HAVE_CUDA
2128     schro_upsampled_gpuframe_upsample (picture->upsampled_frame);
2129 #else
2130     SCHRO_ASSERT (0);
2131 #endif
2132   } else if (decoder->use_opengl) {
2133 #ifdef HAVE_OPENGL
2134     schro_opengl_upsampled_frame_upsample (picture->upsampled_frame);
2135 #else
2136     SCHRO_ASSERT (0);
2137 #endif
2138   } else {
2139     schro_upsampled_frame_upsample (picture->upsampled_frame);
2140   }
2141 }
2142 
2143 int
schro_decoder_decode_parse_header(SchroUnpack * unpack)2144 schro_decoder_decode_parse_header (SchroUnpack * unpack)
2145 {
2146   int v1, v2, v3, v4;
2147   int parse_code;
2148 
2149   v1 = schro_unpack_decode_bits (unpack, 8);
2150   v2 = schro_unpack_decode_bits (unpack, 8);
2151   v3 = schro_unpack_decode_bits (unpack, 8);
2152   v4 = schro_unpack_decode_bits (unpack, 8);
2153   SCHRO_DEBUG ("parse header %02x %02x %02x %02x", v1, v2, v3, v4);
2154   if (v1 != 'B' || v2 != 'B' || v3 != 'C' || v4 != 'D') {
2155     SCHRO_ERROR ("expected parse header");
2156     return -1;
2157   }
2158 
2159   parse_code = schro_unpack_decode_bits (unpack, 8);
2160   SCHRO_DEBUG ("parse code %02x", parse_code);
2161 
2162   v1 = schro_unpack_decode_bits (unpack, 32);
2163   SCHRO_DEBUG ("next_parse_offset %d", v1);
2164   v1 = schro_unpack_decode_bits (unpack, 32);
2165   SCHRO_DEBUG ("prev_parse_offset %d", v1);
2166 
2167   return parse_code;
2168 }
2169 
2170 static int
schro_decoder_check_version(int major,int minor)2171 schro_decoder_check_version (int major, int minor)
2172 {
2173   if (major == 0 && minor == 20071203)
2174     return TRUE;
2175   if (major == 1 && minor == 0)
2176     return TRUE;
2177   if (major == 2 && minor == 0)
2178     return TRUE;
2179   if (major == 2 && minor == 1)
2180     return TRUE;
2181   if (major == 2 && minor == 2)
2182     return TRUE;
2183 
2184   return FALSE;
2185 }
2186 
2187 int
schro_decoder_compare_sequence_header_buffer(SchroBuffer * a,SchroBuffer * b)2188 schro_decoder_compare_sequence_header_buffer (SchroBuffer * a, SchroBuffer * b)
2189 {
2190   return TRUE;
2191 
2192   /* FIXME */
2193   if (a->length != b->length) {
2194     SCHRO_ERROR ("length different %d vs %d", a->length, b->length);
2195     return FALSE;
2196   }
2197   if (a->length < 13)
2198     return FALSE;
2199 
2200   {
2201     int i;
2202     for (i = 13; i < a->length; i++) {
2203       SCHRO_ERROR ("seq headers: %02x %02x", a->data[i], b->data[i]);
2204     }
2205   }
2206 
2207   if (memcmp (a->data + 13, b->data + 13, a->length - 13) != 0)
2208     return FALSE;
2209 
2210   return TRUE;
2211 }
2212 
2213 void
schro_decoder_parse_sequence_header(SchroDecoderInstance * instance,SchroUnpack * unpack)2214 schro_decoder_parse_sequence_header (SchroDecoderInstance * instance,
2215     SchroUnpack * unpack)
2216 {
2217   int bit;
2218   int index;
2219   SchroVideoFormat *format = &instance->video_format;
2220 
2221   SCHRO_DEBUG ("decoding sequence header");
2222 
2223   /* parse parameters */
2224   instance->major_version = schro_unpack_decode_uint (unpack);
2225   SCHRO_DEBUG ("major_version = %d", instance->major_version);
2226   instance->minor_version = schro_unpack_decode_uint (unpack);
2227   SCHRO_DEBUG ("minor_version = %d", instance->minor_version);
2228   instance->profile = schro_unpack_decode_uint (unpack);
2229   SCHRO_DEBUG ("profile = %d", instance->profile);
2230   instance->level = schro_unpack_decode_uint (unpack);
2231   SCHRO_DEBUG ("level = %d", instance->level);
2232 
2233   if (!schro_decoder_check_version (instance->major_version,
2234           instance->minor_version)) {
2235     SCHRO_WARNING
2236         ("Stream version number %d:%d not handled, expecting 0:20071203, 1:0, 2:0, 2:1, or 2:2",
2237         instance->major_version, instance->minor_version);
2238   }
2239 
2240   /* base video format */
2241   index = schro_unpack_decode_uint (unpack);
2242   schro_video_format_set_std_video_format (format, index);
2243 
2244   /* source parameters */
2245   /* frame dimensions */
2246   bit = schro_unpack_decode_bit (unpack);
2247   if (bit) {
2248     format->width = schro_unpack_decode_uint (unpack);
2249     format->height = schro_unpack_decode_uint (unpack);
2250   }
2251   SCHRO_DEBUG ("size = %d x %d", format->width, format->height);
2252 
2253   /* chroma format */
2254   bit = schro_unpack_decode_bit (unpack);
2255   if (bit) {
2256     format->chroma_format = schro_unpack_decode_uint (unpack);
2257   }
2258   SCHRO_DEBUG ("chroma_format %d", format->chroma_format);
2259 
2260   /* scan format */
2261   bit = schro_unpack_decode_bit (unpack);
2262   if (bit) {
2263     format->interlaced = schro_unpack_decode_uint (unpack);
2264   }
2265   SCHRO_DEBUG ("interlaced %d top_field_first %d",
2266       format->interlaced, format->top_field_first);
2267 
2268   MARKER ();
2269 
2270   /* frame rate */
2271   bit = schro_unpack_decode_bit (unpack);
2272   if (bit) {
2273     index = schro_unpack_decode_uint (unpack);
2274     if (index == 0) {
2275       format->frame_rate_numerator = schro_unpack_decode_uint (unpack);
2276       format->frame_rate_denominator = schro_unpack_decode_uint (unpack);
2277     } else {
2278       schro_video_format_set_std_frame_rate (format, index);
2279     }
2280   }
2281   SCHRO_DEBUG ("frame rate %d/%d", format->frame_rate_numerator,
2282       format->frame_rate_denominator);
2283 
2284   MARKER ();
2285 
2286   /* aspect ratio */
2287   bit = schro_unpack_decode_bit (unpack);
2288   if (bit) {
2289     index = schro_unpack_decode_uint (unpack);
2290     if (index == 0) {
2291       format->aspect_ratio_numerator = schro_unpack_decode_uint (unpack);
2292       format->aspect_ratio_denominator = schro_unpack_decode_uint (unpack);
2293     } else {
2294       schro_video_format_set_std_aspect_ratio (format, index);
2295     }
2296   }
2297   SCHRO_DEBUG ("aspect ratio %d/%d", format->aspect_ratio_numerator,
2298       format->aspect_ratio_denominator);
2299 
2300   MARKER ();
2301 
2302   /* clean area */
2303   bit = schro_unpack_decode_bit (unpack);
2304   if (bit) {
2305     format->clean_width = schro_unpack_decode_uint (unpack);
2306     format->clean_height = schro_unpack_decode_uint (unpack);
2307     format->left_offset = schro_unpack_decode_uint (unpack);
2308     format->top_offset = schro_unpack_decode_uint (unpack);
2309   }
2310   SCHRO_DEBUG ("clean offset %d %d", format->left_offset, format->top_offset);
2311   SCHRO_DEBUG ("clean size %d %d", format->clean_width, format->clean_height);
2312 
2313   MARKER ();
2314 
2315   /* signal range */
2316   bit = schro_unpack_decode_bit (unpack);
2317   if (bit) {
2318     index = schro_unpack_decode_uint (unpack);
2319     if (index == 0) {
2320       format->luma_offset = schro_unpack_decode_uint (unpack);
2321       format->luma_excursion = schro_unpack_decode_uint (unpack);
2322       format->chroma_offset = schro_unpack_decode_uint (unpack);
2323       format->chroma_excursion = schro_unpack_decode_uint (unpack);
2324     } else {
2325       if (index <= SCHRO_SIGNAL_RANGE_12BIT_VIDEO) {
2326         schro_video_format_set_std_signal_range (format, index);
2327       } else {
2328         schro_decoder_error (instance->decoder,
2329             "signal range index out of range");
2330       }
2331     }
2332   }
2333   SCHRO_DEBUG ("luma offset %d excursion %d", format->luma_offset,
2334       format->luma_excursion);
2335   SCHRO_DEBUG ("chroma offset %d excursion %d", format->chroma_offset,
2336       format->chroma_excursion);
2337 
2338   MARKER ();
2339 
2340   /* colour spec */
2341   bit = schro_unpack_decode_bit (unpack);
2342   if (bit) {
2343     index = schro_unpack_decode_uint (unpack);
2344     if (index <= SCHRO_COLOUR_SPEC_CINEMA) {
2345       schro_video_format_set_std_colour_spec (format, index);
2346     } else {
2347       schro_decoder_error (instance->decoder, "colour spec index out of range");
2348     }
2349     if (index == 0) {
2350       /* colour primaries */
2351       bit = schro_unpack_decode_bit (unpack);
2352       if (bit) {
2353         format->colour_primaries = schro_unpack_decode_uint (unpack);
2354       }
2355       /* colour matrix */
2356       bit = schro_unpack_decode_bit (unpack);
2357       if (bit) {
2358         format->colour_matrix = schro_unpack_decode_uint (unpack);
2359       }
2360       /* transfer function */
2361       bit = schro_unpack_decode_bit (unpack);
2362       if (bit) {
2363         format->transfer_function = schro_unpack_decode_uint (unpack);
2364       }
2365     }
2366   }
2367 
2368   format->interlaced_coding = schro_unpack_decode_uint (unpack);
2369 
2370   MARKER ();
2371 
2372   schro_video_format_validate (format);
2373 }
2374 
2375 void
schro_decoder_parse_picture_header(SchroPicture * picture,SchroUnpack * unpack)2376 schro_decoder_parse_picture_header (SchroPicture * picture,
2377     SchroUnpack * unpack)
2378 {
2379   SchroParams *params = &picture->params;
2380 
2381   schro_unpack_byte_sync (unpack);
2382 
2383   picture->picture_number = schro_unpack_decode_bits (unpack, 32);
2384   SCHRO_DEBUG ("picture number %d", picture->picture_number);
2385 
2386   if (params->num_refs > 0) {
2387     picture->reference1 = picture->picture_number +
2388         schro_unpack_decode_sint (unpack);
2389     SCHRO_DEBUG ("ref1 %d", picture->reference1);
2390   }
2391 
2392   if (params->num_refs > 1) {
2393     picture->reference2 = picture->picture_number +
2394         schro_unpack_decode_sint (unpack);
2395     SCHRO_DEBUG ("ref2 %d", picture->reference2);
2396   }
2397 
2398   if (picture->is_ref) {
2399     picture->retired_picture_number = picture->picture_number +
2400         schro_unpack_decode_sint (unpack);
2401   }
2402 }
2403 
2404 void
schro_decoder_parse_picture_prediction_parameters(SchroPicture * picture,SchroUnpack * unpack)2405 schro_decoder_parse_picture_prediction_parameters (SchroPicture * picture,
2406     SchroUnpack * unpack)
2407 {
2408   SchroParams *params = &picture->params;
2409   int bit;
2410   int index;
2411   int ret;
2412 
2413   /* block parameters */
2414   index = schro_unpack_decode_uint (unpack);
2415   if (index == 0) {
2416     params->xblen_luma = schro_unpack_decode_uint (unpack);
2417     params->yblen_luma = schro_unpack_decode_uint (unpack);
2418     params->xbsep_luma = schro_unpack_decode_uint (unpack);
2419     params->ybsep_luma = schro_unpack_decode_uint (unpack);
2420     if (!schro_params_verify_block_params (params))
2421       picture->error = TRUE;
2422   } else {
2423     ret = schro_params_set_block_params (params, index);
2424     if (!ret)
2425       picture->error = TRUE;
2426   }
2427   SCHRO_DEBUG ("blen_luma %d %d bsep_luma %d %d",
2428       params->xblen_luma, params->yblen_luma,
2429       params->xbsep_luma, params->ybsep_luma);
2430 
2431   MARKER ();
2432 
2433   /* mv precision */
2434   params->mv_precision = schro_unpack_decode_uint (unpack);
2435   SCHRO_DEBUG ("mv_precision %d", params->mv_precision);
2436   if (params->mv_precision > 3) {
2437     picture->error = TRUE;
2438   }
2439 
2440   MARKER ();
2441 
2442   /* global motion */
2443   params->have_global_motion = schro_unpack_decode_bit (unpack);
2444   if (params->have_global_motion) {
2445     int i;
2446 
2447     for (i = 0; i < params->num_refs; i++) {
2448       SchroGlobalMotion *gm = params->global_motion + i;
2449 
2450       /* pan/tilt */
2451       bit = schro_unpack_decode_bit (unpack);
2452       if (bit) {
2453         gm->b0 = schro_unpack_decode_sint (unpack);
2454         gm->b1 = schro_unpack_decode_sint (unpack);
2455       } else {
2456         gm->b0 = 0;
2457         gm->b1 = 0;
2458       }
2459 
2460       /* zoom/rotate/shear */
2461       bit = schro_unpack_decode_bit (unpack);
2462       if (bit) {
2463         gm->a_exp = schro_unpack_decode_uint (unpack);
2464         gm->a00 = schro_unpack_decode_sint (unpack);
2465         gm->a01 = schro_unpack_decode_sint (unpack);
2466         gm->a10 = schro_unpack_decode_sint (unpack);
2467         gm->a11 = schro_unpack_decode_sint (unpack);
2468       } else {
2469         gm->a_exp = 0;
2470         gm->a00 = 1;
2471         gm->a01 = 0;
2472         gm->a10 = 0;
2473         gm->a11 = 1;
2474       }
2475 
2476       /* perspective */
2477       bit = schro_unpack_decode_bit (unpack);
2478       if (bit) {
2479         gm->c_exp = schro_unpack_decode_uint (unpack);
2480         gm->c0 = schro_unpack_decode_sint (unpack);
2481         gm->c1 = schro_unpack_decode_sint (unpack);
2482       } else {
2483         gm->c_exp = 0;
2484         gm->c0 = 0;
2485         gm->c1 = 0;
2486       }
2487 
2488       SCHRO_DEBUG ("ref %d pan %d %d matrix %d %d %d %d perspective %d %d",
2489           i, gm->b0, gm->b1, gm->a00, gm->a01, gm->a10, gm->a11,
2490           gm->c0, gm->c1);
2491     }
2492   }
2493 
2494   MARKER ();
2495 
2496   /* picture prediction mode */
2497   params->picture_pred_mode = schro_unpack_decode_uint (unpack);
2498   if (params->picture_pred_mode != 0) {
2499     picture->error = TRUE;
2500   }
2501 
2502   /* reference picture weights */
2503   params->picture_weight_bits = 1;
2504   params->picture_weight_1 = 1;
2505   params->picture_weight_2 = 1;
2506   bit = schro_unpack_decode_bit (unpack);
2507   if (bit) {
2508     params->picture_weight_bits = schro_unpack_decode_uint (unpack);
2509     params->picture_weight_1 = schro_unpack_decode_sint (unpack);
2510     if (params->num_refs > 1) {
2511       params->picture_weight_2 = schro_unpack_decode_sint (unpack);
2512     }
2513   }
2514 
2515   MARKER ();
2516 }
2517 
2518 enum
2519 {
2520   SCHRO_DECODER_ARITH_SUPERBLOCK,
2521   SCHRO_DECODER_ARITH_PRED_MODE,
2522   SCHRO_DECODER_ARITH_VECTOR_REF1_X,
2523   SCHRO_DECODER_ARITH_VECTOR_REF1_Y,
2524   SCHRO_DECODER_ARITH_VECTOR_REF2_X,
2525   SCHRO_DECODER_ARITH_VECTOR_REF2_Y,
2526   SCHRO_DECODER_ARITH_DC_0,
2527   SCHRO_DECODER_ARITH_DC_1,
2528   SCHRO_DECODER_ARITH_DC_2
2529 };
2530 
2531 void
schro_decoder_parse_block_data(SchroPicture * picture,SchroUnpack * unpack)2532 schro_decoder_parse_block_data (SchroPicture * picture, SchroUnpack * unpack)
2533 {
2534   SchroParams *params = &picture->params;
2535   int i;
2536 
2537   for (i = 0; i < 9; i++) {
2538     int length;
2539 
2540     if (params->num_refs < 2 && (i == SCHRO_DECODER_ARITH_VECTOR_REF2_X ||
2541             i == SCHRO_DECODER_ARITH_VECTOR_REF2_Y)) {
2542       picture->motion_buffers[i] = NULL;
2543       continue;
2544     }
2545 
2546     length = schro_unpack_decode_uint (unpack);
2547     schro_unpack_byte_sync (unpack);
2548     picture->motion_buffers[i] =
2549         schro_buffer_new_subbuffer (picture->input_buffer,
2550         schro_unpack_get_bits_read (unpack) / 8, length);
2551     schro_unpack_skip_bits (unpack, length * 8);
2552   }
2553 }
2554 
2555 void
schro_decoder_decode_block_data(SchroPicture * picture)2556 schro_decoder_decode_block_data (SchroPicture * picture)
2557 {
2558   SchroParams *params = &picture->params;
2559   SchroArith *arith[9];
2560   SchroUnpack unpack[9];
2561   int i, j;
2562 
2563   orc_splat_u8_ns ((uint8_t *) picture->motion->motion_vectors, 0,
2564       sizeof (SchroMotionVector) * params->y_num_blocks * params->x_num_blocks);
2565 
2566   for (i = 0; i < 9; i++) {
2567     if (params->num_refs < 2 && (i == SCHRO_DECODER_ARITH_VECTOR_REF2_X ||
2568             i == SCHRO_DECODER_ARITH_VECTOR_REF2_Y)) {
2569       arith[i] = NULL;
2570       continue;
2571     }
2572 
2573     if (!params->is_noarith) {
2574       arith[i] = schro_arith_new ();
2575       schro_arith_decode_init (arith[i], picture->motion_buffers[i]);
2576     } else {
2577       schro_unpack_init_with_data (unpack + i,
2578           picture->motion_buffers[i]->data,
2579           picture->motion_buffers[i]->length, 1);
2580     }
2581   }
2582 
2583   for (j = 0; j < params->y_num_blocks; j += 4) {
2584     for (i = 0; i < params->x_num_blocks; i += 4) {
2585       schro_decoder_decode_macroblock (picture, arith, unpack, i, j);
2586     }
2587   }
2588 
2589   for (i = 0; i < 9; i++) {
2590     if (!params->is_noarith) {
2591       if (arith[i] == NULL)
2592         continue;
2593 
2594       if (arith[i]->offset < arith[i]->buffer->length) {
2595         SCHRO_DEBUG ("arith decoding %d didn't consume buffer (%d < %d)", i,
2596             arith[i]->offset, arith[i]->buffer->length);
2597       }
2598       if (arith[i]->offset > arith[i]->buffer->length + 6) {
2599         SCHRO_WARNING ("arith decoding %d overran buffer (%d > %d)", i,
2600             arith[i]->offset, arith[i]->buffer->length);
2601       }
2602       schro_arith_free (arith[i]);
2603     } else {
2604       /* FIXME complain about buffer over/underrun */
2605     }
2606   }
2607 
2608   schro_motion_verify (picture->motion);
2609 }
2610 
2611 void
schro_decoder_decode_macroblock(SchroPicture * picture,SchroArith ** arith,SchroUnpack * unpack,int i,int j)2612 schro_decoder_decode_macroblock (SchroPicture * picture, SchroArith ** arith,
2613     SchroUnpack * unpack, int i, int j)
2614 {
2615   SchroParams *params = &picture->params;
2616   SchroMotion *motion = picture->motion;
2617   SchroMotionVector *mv = &motion->motion_vectors[j * params->x_num_blocks + i];
2618   int k, l;
2619   int split_prediction;
2620 
2621   split_prediction = schro_motion_split_prediction (motion, i, j);
2622   if (!params->is_noarith) {
2623     mv->split = (split_prediction +
2624         _schro_arith_decode_uint (arith[SCHRO_DECODER_ARITH_SUPERBLOCK],
2625             SCHRO_CTX_SB_F1, SCHRO_CTX_SB_DATA)) % 3;
2626   } else {
2627     mv->split = (split_prediction +
2628         schro_unpack_decode_uint (unpack + SCHRO_DECODER_ARITH_SUPERBLOCK)) % 3;
2629   }
2630 
2631   switch (mv->split) {
2632     default:
2633       /* this can happen if _schro_arith_decode_uint() decodes a value
2634        * that overflows.  FIXME should cause a picture error.  For now,
2635        * just pretend it didn't happen.  */
2636       SCHRO_ERROR ("mv->split == %d, split_prediction %d", mv->split,
2637           split_prediction);
2638       /* fall through */
2639     case 0:
2640       schro_decoder_decode_prediction_unit (picture, arith, unpack,
2641           motion->motion_vectors, i, j);
2642       mv[1] = mv[0];
2643       mv[2] = mv[0];
2644       mv[3] = mv[0];
2645       memcpy (mv + params->x_num_blocks, mv, 4 * sizeof (*mv));
2646       memcpy (mv + 2 * params->x_num_blocks, mv, 4 * sizeof (*mv));
2647       memcpy (mv + 3 * params->x_num_blocks, mv, 4 * sizeof (*mv));
2648       break;
2649     case 1:
2650       schro_decoder_decode_prediction_unit (picture, arith, unpack,
2651           motion->motion_vectors, i, j);
2652       mv[1] = mv[0];
2653       schro_decoder_decode_prediction_unit (picture, arith, unpack,
2654           motion->motion_vectors, i + 2, j);
2655       mv[2].split = 1;
2656       mv[3] = mv[2];
2657       memcpy (mv + params->x_num_blocks, mv, 4 * sizeof (*mv));
2658 
2659       mv += 2 * params->x_num_blocks;
2660       schro_decoder_decode_prediction_unit (picture, arith, unpack,
2661           motion->motion_vectors, i, j + 2);
2662       mv[0].split = 1;
2663       mv[1] = mv[0];
2664       schro_decoder_decode_prediction_unit (picture, arith, unpack,
2665           motion->motion_vectors, i + 2, j + 2);
2666       mv[2].split = 1;
2667       mv[3] = mv[2];
2668       memcpy (mv + params->x_num_blocks, mv, 4 * sizeof (*mv));
2669       break;
2670     case 2:
2671       for (l = 0; l < 4; l++) {
2672         for (k = 0; k < 4; k++) {
2673           mv[l * params->x_num_blocks + k].split = 2;
2674           schro_decoder_decode_prediction_unit (picture, arith, unpack,
2675               motion->motion_vectors, i + k, j + l);
2676         }
2677       }
2678       break;
2679   }
2680 }
2681 
2682 void
schro_decoder_decode_prediction_unit(SchroPicture * picture,SchroArith ** arith,SchroUnpack * unpack,SchroMotionVector * motion_vectors,int x,int y)2683 schro_decoder_decode_prediction_unit (SchroPicture * picture,
2684     SchroArith ** arith, SchroUnpack * unpack,
2685     SchroMotionVector * motion_vectors, int x, int y)
2686 {
2687   SchroParams *params = &picture->params;
2688   SchroMotion *motion = picture->motion;
2689   SchroMotionVector *mv = &motion_vectors[y * params->x_num_blocks + x];
2690 
2691   mv->pred_mode = schro_motion_get_mode_prediction (motion, x, y);
2692   if (!params->is_noarith) {
2693     mv->pred_mode ^=
2694         _schro_arith_decode_bit (arith[SCHRO_DECODER_ARITH_PRED_MODE],
2695         SCHRO_CTX_BLOCK_MODE_REF1);
2696   } else {
2697     mv->pred_mode ^=
2698         schro_unpack_decode_bit (unpack + SCHRO_DECODER_ARITH_PRED_MODE);
2699   }
2700   if (params->num_refs > 1) {
2701     if (!params->is_noarith) {
2702       mv->pred_mode ^=
2703           _schro_arith_decode_bit (arith[SCHRO_DECODER_ARITH_PRED_MODE],
2704           SCHRO_CTX_BLOCK_MODE_REF2) << 1;
2705     } else {
2706       mv->pred_mode ^=
2707           schro_unpack_decode_bit (unpack + SCHRO_DECODER_ARITH_PRED_MODE) << 1;
2708     }
2709   }
2710 
2711   if (mv->pred_mode == 0) {
2712     int pred[3];
2713 
2714     schro_motion_dc_prediction (motion, x, y, pred);
2715 
2716     if (!params->is_noarith) {
2717       mv->u.dc.dc[0] =
2718           pred[0] + _schro_arith_decode_sint (arith[SCHRO_DECODER_ARITH_DC_0],
2719           SCHRO_CTX_LUMA_DC_CONT_BIN1, SCHRO_CTX_LUMA_DC_VALUE,
2720           SCHRO_CTX_LUMA_DC_SIGN);
2721       mv->u.dc.dc[1] =
2722           pred[1] + _schro_arith_decode_sint (arith[SCHRO_DECODER_ARITH_DC_1],
2723           SCHRO_CTX_CHROMA1_DC_CONT_BIN1, SCHRO_CTX_CHROMA1_DC_VALUE,
2724           SCHRO_CTX_CHROMA1_DC_SIGN);
2725       mv->u.dc.dc[2] =
2726           pred[2] + _schro_arith_decode_sint (arith[SCHRO_DECODER_ARITH_DC_2],
2727           SCHRO_CTX_CHROMA2_DC_CONT_BIN1, SCHRO_CTX_CHROMA2_DC_VALUE,
2728           SCHRO_CTX_CHROMA2_DC_SIGN);
2729     } else {
2730       mv->u.dc.dc[0] = pred[0] +
2731           schro_unpack_decode_sint (unpack + SCHRO_DECODER_ARITH_DC_0);
2732       mv->u.dc.dc[1] = pred[1] +
2733           schro_unpack_decode_sint (unpack + SCHRO_DECODER_ARITH_DC_1);
2734       mv->u.dc.dc[2] = pred[2] +
2735           schro_unpack_decode_sint (unpack + SCHRO_DECODER_ARITH_DC_2);
2736     }
2737   } else {
2738     int pred_x, pred_y;
2739 
2740     if (params->have_global_motion) {
2741       int pred;
2742       pred = schro_motion_get_global_prediction (motion, x, y);
2743       if (!params->is_noarith) {
2744         mv->using_global =
2745             pred ^
2746             _schro_arith_decode_bit (arith[SCHRO_DECODER_ARITH_PRED_MODE],
2747             SCHRO_CTX_GLOBAL_BLOCK);
2748       } else {
2749         mv->using_global =
2750             pred ^ schro_unpack_decode_bit (unpack +
2751             SCHRO_DECODER_ARITH_PRED_MODE);
2752       }
2753     } else {
2754       mv->using_global = FALSE;
2755     }
2756     if (!mv->using_global) {
2757       if (mv->pred_mode & 1) {
2758         schro_motion_vector_prediction (motion, x, y, &pred_x, &pred_y, 1);
2759 
2760         if (!params->is_noarith) {
2761           mv->u.vec.dx[0] =
2762               pred_x +
2763               _schro_arith_decode_sint (arith
2764               [SCHRO_DECODER_ARITH_VECTOR_REF1_X],
2765               SCHRO_CTX_MV_REF1_H_CONT_BIN1, SCHRO_CTX_MV_REF1_H_VALUE,
2766               SCHRO_CTX_MV_REF1_H_SIGN);
2767           mv->u.vec.dy[0] =
2768               pred_y +
2769               _schro_arith_decode_sint (arith
2770               [SCHRO_DECODER_ARITH_VECTOR_REF1_Y],
2771               SCHRO_CTX_MV_REF1_V_CONT_BIN1, SCHRO_CTX_MV_REF1_V_VALUE,
2772               SCHRO_CTX_MV_REF1_V_SIGN);
2773         } else {
2774           mv->u.vec.dx[0] =
2775               pred_x + schro_unpack_decode_sint (unpack +
2776               SCHRO_DECODER_ARITH_VECTOR_REF1_X);
2777           mv->u.vec.dy[0] =
2778               pred_y + schro_unpack_decode_sint (unpack +
2779               SCHRO_DECODER_ARITH_VECTOR_REF1_Y);
2780         }
2781       }
2782       if (mv->pred_mode & 2) {
2783         schro_motion_vector_prediction (motion, x, y, &pred_x, &pred_y, 2);
2784 
2785         if (!params->is_noarith) {
2786           mv->u.vec.dx[1] =
2787               pred_x +
2788               _schro_arith_decode_sint (arith
2789               [SCHRO_DECODER_ARITH_VECTOR_REF2_X],
2790               SCHRO_CTX_MV_REF2_H_CONT_BIN1, SCHRO_CTX_MV_REF2_H_VALUE,
2791               SCHRO_CTX_MV_REF2_H_SIGN);
2792           mv->u.vec.dy[1] =
2793               pred_y +
2794               _schro_arith_decode_sint (arith
2795               [SCHRO_DECODER_ARITH_VECTOR_REF2_Y],
2796               SCHRO_CTX_MV_REF2_V_CONT_BIN1, SCHRO_CTX_MV_REF2_V_VALUE,
2797               SCHRO_CTX_MV_REF2_V_SIGN);
2798         } else {
2799           mv->u.vec.dx[1] =
2800               pred_x + schro_unpack_decode_sint (unpack +
2801               SCHRO_DECODER_ARITH_VECTOR_REF2_X);
2802           mv->u.vec.dy[1] =
2803               pred_y + schro_unpack_decode_sint (unpack +
2804               SCHRO_DECODER_ARITH_VECTOR_REF2_Y);
2805         }
2806       }
2807     } else {
2808       mv->u.vec.dx[0] = 0;
2809       mv->u.vec.dy[0] = 0;
2810       mv->u.vec.dx[1] = 0;
2811       mv->u.vec.dy[1] = 0;
2812     }
2813   }
2814 }
2815 
2816 void
schro_decoder_parse_transform_parameters(SchroPicture * picture,SchroUnpack * unpack)2817 schro_decoder_parse_transform_parameters (SchroPicture * picture,
2818     SchroUnpack * unpack)
2819 {
2820   int bit;
2821   int i;
2822   SchroParams *params = &picture->params;
2823 
2824   /* transform */
2825   params->wavelet_filter_index = schro_unpack_decode_uint (unpack);
2826   SCHRO_DEBUG ("wavelet filter index %d", params->wavelet_filter_index);
2827 
2828   /* transform depth */
2829   params->transform_depth = schro_unpack_decode_uint (unpack);
2830   SCHRO_DEBUG ("transform depth %d", params->transform_depth);
2831   if (params->transform_depth > SCHRO_LIMIT_TRANSFORM_DEPTH) {
2832     picture->error = TRUE;
2833     return;
2834   }
2835 
2836   if (!params->is_lowdelay) {
2837     /* codeblock parameters */
2838     params->codeblock_mode_index = 0;
2839     for (i = 0; i < params->transform_depth + 1; i++) {
2840       params->horiz_codeblocks[i] = 1;
2841       params->vert_codeblocks[i] = 1;
2842     }
2843 
2844     bit = schro_unpack_decode_bit (unpack);
2845     if (bit) {
2846       for (i = 0; i < params->transform_depth + 1; i++) {
2847         params->horiz_codeblocks[i] = schro_unpack_decode_uint (unpack);
2848         params->vert_codeblocks[i] = schro_unpack_decode_uint (unpack);
2849       }
2850       params->codeblock_mode_index = schro_unpack_decode_uint (unpack);
2851     }
2852   } else {
2853     /* slice parameters */
2854     params->n_horiz_slices = schro_unpack_decode_uint (unpack);
2855     params->n_vert_slices = schro_unpack_decode_uint (unpack);
2856 
2857     params->slice_bytes_num = schro_unpack_decode_uint (unpack);
2858     params->slice_bytes_denom = schro_unpack_decode_uint (unpack);
2859 
2860     /* quant matrix */
2861     bit = schro_unpack_decode_bit (unpack);
2862     if (bit) {
2863       params->quant_matrix[0] = schro_unpack_decode_uint (unpack);
2864       for (i = 0; i < params->transform_depth; i++) {
2865         params->quant_matrix[1 + 3 * i] = schro_unpack_decode_uint (unpack);
2866         params->quant_matrix[2 + 3 * i] = schro_unpack_decode_uint (unpack);
2867         params->quant_matrix[3 + 3 * i] = schro_unpack_decode_uint (unpack);
2868       }
2869     } else {
2870       schro_params_set_default_quant_matrix (params);
2871     }
2872   }
2873 }
2874 
2875 void
schro_decoder_init_subband_frame_data_interleaved(SchroPicture * picture)2876 schro_decoder_init_subband_frame_data_interleaved (SchroPicture * picture)
2877 {
2878   int i;
2879   int component;
2880   SchroFrameData *fd;
2881   SchroParams *params = &picture->params;
2882   int position;
2883 
2884   if (picture->error)
2885     return;
2886 
2887   for (component = 0; component < 3; component++) {
2888     for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
2889       position = schro_subband_get_position (i);
2890 
2891       fd = &picture->subband_data[component][i];
2892 
2893       schro_subband_get_frame_data (fd, picture->transform_frame,
2894           component, position, params);
2895     }
2896   }
2897 }
2898 
2899 #ifdef HAVE_OPENGL
2900 void
schro_decoder_init_subband_frame_data(SchroPicture * picture)2901 schro_decoder_init_subband_frame_data (SchroPicture * picture)
2902 {
2903   int i;
2904   int component;
2905   SchroFrameData *fd;
2906   SchroParams *params = &picture->params;
2907   int position;
2908 
2909   if (picture->error)
2910     return;
2911 
2912   for (component = 0; component < 3; ++component) {
2913     for (i = 0; i < 1 + 3 * params->transform_depth; ++i) {
2914       position = schro_subband_get_position (i);
2915       fd = &picture->subband_data[component][i];
2916 
2917       schro_subband_get_frame_data (fd, picture->transform_frame,
2918           component, position, params);
2919     }
2920   }
2921 }
2922 #endif
2923 
2924 void
schro_decoder_parse_lowdelay_transform_data(SchroPicture * picture,SchroUnpack * unpack)2925 schro_decoder_parse_lowdelay_transform_data (SchroPicture * picture,
2926     SchroUnpack * unpack)
2927 {
2928   SchroParams *params = &picture->params;
2929   int length;
2930 
2931   length = (params->slice_bytes_num * params->n_horiz_slices *
2932       params->n_vert_slices) / params->slice_bytes_denom;
2933   picture->lowdelay_buffer = schro_buffer_new_subbuffer (picture->input_buffer,
2934       schro_unpack_get_bits_read (unpack) / 8, length);
2935   schro_unpack_skip_bits (unpack, length * 8);
2936 }
2937 
2938 void
schro_decoder_parse_transform_data(SchroPicture * picture,SchroUnpack * unpack)2939 schro_decoder_parse_transform_data (SchroPicture * picture,
2940     SchroUnpack * unpack)
2941 {
2942   int i;
2943   int component;
2944   SchroParams *params = &picture->params;
2945   int subband_length;
2946 
2947   if (picture->error)
2948     return;
2949 
2950   for (component = 0; component < 3; component++) {
2951     for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
2952       schro_unpack_byte_sync (unpack);
2953 
2954       subband_length = schro_unpack_decode_uint (unpack);
2955 
2956       SCHRO_DEBUG ("subband %d %d length %d", component, i, subband_length);
2957 
2958       if (subband_length == 0) {
2959         SCHRO_DEBUG ("subband is zero");
2960         schro_unpack_byte_sync (unpack);
2961 
2962         picture->subband_quant_index[component][i] = 0;
2963         picture->subband_length[component][i] = 0;
2964         picture->subband_buffer[component][i] = NULL;
2965       } else {
2966         int quant_index;
2967 
2968         quant_index = schro_unpack_decode_uint (unpack);
2969         SCHRO_DEBUG ("quant index %d", quant_index);
2970 
2971         if (quant_index < 0 || quant_index > 60) {
2972           picture->error = TRUE;
2973           return;
2974         }
2975 
2976         schro_unpack_byte_sync (unpack);
2977 
2978         picture->subband_quant_index[component][i] = quant_index;
2979         picture->subband_length[component][i] = subband_length;
2980         picture->subband_buffer[component][i] =
2981             schro_buffer_new_subbuffer (picture->input_buffer,
2982             schro_unpack_get_bits_read (unpack) / 8, subband_length);
2983         schro_unpack_skip_bits (unpack, subband_length * 8);
2984       }
2985     }
2986   }
2987 }
2988 
2989 void
schro_decoder_decode_transform_data(SchroPicture * picture)2990 schro_decoder_decode_transform_data (SchroPicture * picture)
2991 {
2992   int i;
2993   int component;
2994   SchroParams *params = &picture->params;
2995   SchroPictureSubbandContext context = { 0 }, *ctx = &context;
2996   int skip_subbands;
2997 
2998   if (_schro_decode_prediction_only && params->num_refs > 0 && !picture->is_ref) {
2999     schro_frame_clear (picture->frame);
3000     return;
3001   }
3002 
3003   /* FIXME some day, hook this up into automatic degraded decoding */
3004   skip_subbands = 0;
3005 
3006   for (component = 0; component < 3; component++) {
3007     for (i = 0; i < 1 + 3 * params->transform_depth - skip_subbands; i++) {
3008       ctx->component = component;
3009       ctx->index = i;
3010       ctx->position = schro_subband_get_position (i);
3011 
3012       schro_decoder_decode_subband (picture, ctx);
3013     }
3014   }
3015 }
3016 
3017 static void
codeblock_line_decode_generic(SchroPictureSubbandContext * ctx,int16_t * line,int j,const int16_t * parent_data,const int16_t * prev)3018 codeblock_line_decode_generic (SchroPictureSubbandContext * ctx,
3019     int16_t * line, int j, const int16_t * parent_data, const int16_t * prev)
3020 {
3021   int i;
3022 
3023   for (i = ctx->xmin; i < ctx->xmax; i++) {
3024     int v;
3025     int parent;
3026     int nhood_or;
3027     int previous_value;
3028 
3029     if (parent_data) {
3030       parent = parent_data[(i >> 1)];
3031     } else {
3032       parent = 0;
3033     }
3034 
3035     nhood_or = 0;
3036     if (j > 0)
3037       nhood_or |= prev[i];
3038     if (i > 0)
3039       nhood_or |= line[i - 1];
3040     if (i > 0 && j > 0)
3041       nhood_or |= prev[i - 1];
3042 
3043     previous_value = 0;
3044     if (SCHRO_SUBBAND_IS_HORIZONTALLY_ORIENTED (ctx->position)) {
3045       if (i > 0)
3046         previous_value = line[i - 1];
3047     } else if (SCHRO_SUBBAND_IS_VERTICALLY_ORIENTED (ctx->position)) {
3048       if (j > 0)
3049         previous_value = prev[i];
3050     }
3051 #define STUFF \
3052   do { \
3053     int cont_context, sign_context, value_context; \
3054     \
3055     if (parent == 0) { \
3056       cont_context = nhood_or ? SCHRO_CTX_ZPNN_F1 : SCHRO_CTX_ZPZN_F1; \
3057     } else { \
3058       cont_context = nhood_or ? SCHRO_CTX_NPNN_F1 : SCHRO_CTX_NPZN_F1; \
3059     } \
3060      \
3061     if (previous_value < 0) { \
3062       sign_context = SCHRO_CTX_SIGN_NEG; \
3063     } else { \
3064       sign_context = (previous_value > 0) ? SCHRO_CTX_SIGN_POS : \
3065         SCHRO_CTX_SIGN_ZERO; \
3066     } \
3067  \
3068     value_context = SCHRO_CTX_COEFF_DATA; \
3069  \
3070     v = _schro_arith_decode_uint (ctx->arith, cont_context, \
3071         value_context); \
3072     if (v) { \
3073       v = (ctx->quant_offset + ctx->quant_factor * v + 2)>>2; \
3074       if (_schro_arith_decode_bit (ctx->arith, sign_context)) { \
3075         v = -v; \
3076       } \
3077       line[i] = v; \
3078     } else { \
3079       line[i] = 0; \
3080     } \
3081   } while(0)
3082 
3083     STUFF;
3084   }
3085 }
3086 
3087 static void
codeblock_line_decode_generic_s32(SchroPictureSubbandContext * ctx,int32_t * line,int j,const int32_t * parent_data,const int32_t * prev)3088 codeblock_line_decode_generic_s32 (SchroPictureSubbandContext * ctx,
3089     int32_t * line, int j, const int32_t * parent_data, const int32_t * prev)
3090 {
3091   int i;
3092 
3093   for (i = ctx->xmin; i < ctx->xmax; i++) {
3094     int v;
3095     int parent;
3096     int nhood_or;
3097     int previous_value;
3098 
3099     if (parent_data) {
3100       parent = parent_data[(i >> 1)];
3101     } else {
3102       parent = 0;
3103     }
3104 
3105     nhood_or = 0;
3106     if (j > 0)
3107       nhood_or |= prev[i];
3108     if (i > 0)
3109       nhood_or |= line[i - 1];
3110     if (i > 0 && j > 0)
3111       nhood_or |= prev[i - 1];
3112 
3113     previous_value = 0;
3114     if (SCHRO_SUBBAND_IS_HORIZONTALLY_ORIENTED (ctx->position)) {
3115       if (i > 0)
3116         previous_value = line[i - 1];
3117     } else if (SCHRO_SUBBAND_IS_VERTICALLY_ORIENTED (ctx->position)) {
3118       if (j > 0)
3119         previous_value = prev[i];
3120     }
3121 
3122     STUFF;
3123   }
3124 }
3125 
3126 static void
codeblock_line_decode_p_horiz(SchroPictureSubbandContext * ctx,int16_t * line,int j,const int16_t * parent_data,const int16_t * prev)3127 codeblock_line_decode_p_horiz (SchroPictureSubbandContext * ctx,
3128     int16_t * line, int j, const int16_t * parent_data, const int16_t * prev)
3129 {
3130   int i = ctx->xmin;
3131   int v;
3132   int parent;
3133   int nhood_or;
3134   int previous_value;
3135 
3136   if (i == 0) {
3137     parent = parent_data[(i >> 1)];
3138     nhood_or = prev[i];
3139     previous_value = 0;
3140 
3141     STUFF;
3142     i++;
3143   }
3144   for (; i < ctx->xmax; i++) {
3145     parent = parent_data[(i >> 1)];
3146 
3147     nhood_or = prev[i];
3148     nhood_or |= line[i - 1];
3149     nhood_or |= prev[i - 1];
3150 
3151     previous_value = line[i - 1];
3152 
3153     STUFF;
3154   }
3155 }
3156 
3157 static void
codeblock_line_decode_p_vert(SchroPictureSubbandContext * ctx,int16_t * line,int j,const int16_t * parent_data,const int16_t * prev)3158 codeblock_line_decode_p_vert (SchroPictureSubbandContext * ctx,
3159     int16_t * line, int j, const int16_t * parent_data, const int16_t * prev)
3160 {
3161   int i = ctx->xmin;
3162   int v;
3163   int parent;
3164   int nhood_or;
3165   int previous_value;
3166 
3167   if (i == 0) {
3168     parent = parent_data[(i >> 1)];
3169     nhood_or = prev[i];
3170     previous_value = prev[i];
3171 
3172     STUFF;
3173     i++;
3174   }
3175   for (; i < ctx->xmax; i++) {
3176     parent = parent_data[(i >> 1)];
3177 
3178     nhood_or = prev[i];
3179     nhood_or |= line[i - 1];
3180     nhood_or |= prev[i - 1];
3181 
3182     previous_value = prev[i];
3183 
3184     STUFF;
3185   }
3186 }
3187 
3188 static void
codeblock_line_decode_p_diag(SchroPictureSubbandContext * ctx,int16_t * line,int j,const int16_t * parent_data,const int16_t * prev)3189 codeblock_line_decode_p_diag (SchroPictureSubbandContext * ctx,
3190     int16_t * line, int j, const int16_t * parent_data, const int16_t * prev)
3191 {
3192   int i;
3193   int v;
3194   int parent;
3195   int nhood_or;
3196   int previous_value;
3197 
3198   i = ctx->xmin;
3199   if (i == 0) {
3200     parent = parent_data[(i >> 1)];
3201     nhood_or = prev[i];
3202     previous_value = 0;
3203 
3204     STUFF;
3205     i++;
3206   }
3207   for (; i < ctx->xmax; i++) {
3208     parent = parent_data[(i >> 1)];
3209 
3210     nhood_or = prev[i];
3211     nhood_or |= line[i - 1];
3212     nhood_or |= prev[i - 1];
3213     previous_value = 0;
3214 
3215     STUFF;
3216   }
3217 }
3218 
3219 void
schro_decoder_subband_dc_predict(SchroFrameData * fd)3220 schro_decoder_subband_dc_predict (SchroFrameData * fd)
3221 {
3222   int16_t *prev_line;
3223   int16_t *line;
3224   int i, j;
3225   int pred_value;
3226 
3227   line = SCHRO_FRAME_DATA_GET_LINE (fd, 0);
3228   for (i = 1; i < fd->width; i++) {
3229     pred_value = line[i - 1];
3230     line[i] += pred_value;
3231   }
3232 
3233   for (j = 1; j < fd->height; j++) {
3234     line = SCHRO_FRAME_DATA_GET_LINE (fd, j);
3235     prev_line = SCHRO_FRAME_DATA_GET_LINE (fd, j - 1);
3236 
3237     pred_value = prev_line[0];
3238     line[0] += pred_value;
3239 
3240     for (i = 1; i < fd->width; i++) {
3241       pred_value = schro_divide3 (line[i - 1] + prev_line[i] +
3242           prev_line[i - 1] + 1);
3243       line[i] += pred_value;
3244     }
3245   }
3246 
3247 }
3248 
3249 void
schro_decoder_subband_dc_predict_s32(SchroFrameData * fd)3250 schro_decoder_subband_dc_predict_s32 (SchroFrameData * fd)
3251 {
3252   int32_t *prev_line;
3253   int32_t *line;
3254   int i, j;
3255   int pred_value;
3256 
3257   line = SCHRO_FRAME_DATA_GET_LINE (fd, 0);
3258   for (i = 1; i < fd->width; i++) {
3259     pred_value = line[i - 1];
3260     line[i] += pred_value;
3261   }
3262 
3263   for (j = 1; j < fd->height; j++) {
3264     line = SCHRO_FRAME_DATA_GET_LINE (fd, j);
3265     prev_line = SCHRO_FRAME_DATA_GET_LINE (fd, j - 1);
3266 
3267     pred_value = prev_line[0];
3268     line[0] += pred_value;
3269 
3270     for (i = 1; i < fd->width; i++) {
3271       pred_value = schro_divide (line[i - 1] + prev_line[i] +
3272           prev_line[i - 1] + 1, 3);
3273       line[i] += pred_value;
3274     }
3275   }
3276 
3277 }
3278 
3279 static void
schro_decoder_setup_codeblocks(SchroPicture * picture,SchroPictureSubbandContext * ctx)3280 schro_decoder_setup_codeblocks (SchroPicture * picture,
3281     SchroPictureSubbandContext * ctx)
3282 {
3283   SchroParams *params = &picture->params;
3284 
3285   if (ctx->position == 0) {
3286     ctx->vert_codeblocks = params->vert_codeblocks[0];
3287     ctx->horiz_codeblocks = params->horiz_codeblocks[0];
3288   } else {
3289     ctx->vert_codeblocks =
3290         params->vert_codeblocks[SCHRO_SUBBAND_SHIFT (ctx->position) + 1];
3291     ctx->horiz_codeblocks =
3292         params->horiz_codeblocks[SCHRO_SUBBAND_SHIFT (ctx->position) + 1];
3293   }
3294   if (ctx->horiz_codeblocks > 1 || ctx->vert_codeblocks > 1) {
3295     ctx->have_zero_flags = TRUE;
3296   } else {
3297     ctx->have_zero_flags = FALSE;
3298   }
3299   if (params->codeblock_mode_index == 1) {
3300     ctx->have_quant_offset = TRUE;
3301     if (picture->decoder_instance->compat_quant_offset &&
3302         ctx->horiz_codeblocks == 1 && ctx->vert_codeblocks == 1) {
3303       ctx->have_quant_offset = FALSE;
3304     }
3305   } else {
3306     ctx->have_quant_offset = FALSE;
3307   }
3308 }
3309 
3310 static void
schro_decoder_zero_block(SchroPictureSubbandContext * ctx,int x1,int y1,int x2,int y2)3311 schro_decoder_zero_block (SchroPictureSubbandContext * ctx,
3312     int x1, int y1, int x2, int y2)
3313 {
3314   if (SCHRO_FRAME_FORMAT_DEPTH (ctx->frame_data->format) ==
3315       SCHRO_FRAME_FORMAT_DEPTH_S16) {
3316     orc_splat_s16_2d (SCHRO_FRAME_DATA_GET_PIXEL_S16 (ctx->frame_data, x1, y1),
3317         ctx->frame_data->stride, 0, x2 - x1, y2 - y1);
3318   } else {
3319     orc_splat_s32_2d (SCHRO_FRAME_DATA_GET_PIXEL_S32 (ctx->frame_data, x1, y1),
3320         ctx->frame_data->stride, 0, x2 - x1, y2 - y1);
3321   }
3322 }
3323 
3324 static void
schro_decoder_test_quant_offset_compat(SchroPicture * picture,SchroPictureSubbandContext * ctx)3325 schro_decoder_test_quant_offset_compat (SchroPicture * picture,
3326     SchroPictureSubbandContext * ctx)
3327 {
3328   SchroParams *params = &picture->params;
3329   int quant_index = ctx->quant_index;
3330 
3331   if (ctx->have_quant_offset && ctx->horiz_codeblocks == 1 &&
3332       ctx->vert_codeblocks == 1 && ctx->index == 0 &&
3333       ctx->ymin == 0 && ctx->xmin == 0) {
3334     if (params->is_noarith) {
3335       SchroUnpack unpack_copy;
3336       schro_unpack_copy (&unpack_copy, &ctx->unpack);
3337       quant_index += schro_unpack_decode_sint (&unpack_copy);
3338     } else {
3339       SchroArith arith_copy;
3340       memcpy (&arith_copy, ctx->arith, sizeof (SchroArith));
3341       quant_index += _schro_arith_decode_sint (&arith_copy,
3342           SCHRO_CTX_QUANTISER_CONT, SCHRO_CTX_QUANTISER_VALUE,
3343           SCHRO_CTX_QUANTISER_SIGN);
3344     }
3345 
3346     if (quant_index < 0 || quant_index > 60) {
3347       SCHRO_WARNING ("turning on codeblock quantiser compatibility mode");
3348       picture->decoder_instance->compat_quant_offset = TRUE;
3349       ctx->have_quant_offset = FALSE;
3350     }
3351   }
3352 }
3353 
3354 static void
schro_decoder_decode_codeblock_noarith(SchroPicture * picture,SchroPictureSubbandContext * ctx)3355 schro_decoder_decode_codeblock_noarith (SchroPicture * picture,
3356     SchroPictureSubbandContext * ctx)
3357 {
3358   SchroParams *params = &picture->params;
3359   int n = ctx->xmax - ctx->xmin;
3360   int j;
3361 
3362   if (ctx->have_zero_flags) {
3363     int bit;
3364 
3365     /* zero codeblock */
3366     bit = schro_unpack_decode_bit (&ctx->unpack);
3367     if (bit) {
3368 #if 0
3369       if (ctx->xmax - ctx->xmin == 8) {
3370         orc_splat_s16_2d_8xn (SCHRO_FRAME_DATA_GET_PIXEL_S16 (ctx->frame_data,
3371                 ctx->xmin, ctx->ymin),
3372             ctx->frame_data->stride, 0, ctx->ymax - ctx->ymin);
3373       } else if (ctx->xmax - ctx->xmin == 4) {
3374         orc_splat_s16_2d_4xn (SCHRO_FRAME_DATA_GET_PIXEL_S16 (ctx->frame_data,
3375                 ctx->xmin, ctx->ymin),
3376             ctx->frame_data->stride, 0, ctx->ymax - ctx->ymin);
3377       } else {
3378         schro_decoder_zero_block (ctx, ctx->xmin, ctx->ymin,
3379             ctx->xmax, ctx->ymax);
3380       }
3381 #else
3382       schro_decoder_zero_block (ctx, ctx->xmin, ctx->ymin,
3383           ctx->xmax, ctx->ymax);
3384 #endif
3385       return;
3386     }
3387   }
3388 
3389   schro_decoder_test_quant_offset_compat (picture, ctx);
3390 
3391   if (ctx->have_quant_offset) {
3392     ctx->quant_index += schro_unpack_decode_sint (&ctx->unpack);
3393 
3394     if (ctx->quant_index < 0 || ctx->quant_index > 60) {
3395       /* FIXME decode error */
3396     }
3397     ctx->quant_index = CLAMP (ctx->quant_index, 0, 60);
3398   }
3399 
3400   ctx->quant_factor = schro_table_quant[ctx->quant_index];
3401   if (params->num_refs > 0) {
3402     ctx->quant_offset = schro_table_offset_3_8[ctx->quant_index];
3403   } else {
3404     ctx->quant_offset = schro_table_offset_1_2[ctx->quant_index];
3405   }
3406 
3407   if (SCHRO_FRAME_FORMAT_DEPTH (ctx->frame_data->format) ==
3408       SCHRO_FRAME_FORMAT_DEPTH_S16) {
3409     if (n == 8 && (ctx->ymax - ctx->ymin) <= 9) {
3410       int16_t tmp[72];
3411 
3412       schro_unpack_decode_sint_s16 (tmp, &ctx->unpack,
3413           n * (ctx->ymax - ctx->ymin));
3414       orc_dequantise_s16_2d_8xn (SCHRO_FRAME_DATA_GET_PIXEL_S16 (ctx->frame_data,
3415               ctx->xmin, ctx->ymin), ctx->frame_data->stride, tmp,
3416           n * sizeof (int16_t), ctx->quant_factor, ctx->quant_offset + 2,
3417           ctx->ymax - ctx->ymin);
3418     } else if (n == 4 && (ctx->ymax - ctx->ymin) <= 9) {
3419       int16_t tmp[36];
3420 
3421       schro_unpack_decode_sint_s16 (tmp, &ctx->unpack,
3422           n * (ctx->ymax - ctx->ymin));
3423       orc_dequantise_s16_2d_4xn (SCHRO_FRAME_DATA_GET_PIXEL_S16 (ctx->frame_data,
3424               ctx->xmin, ctx->ymin), ctx->frame_data->stride, tmp,
3425           n * sizeof (int16_t), ctx->quant_factor, ctx->quant_offset + 2,
3426           ctx->ymax - ctx->ymin);
3427     } else {
3428       for (j = ctx->ymin; j < ctx->ymax; j++) {
3429         int16_t *p = SCHRO_FRAME_DATA_GET_LINE (ctx->frame_data, j);
3430 
3431         schro_unpack_decode_sint_s16 (p + ctx->xmin, &ctx->unpack, n);
3432       }
3433 
3434       orc_dequantise_s16_ip_2d (SCHRO_FRAME_DATA_GET_PIXEL_S16 (ctx->frame_data,
3435               ctx->xmin, ctx->ymin), ctx->frame_data->stride, ctx->quant_factor,
3436           ctx->quant_offset + 2, n, ctx->ymax - ctx->ymin);
3437     }
3438   } else {
3439     for (j = ctx->ymin; j < ctx->ymax; j++) {
3440       int32_t *p = SCHRO_FRAME_DATA_GET_LINE (ctx->frame_data, j);
3441 
3442       schro_unpack_decode_sint_s32 (p + ctx->xmin, &ctx->unpack, n);
3443     }
3444 
3445     orc_dequantise_s32_ip_2d (SCHRO_FRAME_DATA_GET_PIXEL_S32 (ctx->frame_data,
3446             ctx->xmin, ctx->ymin), ctx->frame_data->stride, ctx->quant_factor,
3447         ctx->quant_offset + 2, n, ctx->ymax - ctx->ymin);
3448   }
3449 }
3450 
3451 static void
schro_decoder_decode_codeblock(SchroPicture * picture,SchroPictureSubbandContext * ctx)3452 schro_decoder_decode_codeblock (SchroPicture * picture,
3453     SchroPictureSubbandContext * ctx)
3454 {
3455   SchroParams *params = &picture->params;
3456   int j;
3457 
3458   if (ctx->have_zero_flags) {
3459     int bit;
3460 
3461     /* zero codeblock */
3462     bit = _schro_arith_decode_bit (ctx->arith, SCHRO_CTX_ZERO_CODEBLOCK);
3463     if (bit) {
3464       schro_decoder_zero_block (ctx, ctx->xmin, ctx->ymin,
3465           ctx->xmax, ctx->ymax);
3466       return;
3467     }
3468   }
3469 
3470   schro_decoder_test_quant_offset_compat (picture, ctx);
3471 
3472   if (ctx->have_quant_offset) {
3473     ctx->quant_index += _schro_arith_decode_sint (ctx->arith,
3474         SCHRO_CTX_QUANTISER_CONT, SCHRO_CTX_QUANTISER_VALUE,
3475         SCHRO_CTX_QUANTISER_SIGN);
3476 
3477     if (ctx->quant_index < 0 || ctx->quant_index > 60) {
3478       /* FIXME decode error */
3479     }
3480     ctx->quant_index = CLAMP (ctx->quant_index, 0, 60);
3481   }
3482 
3483   ctx->quant_factor = schro_table_quant[ctx->quant_index];
3484   if (params->num_refs > 0) {
3485     ctx->quant_offset = schro_table_offset_3_8[ctx->quant_index];
3486   } else {
3487     ctx->quant_offset = schro_table_offset_1_2[ctx->quant_index];
3488   }
3489 
3490   for (j = ctx->ymin; j < ctx->ymax; j++) {
3491     int16_t *p = SCHRO_FRAME_DATA_GET_LINE (ctx->frame_data, j);
3492     const int16_t *parent_line;
3493     const int16_t *prev_line;
3494 
3495     if (ctx->position >= 4) {
3496       parent_line =
3497           SCHRO_FRAME_DATA_GET_LINE (ctx->parent_frame_data, (j >> 1));
3498     } else {
3499       parent_line = NULL;
3500     }
3501     if (j == 0) {
3502       prev_line = NULL;
3503     } else {
3504       prev_line = SCHRO_FRAME_DATA_GET_LINE (ctx->frame_data, (j - 1));
3505     }
3506     if (SCHRO_FRAME_FORMAT_DEPTH (ctx->frame_data->format) ==
3507         SCHRO_FRAME_FORMAT_DEPTH_S32) {
3508       codeblock_line_decode_generic_s32 (ctx, (int32_t *)p, j,
3509           (int32_t *)parent_line, (int32_t *)prev_line);
3510     } else if (ctx->position >= 4 && j > 0) {
3511       if (SCHRO_SUBBAND_IS_HORIZONTALLY_ORIENTED (ctx->position)) {
3512         codeblock_line_decode_p_horiz (ctx, p, j, parent_line, prev_line);
3513       } else if (SCHRO_SUBBAND_IS_VERTICALLY_ORIENTED (ctx->position)) {
3514         codeblock_line_decode_p_vert (ctx, p, j, parent_line, prev_line);
3515       } else {
3516         codeblock_line_decode_p_diag (ctx, p, j, parent_line, prev_line);
3517       }
3518     } else {
3519       codeblock_line_decode_generic (ctx, p, j, parent_line, prev_line);
3520     }
3521   }
3522 }
3523 
3524 void
schro_decoder_decode_subband(SchroPicture * picture,SchroPictureSubbandContext * ctx)3525 schro_decoder_decode_subband (SchroPicture * picture,
3526     SchroPictureSubbandContext * ctx)
3527 {
3528   SchroParams *params = &picture->params;
3529   int x, y;
3530 
3531   ctx->subband_length = picture->subband_length[ctx->component][ctx->index];
3532   ctx->quant_index = picture->subband_quant_index[ctx->component][ctx->index];
3533   ctx->is_intra = (params->num_refs == 0);
3534 
3535   ctx->frame_data = &picture->subband_data[ctx->component][ctx->index];
3536   if (ctx->position >= 4) {
3537     ctx->parent_frame_data =
3538         &picture->subband_data[ctx->component][ctx->index - 3];
3539   }
3540 
3541   if (picture->subband_length[ctx->component][ctx->index] == 0) {
3542     schro_decoder_zero_block (ctx, 0, 0,
3543         ctx->frame_data->width, ctx->frame_data->height);
3544     return;
3545   }
3546 
3547   if (!params->is_noarith) {
3548     ctx->arith = schro_arith_new ();
3549     schro_arith_decode_init (ctx->arith,
3550         picture->subband_buffer[ctx->component][ctx->index]);
3551   } else {
3552     schro_unpack_init_with_data (&ctx->unpack,
3553         picture->subband_buffer[ctx->component][ctx->index]->data,
3554         picture->subband_buffer[ctx->component][ctx->index]->length, 1);
3555   }
3556 
3557   schro_decoder_setup_codeblocks (picture, ctx);
3558 
3559   for (y = 0; y < ctx->vert_codeblocks; y++) {
3560     int xmin;
3561     int acc;
3562     int codeblock_width;
3563     int inc;
3564 
3565     ctx->ymin = (ctx->frame_data->height * y) / ctx->vert_codeblocks;
3566     ctx->ymax = (ctx->frame_data->height * (y + 1)) / ctx->vert_codeblocks;
3567 
3568     xmin = 0;
3569     codeblock_width = ctx->frame_data->width / ctx->horiz_codeblocks;
3570     inc = ctx->frame_data->width - (ctx->horiz_codeblocks * codeblock_width);
3571     acc = 0;
3572     for (x = 0; x < ctx->horiz_codeblocks; x++) {
3573       ctx->xmin = xmin;
3574       xmin += codeblock_width;
3575       acc += inc;
3576       if (acc >= ctx->horiz_codeblocks) {
3577         acc -= ctx->horiz_codeblocks;
3578         xmin++;
3579       }
3580       ctx->xmax = xmin;
3581 
3582       if (params->is_noarith) {
3583         schro_decoder_decode_codeblock_noarith (picture, ctx);
3584       } else {
3585         schro_decoder_decode_codeblock (picture, ctx);
3586       }
3587     }
3588   }
3589   if (!params->is_noarith) {
3590     schro_arith_decode_flush (ctx->arith);
3591     if (ctx->arith->offset < ctx->subband_length - 1) {
3592       SCHRO_WARNING ("arith decoding didn't consume buffer (%d < %d)",
3593           ctx->arith->offset, ctx->subband_length);
3594 #ifdef DONT_DO_THIS
3595       ctx->broken = TRUE;
3596 #endif
3597     }
3598     if (ctx->arith->offset > ctx->subband_length + 4) {
3599       SCHRO_WARNING ("arith decoding overran buffer (%d > %d)",
3600           ctx->arith->offset, ctx->subband_length);
3601 #ifdef DONT_DO_THIS
3602       ctx->broken = TRUE;
3603 #endif
3604     }
3605     schro_arith_free (ctx->arith);
3606   } else {
3607     int offset = (ctx->unpack.n_bits_read + 0) / 8;
3608     if (offset < ctx->subband_length - 1) {
3609       SCHRO_WARNING
3610           ("vlc decoding didn't consume buffer (%d < %d) component=%d subband=%d",
3611           offset, ctx->subband_length, ctx->component, ctx->index);
3612 #ifdef DONT_DO_THIS
3613       ctx->broken = TRUE;
3614 #endif
3615     }
3616     if (offset > ctx->subband_length + 0) {
3617       SCHRO_WARNING ("vlc decoding overran buffer (%d > %d)",
3618           offset, ctx->subband_length);
3619 #ifdef DONT_DO_THIS
3620       ctx->broken = TRUE;
3621 #endif
3622     }
3623   }
3624 
3625   if (ctx->broken && ctx->position != 0) {
3626     schro_decoder_zero_block (ctx, 0, 0,
3627         ctx->frame_data->width, ctx->frame_data->height);
3628   }
3629 
3630   if (ctx->position == 0 && picture->params.num_refs == 0) {
3631     if (SCHRO_FRAME_FORMAT_DEPTH (ctx->frame_data->format) ==
3632         SCHRO_FRAME_FORMAT_DEPTH_S16) {
3633       schro_decoder_subband_dc_predict (ctx->frame_data);
3634     } else {
3635       schro_decoder_subband_dc_predict_s32 (ctx->frame_data);
3636     }
3637   }
3638 }
3639 
3640 /* reference pool */
3641 
3642 static void
schro_decoder_reference_add(SchroDecoderInstance * instance,SchroPicture * picture)3643 schro_decoder_reference_add (SchroDecoderInstance * instance,
3644     SchroPicture * picture)
3645 {
3646   SCHRO_DEBUG ("adding %d", picture->picture_number);
3647 
3648   if (schro_queue_is_full (instance->reference_queue)) {
3649     SCHRO_ERROR ("auto-retiring reference picture");
3650     schro_queue_pop (instance->reference_queue);
3651   }
3652   schro_queue_add (instance->reference_queue, schro_picture_ref (picture),
3653       picture->picture_number);
3654 }
3655 
3656 static SchroPicture *
schro_decoder_reference_get(SchroDecoderInstance * instance,SchroPictureNumber picture_number)3657 schro_decoder_reference_get (SchroDecoderInstance * instance,
3658     SchroPictureNumber picture_number)
3659 {
3660   SCHRO_DEBUG ("getting %d", picture_number);
3661   return schro_queue_find (instance->reference_queue, picture_number);
3662 }
3663 
3664 static void
schro_decoder_reference_retire(SchroDecoderInstance * instance,SchroPictureNumber picture_number)3665 schro_decoder_reference_retire (SchroDecoderInstance * instance,
3666     SchroPictureNumber picture_number)
3667 {
3668   SCHRO_DEBUG ("retiring %d", picture_number);
3669   schro_queue_delete (instance->reference_queue, picture_number);
3670 }
3671 
3672 static void
schro_decoder_error(SchroDecoder * decoder,const char * s)3673 schro_decoder_error (SchroDecoder * decoder, const char *s)
3674 {
3675   SCHRO_ERROR ("decoder error: %s", s);
3676   decoder->error = TRUE;
3677   if (!decoder->error_message) {
3678     decoder->error_message = strdup (s);
3679   }
3680 }
3681 
3682 /* compare picture numbers in a way that handles wrap around */
3683 static int
schro_picture_n_before_m(SchroPictureNumber n,SchroPictureNumber m)3684 schro_picture_n_before_m (SchroPictureNumber n, SchroPictureNumber m)
3685 {
3686   /* specified as: n occurs before m if:
3687    *   (m - n) mod (1<<32) < D */
3688   return (uint32_t) (m - n) < (1u << 31);
3689 }
3690 
3691 /* model the reorder buffer */
3692 static void
schro_picturequeue_rob_insert(SchroQueue * queue,SchroPicture * picture,unsigned windowsize)3693 schro_picturequeue_rob_insert (SchroQueue * queue, SchroPicture * picture,
3694     unsigned windowsize)
3695 {
3696   /* the implemented reorder buffer may be larger than signalled in the stream
3697    * to fix this, window the insertion sort to work on upto the last @windowsize@
3698    * elements. */
3699   /* NB, a window size of 1 implies coded_order */
3700   int i = queue->n + 1 - windowsize;
3701 
3702   SCHRO_ASSERT (queue->n < queue->size);
3703 
3704   if (i < 0)
3705     i = 0;
3706 
3707   /* find the position to insert before. */
3708   for (; i < queue->n; i++) {
3709     if (schro_picture_n_before_m (picture->picture_number,
3710             queue->elements[i].picture_number)) {
3711       break;
3712     }
3713   }
3714 
3715   /* make space and insert at i */
3716   memmove (queue->elements + i + 1, queue->elements + i,
3717       sizeof (SchroQueueElement) * (queue->n - i));
3718   queue->n++;
3719   queue->elements[i].data = picture;
3720   queue->elements[i].picture_number = picture->picture_number;
3721 }
3722 
3723 /**
3724  * schro_decoder_set_rob_size:
3725  * @instance a decoder instance to set the rob size
3726  *
3727  * precondition: either be using coded_order, or the instance must have
3728  * seen a sequence_header for guaranteed operation.
3729  *
3730  * caller must guarantee that modifying the RoB size is safe
3731  */
3732 static void
schro_decoder_set_rob_size(SchroDecoderInstance * instance)3733 schro_decoder_set_rob_size (SchroDecoderInstance * instance)
3734 {
3735   if (instance->decoder->coded_order) {
3736     /* when using coded_order, the RoB size is 1 */
3737     instance->reorder_queue_size = 1;
3738     return;
3739   }
3740 
3741   /* set default RoB sizes */
3742   if (!instance->video_format.interlaced_coding) {
3743     instance->reorder_queue_size = 2;
3744   } else {
3745     instance->reorder_queue_size = 4;
3746   }
3747 
3748   /* todo: override rob size with value specified in sequence header */
3749 
3750   /* Under normal operation, the value of reorder_queue_size needs to be one
3751    * greater than any spec derived value, since schro is unable to bypass the
3752    * reorder buffer in the same way a realtime h/w system would. */
3753   instance->reorder_queue_size++;
3754 
3755   SCHRO_ASSERT (instance->reorder_queue_size <= instance->reorder_queue->size);
3756 }
3757 
3758 /**
3759  * schro_decoder_frame_is_twofield:
3760  *
3761  * Returns: TRUE if @frame can store two field picture from @instance in a
3762  *          pseudo-progressive manner
3763  */
3764 static int
schro_decoder_frame_is_twofield(SchroDecoderInstance * instance,SchroFrame * frame)3765 schro_decoder_frame_is_twofield (SchroDecoderInstance * instance,
3766     SchroFrame * frame)
3767 {
3768   int picture_height =
3769       schro_video_format_get_picture_height (&instance->video_format);
3770 
3771   if (frame->height == 0) {
3772     /* Indicates a skipped picture */
3773     return FALSE;
3774   }
3775 
3776   if (frame->height == picture_height)
3777     return FALSE;
3778 
3779   if (!instance->video_format.interlaced_coding) {
3780     SCHRO_ERROR
3781         ("supplying non frame-sized pictures when frame_coding is not supported (%d should be %d)",
3782         frame->height, picture_height);
3783   }
3784 
3785   return TRUE;
3786 }
3787 
3788 static void
3789 schro_frame_data_draw_box (SchroFrameData * fd,
3790     int x, int y, int size_x, int size_y, SchroMotionVector * mv);
3791 
3792 static void
schro_decoder_telemetry(SchroPicture * picture,SchroFrame * output_picture)3793 schro_decoder_telemetry (SchroPicture * picture, SchroFrame * output_picture)
3794 {
3795   int i, j;
3796   SchroFrameData *fd = output_picture->components + 0;
3797   SchroParams *params = &picture->params;
3798   SchroMotionVector *mv;
3799   int skip;
3800   int ii, jj;
3801 
3802   for (j = 0; j < params->y_num_blocks; j += 4) {
3803     for (i = 0; i < params->x_num_blocks; i += 4) {
3804       mv = &picture->motion->motion_vectors[j * params->x_num_blocks + i];
3805       skip = 4 >> mv->split;
3806 
3807       for (jj = 0; jj < 4; jj += skip) {
3808         for (ii = 0; ii < 4; ii += skip) {
3809           schro_frame_data_draw_box (fd,
3810               (i + ii) * params->xbsep_luma,
3811               (j + jj) * params->ybsep_luma,
3812               skip * params->xbsep_luma,
3813               skip * params->ybsep_luma,
3814               &picture->motion->motion_vectors[(j + jj) * params->x_num_blocks +
3815                   (i + ii)]);
3816         }
3817       }
3818     }
3819   }
3820 
3821   if (picture->params.num_refs > 0) {
3822     uint8_t *data;
3823     int n1;
3824     int n2;
3825 
3826     data = SCHRO_FRAME_DATA_GET_LINE (fd, 0);
3827     n1 = picture->reference1 - picture->picture_number;
3828     if (picture->params.num_refs > 1) {
3829       n2 = picture->reference2 - picture->picture_number;
3830     } else {
3831       n2 = 0;
3832     }
3833     if (n2 < n1) {
3834       int x = n2;
3835       n2 = n1;
3836       n1 = x;
3837     }
3838 
3839     for (j = -4; j < -2; j++) {
3840       if (fd->width / 2 + j >= 0)
3841         data[fd->width / 2 + j] = 255;
3842     }
3843     if (n2 < 0) {
3844       for (i = n1; i < n2 - 1; i++) {
3845         for (j = i * 16; j < i * 16 + 10; j++) {
3846           if (fd->width / 2 + j >= 0)
3847             data[fd->width / 2 + j] = 255;
3848         }
3849       }
3850       for (i = n2; i < 0; i++) {
3851         for (j = i * 16; j < i * 16 + 10; j++) {
3852           if (fd->width / 2 + j >= 0)
3853             data[fd->width / 2 + j] = 255;
3854         }
3855       }
3856     } else {
3857       for (i = n1; i < 0; i++) {
3858         for (j = i * 16; j < i * 16 + 10; j++) {
3859           if (fd->width / 2 + j >= 0)
3860             data[fd->width / 2 + j] = 255;
3861         }
3862       }
3863       for (i = 0; i < n2; i++) {
3864         for (j = i * 16; j < i * 16 + 10; j++) {
3865           if (fd->width / 2 + j >= 0)
3866             data[fd->width / 2 + j] = 255;
3867         }
3868       }
3869     }
3870 
3871   }
3872 }
3873 
3874 static void
3875 schro_frame_data_draw_line (SchroFrameData * fd, int x1, int y1, int x2,
3876     int y2);
3877 
3878 static void
schro_frame_data_draw_box(SchroFrameData * fd,int x,int y,int size_x,int size_y,SchroMotionVector * mv)3879 schro_frame_data_draw_box (SchroFrameData * fd,
3880     int x, int y, int size_x, int size_y, SchroMotionVector * mv)
3881 {
3882   int i;
3883   uint8_t *data;
3884 
3885   if (y < fd->height) {
3886     data = SCHRO_FRAME_DATA_GET_LINE (fd, y);
3887     for (i = x; i < x + size_x; i++) {
3888       data[i] = 0;
3889     }
3890   }
3891   for (i = y; i < y + size_y && i < fd->height; i++) {
3892     data = SCHRO_FRAME_DATA_GET_LINE (fd, i);
3893     if (x < fd->width)
3894       data[x] = 0;
3895   }
3896   if (y + 1 < fd->height) {
3897     data = SCHRO_FRAME_DATA_GET_LINE (fd, y + 1);
3898     if (mv->pred_mode & 1) {
3899       if (x + size_x / 2 - 1 < fd->width)
3900         data[x + size_x / 2 - 1] = 0;
3901     }
3902     if (mv->pred_mode & 2) {
3903       if (x + size_x / 2 + 1 < fd->width)
3904         data[x + size_x / 2 + 1] = 0;
3905     }
3906   }
3907 #if 0
3908   if (mv->pred_mode & 1) {
3909     data = SCHRO_FRAME_DATA_GET_LINE (fd, y + size_y / 2 + mv->u.vec.dy[0]);
3910     data[x + size_x / 2 + mv->u.vec.dx[0]] = 0;
3911   }
3912   if (mv->pred_mode & 2) {
3913     data = SCHRO_FRAME_DATA_GET_LINE (fd, y + size_y / 2 + mv->u.vec.dy[1]);
3914     data[x + size_x / 2 + mv->u.vec.dx[1]] = 0;
3915   }
3916 #endif
3917   if (mv->pred_mode & 1) {
3918     schro_frame_data_draw_line (fd, x + size_x / 2, y + size_y / 2,
3919         x + size_x / 2 + mv->u.vec.dx[0], y + size_y / 2 + mv->u.vec.dy[0]);
3920   }
3921   if (mv->pred_mode & 2) {
3922     schro_frame_data_draw_line (fd, x + size_x / 2, y + size_y / 2,
3923         x + size_x / 2 + mv->u.vec.dx[1], y + size_y / 2 + mv->u.vec.dy[1]);
3924   }
3925 
3926 }
3927 
3928 static void
schro_frame_data_draw_point(SchroFrameData * fd,int x,int y)3929 schro_frame_data_draw_point (SchroFrameData * fd, int x, int y)
3930 {
3931   uint8_t *data;
3932 
3933   if (y >= 0 && y < fd->height && x >= 0 && x < fd->width) {
3934     data = SCHRO_FRAME_DATA_GET_LINE (fd, y);
3935     data[x] = 0;
3936   }
3937 }
3938 
3939 static void
schro_frame_data_draw_line(SchroFrameData * fd,int x1,int y1,int x2,int y2)3940 schro_frame_data_draw_line (SchroFrameData * fd, int x1, int y1, int x2, int y2)
3941 {
3942   int dx, dy;
3943   int a = 0;
3944   int i, j;
3945   int b;
3946 
3947   dx = abs (x1 - x2);
3948   dy = abs (y1 - y2);
3949   if (dx > dy) {
3950     if (x1 < x2) {
3951       j = y1;
3952       b = (y1 < y2) ? 1 : -1;
3953       a = dy / 2;
3954       for (i = x1; i <= x2; i++) {
3955         schro_frame_data_draw_point (fd, i, j);
3956         a += dy;
3957         if (a >= dx) {
3958           a -= dx;
3959           j += b;
3960         }
3961       }
3962     } else {
3963       j = y2;
3964       b = (y2 < y1) ? 1 : -1;
3965       a = dy / 2;
3966       for (i = x2; i <= x1; i++) {
3967         schro_frame_data_draw_point (fd, i, j);
3968         a += dy;
3969         if (a > dx) {
3970           a -= dx;
3971           j += b;
3972         }
3973       }
3974     }
3975   } else {
3976     if (y1 < y2) {
3977       i = x1;
3978       b = (x1 < x2) ? 1 : -1;
3979       a = dx / 2;
3980       for (j = y1; j <= y2; j++) {
3981         schro_frame_data_draw_point (fd, i, j);
3982         a += dx;
3983         if (a >= dy) {
3984           a -= dy;
3985           i += b;
3986         }
3987       }
3988     } else {
3989       i = x2;
3990       b = (x2 < x1) ? 1 : -1;
3991       a = dx / 2;
3992       for (j = y2; j <= y1; j++) {
3993         schro_frame_data_draw_point (fd, i, j);
3994         a += dx;
3995         if (a > dy) {
3996           a -= dy;
3997           i += b;
3998         }
3999       }
4000     }
4001   }
4002 }
4003