+static inline int dv_is_pal( AVPacket *pkt )
+{
+ return pkt->data[3] & 0x80;
+}
+
+static int dv_is_wide( AVPacket *pkt )
+{
+ int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
+
+ for ( ; i < pkt->size; i += 5 /* packet size */ )
+ {
+ if ( pkt->data[ i ] == 0x61 )
+ {
+ uint8_t x = pkt->data[ i + 2 ] & 0x7;
+ return ( x == 2 ) || ( x == 7 );
+ }
+ }
+ return 0;
+}
+
+static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
+{
+ double aspect_ratio = 1.0;
+
+ if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
+ {
+ if ( pkt )
+ {
+ if ( dv_is_pal( pkt ) )
+ {
+ aspect_ratio = dv_is_wide( pkt )
+ ? 64.0/45.0 // 16:9 PAL
+ : 16.0/15.0; // 4:3 PAL
+ }
+ else
+ {
+ aspect_ratio = dv_is_wide( pkt )
+ ? 32.0/27.0 // 16:9 NTSC
+ : 8.0/9.0; // 4:3 NTSC
+ }
+ }
+ else
+ {
+ AVRational ar =
+#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
+ stream->sample_aspect_ratio;
+#else
+ codec_context->sample_aspect_ratio;
+#endif
+ // Override FFmpeg's notion of DV aspect ratios, which are
+ // based upon a width of 704. Since we do not have a normaliser
+ // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
+ // we just coerce the values to facilitate a passive behaviour through
+ // the rescale normaliser when using equivalent producers and consumers.
+ // = display_aspect / (width * height)
+ if ( ar.num == 10 && ar.den == 11 )
+ aspect_ratio = 8.0/9.0; // 4:3 NTSC
+ else if ( ar.num == 59 && ar.den == 54 )
+ aspect_ratio = 16.0/15.0; // 4:3 PAL
+ else if ( ar.num == 40 && ar.den == 33 )
+ aspect_ratio = 32.0/27.0; // 16:9 NTSC
+ else if ( ar.num == 118 && ar.den == 81 )
+ aspect_ratio = 64.0/45.0; // 16:9 PAL
+ }
+ }
+ else
+ {
+ AVRational codec_sar = codec_context->sample_aspect_ratio;
+ AVRational stream_sar =
+#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
+ stream->sample_aspect_ratio;
+#else
+ { 0, 1 };
+#endif
+ if ( codec_sar.num > 0 )
+ aspect_ratio = av_q2d( codec_sar );
+ else if ( stream_sar.num > 0 )
+ aspect_ratio = av_q2d( stream_sar );
+ }
+ return aspect_ratio;
+}
+