2 * Copyright (C) <2011> Intel Corporation
3 * Copyright (C) <2011> Collabora Ltd.
4 * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
6 * From bad/sys/vdpau/mpeg/mpegutil.c:
7 * Copyright (C) <2007> Jan Schmidt <thaytan@mad.scientist.com>
8 * Copyright (C) <2009> Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:gstmpegvideoparser
28 * @title: GstMpegvideoParser
29 * @short_description: Convenience library for mpeg1 and 2 video
32 * Provides useful functions for mpeg videos bitstream parsing.
40 #include "gstmpegvideoparser.h"
41 #include "parserutils.h"
44 #include <gst/base/gstbitreader.h>
45 #include <gst/base/gstbytereader.h>
47 #define MARKER_BIT 0x1
49 /* default intra quant matrix, in zig-zag order */
50 static const guint8 default_intra_quantizer_matrix[64] = {
56 27, 27, 27, 26, 26, 26,
57 26, 27, 27, 27, 29, 29, 29,
58 34, 34, 34, 29, 29, 29, 27, 27,
59 29, 29, 32, 32, 34, 34, 37,
60 38, 37, 35, 35, 34, 35,
68 static const guint8 mpeg_zigzag_8x8[64] = {
69 0, 1, 8, 16, 9, 2, 3, 10,
70 17, 24, 32, 25, 18, 11, 4, 5,
71 12, 19, 26, 33, 40, 48, 41, 34,
72 27, 20, 13, 6, 7, 14, 21, 28,
73 35, 42, 49, 56, 57, 50, 43, 36,
74 29, 22, 15, 23, 30, 37, 44, 51,
75 58, 59, 52, 45, 38, 31, 39, 46,
76 53, 60, 61, 54, 47, 55, 62, 63
81 GST_MPEG_VIDEO_MACROBLOCK_ESCAPE = G_MAXUINT,
84 /* Table B-1: Variable length codes for macroblock_address_increment */
85 static const VLCTable mpeg2_mbaddr_vlc_table[] = {
119 {GST_MPEG_VIDEO_MACROBLOCK_ESCAPE, 0x08, 11}
122 #ifndef GST_DISABLE_GST_DEBUG
123 #define GST_CAT_DEFAULT gst_mpegvideo_debug_category_get()
124 static GstDebugCategory *
125 gst_mpegvideo_debug_category_get (void)
127 static gsize cat_gonce = 0;
129 if (g_once_init_enter (&cat_gonce)) {
130 GstDebugCategory *cat = NULL;
132 GST_DEBUG_CATEGORY_INIT (cat, "codecparsers_mpegvideo", 0,
133 "mpegvideo parser library");
135 g_once_init_leave (&cat_gonce, (gsize) cat);
138 return (GstDebugCategory *) cat_gonce;
140 #endif /* GST_DISABLE_GST_DEBUG */
142 /* Set the Pixel Aspect Ratio in our hdr from a ASR code in the data */
144 set_par_from_asr_mpeg1 (GstMpegVideoSequenceHdr * seqhdr, guint8 asr_code)
146 int ratios[16][2] = {
147 {0, 0}, /* 0, Invalid */
149 {10000, 6735}, /* 2, 0.6735 */
150 {64, 45}, /* 3, 0.7031 16:9 625 line */
151 {10000, 7615}, /* 4, 0.7615 */
152 {10000, 8055}, /* 5, 0.8055 */
153 {32, 27}, /* 6, 0.8437 */
154 {10000, 8935}, /* 7, 0.8935 */
155 {10000, 9375}, /* 8, 0.9375 */
156 {10000, 9815}, /* 9, 0.9815 */
157 {10000, 10255}, /* 10, 1.0255 */
158 {10000, 10695}, /* 11, 1.0695 */
159 {8, 9}, /* 12, 1.125 */
160 {10000, 11575}, /* 13, 1.1575 */
161 {10000, 12015}, /* 14, 1.2015 */
162 {0, 0}, /* 15, invalid */
166 seqhdr->par_w = ratios[asr_code][0];
167 seqhdr->par_h = ratios[asr_code][1];
171 set_fps_from_code (GstMpegVideoSequenceHdr * seqhdr, guint8 fps_code)
173 const gint framerates[][2] = {
174 {30, 1}, {24000, 1001}, {24, 1}, {25, 1},
175 {30000, 1001}, {30, 1}, {50, 1}, {60000, 1001},
179 if (fps_code && fps_code < 10) {
180 seqhdr->fps_n = framerates[fps_code][0];
181 seqhdr->fps_d = framerates[fps_code][1];
183 GST_DEBUG ("unknown/invalid frame_rate_code %d", fps_code);
184 /* Force a valid framerate */
185 /* FIXME or should this be kept unknown ?? */
186 seqhdr->fps_n = 30000;
187 seqhdr->fps_d = 1001;
191 /* @size and @offset are wrt current reader position */
193 scan_for_start_codes (const GstByteReader * reader, guint offset, guint size)
198 g_assert ((guint64) offset + size <= reader->size - reader->byte);
200 /* we can't find the pattern with less than 4 bytes */
201 if (G_UNLIKELY (size < 4))
204 data = reader->data + reader->byte + offset;
206 while (i <= (size - 4)) {
207 if (data[i + 2] > 1) {
209 } else if (data[i + 1]) {
211 } else if (data[i] || data[i + 2] != 1) {
228 * gst_mpeg_video_parse:
229 * @packet: a #GstMpegVideoPacket to fill with the data and offset of the
231 * @data: The data to parse
232 * @size: The size of @data
233 * @offset: The offset from which to start parsing
235 * Parses the MPEG 1/2 video bitstream contained in @data, and returns the
236 * offset, and if known also the size, in @packet. This function will scan
237 * the data to find the next packet if needed.
239 * Returns: TRUE if a packet start code was found, otherwise FALSE.
242 gst_mpeg_video_parse (GstMpegVideoPacket * packet,
243 const guint8 * data, gsize size, guint offset)
248 if (size <= offset) {
249 GST_DEBUG ("Can't parse from offset %d, buffer is to small", offset);
254 gst_byte_reader_init (&br, &data[offset], size);
256 off = scan_for_start_codes (&br, 0, size);
259 GST_DEBUG ("No start code prefix in this buffer");
263 if (gst_byte_reader_skip (&br, off + 3) == FALSE)
266 if (gst_byte_reader_get_uint8 (&br, &packet->type) == FALSE)
270 packet->offset = offset + off + 4;
273 /* try to find end of packet */
275 off = scan_for_start_codes (&br, 0, size);
284 GST_WARNING ("Failed to parse");
290 * gst_mpeg_video_packet_parse_sequence_header:
291 * @packet: The #GstMpegVideoPacket that carries the data
292 * @seqhdr: (out): The #GstMpegVideoSequenceHdr structure to fill
294 * Parses the @seqhdr MPEG Video Sequence Header structure members
297 * Returns: %TRUE if the seqhdr could be parsed correctly, %FALSE otherwise.
302 gst_mpeg_video_packet_parse_sequence_header (const GstMpegVideoPacket * packet,
303 GstMpegVideoSequenceHdr * seqhdr)
308 g_return_val_if_fail (seqhdr != NULL, FALSE);
310 if (packet->size < 8)
313 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
315 /* Setting the height/width codes */
316 READ_UINT16 (&br, seqhdr->width, 12);
317 READ_UINT16 (&br, seqhdr->height, 12);
319 READ_UINT8 (&br, seqhdr->aspect_ratio_info, 4);
320 /* Interpret PAR according to MPEG-1. Needs to be reinterpreted
321 * later, if a sequence_display extension is seen */
322 set_par_from_asr_mpeg1 (seqhdr, seqhdr->aspect_ratio_info);
324 READ_UINT8 (&br, seqhdr->frame_rate_code, 4);
325 set_fps_from_code (seqhdr, seqhdr->frame_rate_code);
327 READ_UINT32 (&br, seqhdr->bitrate_value, 18);
328 if (seqhdr->bitrate_value == 0x3ffff) {
332 /* Value in header is in units of 400 bps */
333 seqhdr->bitrate = seqhdr->bitrate_value * 400;
336 READ_UINT8 (&br, bits, 1);
337 if (bits != MARKER_BIT)
340 /* VBV buffer size */
341 READ_UINT16 (&br, seqhdr->vbv_buffer_size_value, 10);
343 /* constrained_parameters_flag */
344 READ_UINT8 (&br, seqhdr->constrained_parameters_flag, 1);
346 /* load_intra_quantiser_matrix */
347 READ_UINT8 (&br, seqhdr->load_intra_quantiser_matrix, 1);
348 if (seqhdr->load_intra_quantiser_matrix) {
350 for (i = 0; i < 64; i++)
351 READ_UINT8 (&br, seqhdr->intra_quantizer_matrix[i], 8);
353 memcpy (seqhdr->intra_quantizer_matrix, default_intra_quantizer_matrix, 64);
355 /* non intra quantizer matrix */
356 READ_UINT8 (&br, seqhdr->load_non_intra_quantiser_matrix, 1);
357 if (seqhdr->load_non_intra_quantiser_matrix) {
359 for (i = 0; i < 64; i++)
360 READ_UINT8 (&br, seqhdr->non_intra_quantizer_matrix[i], 8);
362 memset (seqhdr->non_intra_quantizer_matrix, 16, 64);
365 GST_LOG ("width x height: %d x %d", seqhdr->width, seqhdr->height);
366 GST_LOG ("fps: %d/%d", seqhdr->fps_n, seqhdr->fps_d);
367 GST_LOG ("par: %d/%d", seqhdr->par_w, seqhdr->par_h);
368 GST_LOG ("bitrate: %d", seqhdr->bitrate);
375 GST_WARNING ("Failed to parse sequence header");
376 /* clear out stuff */
377 memset (seqhdr, 0, sizeof (*seqhdr));
383 * gst_mpeg_video_packet_parse_sequence_extension:
384 * @packet: The #GstMpegVideoPacket that carries the data
385 * @seqext: (out): The #GstMpegVideoSequenceExt structure to fill
387 * Parses the @seqext MPEG Video Sequence Extension structure members
390 * Returns: %TRUE if the seqext could be parsed correctly, %FALSE otherwise.
395 gst_mpeg_video_packet_parse_sequence_extension (const GstMpegVideoPacket *
396 packet, GstMpegVideoSequenceExt * seqext)
400 g_return_val_if_fail (seqext != NULL, FALSE);
402 if (packet->size < 6) {
403 GST_DEBUG ("not enough bytes to parse the extension");
407 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
409 if (gst_bit_reader_get_bits_uint8_unchecked (&br, 4) !=
410 GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE) {
411 GST_DEBUG ("Not parsing a sequence extension");
415 /* skip profile and level escape bit */
416 seqext->profile_level_escape_bit =
417 gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
419 seqext->profile = gst_bit_reader_get_bits_uint8_unchecked (&br, 3);
420 seqext->level = gst_bit_reader_get_bits_uint8_unchecked (&br, 4);
423 seqext->progressive = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
426 seqext->chroma_format = gst_bit_reader_get_bits_uint8_unchecked (&br, 2);
428 /* resolution extension */
429 seqext->horiz_size_ext = gst_bit_reader_get_bits_uint8_unchecked (&br, 2);
430 seqext->vert_size_ext = gst_bit_reader_get_bits_uint8_unchecked (&br, 2);
432 seqext->bitrate_ext = gst_bit_reader_get_bits_uint16_unchecked (&br, 12);
434 /* skip marker bits */
435 gst_bit_reader_skip_unchecked (&br, 1);
437 seqext->vbv_buffer_size_extension =
438 gst_bit_reader_get_bits_uint8_unchecked (&br, 8);
439 seqext->low_delay = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
441 /* framerate extension */
442 seqext->fps_n_ext = gst_bit_reader_get_bits_uint8_unchecked (&br, 2);
443 seqext->fps_d_ext = gst_bit_reader_get_bits_uint8_unchecked (&br, 2);
449 * gst_mpeg_video_packet_parse_sequence_display_extension:
450 * @packet: The #GstMpegVideoPacket that carries the data
451 * @seqdisplayext: (out): The #GstMpegVideoSequenceDisplayExt
454 * Parses the @seqext MPEG Video Sequence Display Extension structure
455 * members from video @packet
457 * Returns: %TRUE if the seqext could be parsed correctly, %FALSE otherwise.
462 gst_mpeg_video_packet_parse_sequence_display_extension (const GstMpegVideoPacket
463 * packet, GstMpegVideoSequenceDisplayExt * seqdisplayext)
467 g_return_val_if_fail (seqdisplayext != NULL, FALSE);
469 if (packet->size < 5) {
470 GST_DEBUG ("not enough bytes to parse the extension");
474 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
476 if (gst_bit_reader_get_bits_uint8_unchecked (&br, 4) !=
477 GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_DISPLAY) {
478 GST_DEBUG ("Not parsing a sequence display extension");
482 seqdisplayext->video_format =
483 gst_bit_reader_get_bits_uint8_unchecked (&br, 3);
484 seqdisplayext->colour_description_flag =
485 gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
487 if (seqdisplayext->colour_description_flag) {
488 seqdisplayext->colour_primaries =
489 gst_bit_reader_get_bits_uint8_unchecked (&br, 8);
490 seqdisplayext->transfer_characteristics =
491 gst_bit_reader_get_bits_uint8_unchecked (&br, 8);
492 seqdisplayext->matrix_coefficients =
493 gst_bit_reader_get_bits_uint8_unchecked (&br, 8);
496 if (gst_bit_reader_get_remaining (&br) < 29) {
497 GST_DEBUG ("Not enough remaining bytes to parse the extension");
501 seqdisplayext->display_horizontal_size =
502 gst_bit_reader_get_bits_uint16_unchecked (&br, 14);
503 /* skip marker bit */
504 gst_bit_reader_skip_unchecked (&br, 1);
505 seqdisplayext->display_vertical_size =
506 gst_bit_reader_get_bits_uint16_unchecked (&br, 14);
512 * gst_mpeg_video_packet_parse_sequence_scalable_extension:
513 * @packet: The #GstMpegVideoPacket that carries the data
514 * @seqscaleext: (out): The #GstMpegVideoSequenceScalableExt structure to fill
516 * Parses the @seqscaleext MPEG Video Sequence Scalable Extension structure
517 * members from video @packet
519 * Returns: %TRUE if the seqext could be parsed correctly, %FALSE otherwise.
524 gst_mpeg_video_packet_parse_sequence_scalable_extension
525 (const GstMpegVideoPacket * packet,
526 GstMpegVideoSequenceScalableExt * seqscaleext) {
529 g_return_val_if_fail (seqscaleext != NULL, FALSE);
531 if (packet->size < 2) {
532 GST_DEBUG ("not enough bytes to parse the extension");
536 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
538 if (gst_bit_reader_get_bits_uint8_unchecked (&br, 4) !=
539 GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_SCALABLE) {
540 GST_DEBUG ("Not parsing a sequence scalable extension");
544 READ_UINT8 (&br, seqscaleext->scalable_mode, 2);
545 READ_UINT8 (&br, seqscaleext->layer_id, 4);
547 if (seqscaleext->scalable_mode == GST_MPEG_VIDEO_SEQ_SCALABLE_MODE_SPATIAL) {
548 READ_UINT16 (&br, seqscaleext->lower_layer_prediction_horizontal_size, 14);
552 READ_UINT16 (&br, seqscaleext->lower_layer_prediction_vertical_size, 14);
554 READ_UINT8 (&br, seqscaleext->horizontal_subsampling_factor_m, 5);
555 READ_UINT8 (&br, seqscaleext->horizontal_subsampling_factor_n, 5);
556 READ_UINT8 (&br, seqscaleext->vertical_subsampling_factor_m, 5);
557 READ_UINT8 (&br, seqscaleext->vertical_subsampling_factor_n, 5);
560 if (seqscaleext->scalable_mode == GST_MPEG_VIDEO_SEQ_SCALABLE_MODE_TEMPORAL) {
561 READ_UINT8 (&br, seqscaleext->picture_mux_enable, 1);
562 if (seqscaleext->picture_mux_enable)
563 READ_UINT8 (&br, seqscaleext->mux_to_progressive_sequence, 1);
564 READ_UINT8 (&br, seqscaleext->picture_mux_order, 3);
565 READ_UINT8 (&br, seqscaleext->picture_mux_factor, 3);
571 GST_WARNING ("error parsing \"Sequence Scalable Extension\"");
576 gst_mpeg_video_finalise_mpeg2_sequence_header (GstMpegVideoSequenceHdr * seqhdr,
577 GstMpegVideoSequenceExt * seqext,
578 GstMpegVideoSequenceDisplayExt * displayext)
584 seqhdr->fps_n = seqhdr->fps_n * (seqext->fps_n_ext + 1);
585 seqhdr->fps_d = seqhdr->fps_d * (seqext->fps_d_ext + 1);
586 /* Extend width and height to 14 bits by adding the extension bits */
587 seqhdr->width |= (seqext->horiz_size_ext << 12);
588 seqhdr->height |= (seqext->vert_size_ext << 12);
589 seqhdr->bitrate += (seqext->bitrate_ext << 18) * 400;
595 /* Use the display size for calculating PAR when display ext present.
596 * But we are handling this like what DVD players are doing. Which means,
597 * ignore the display extension values if they are greater than the width/height
598 * values provided by seqhdr and calculate the PAR based on the seqhdr values. */
599 if (displayext->display_horizontal_size < w)
600 w = displayext->display_horizontal_size;
601 if (displayext->display_vertical_size < h)
602 h = displayext->display_vertical_size;
605 /* Pixel_width = DAR_width * display_vertical_size */
606 /* Pixel_height = DAR_height * display_horizontal_size */
607 switch (seqhdr->aspect_ratio_info) {
608 case 0x01: /* Square pixels */
609 seqhdr->par_w = seqhdr->par_h = 1;
611 case 0x02: /* 3:4 DAR = 4:3 pixels */
612 seqhdr->par_w = 4 * h;
613 seqhdr->par_h = 3 * w;
615 case 0x03: /* 9:16 DAR */
616 seqhdr->par_w = 16 * h;
617 seqhdr->par_h = 9 * w;
619 case 0x04: /* 1:2.21 DAR */
620 seqhdr->par_w = 221 * h;
621 seqhdr->par_h = 100 * w;
624 GST_DEBUG ("unknown/invalid aspect_ratio_information %d",
625 seqhdr->aspect_ratio_info);
633 * gst_mpeg_video_packet_parse_quant_matrix_extension:
634 * @packet: The #GstMpegVideoPacket that carries the data
635 * @quant: (out): The #GstMpegVideoQuantMatrixExt structure to fill
637 * Parses the @quant MPEG Video Quantization Matrix Extension
638 * structure members from video @packet
640 * Returns: %TRUE if the quant matrix extension could be parsed correctly,
646 gst_mpeg_video_packet_parse_quant_matrix_extension (const GstMpegVideoPacket *
647 packet, GstMpegVideoQuantMatrixExt * quant)
652 g_return_val_if_fail (quant != NULL, FALSE);
654 if (packet->size < 1) {
655 GST_DEBUG ("not enough bytes to parse the extension");
659 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
661 if (gst_bit_reader_get_bits_uint8_unchecked (&br, 4) !=
662 GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX) {
663 GST_DEBUG ("Not parsing a quant matrix extension");
667 READ_UINT8 (&br, quant->load_intra_quantiser_matrix, 1);
668 if (quant->load_intra_quantiser_matrix) {
669 for (i = 0; i < 64; i++) {
670 READ_UINT8 (&br, quant->intra_quantiser_matrix[i], 8);
674 READ_UINT8 (&br, quant->load_non_intra_quantiser_matrix, 1);
675 if (quant->load_non_intra_quantiser_matrix) {
676 for (i = 0; i < 64; i++) {
677 READ_UINT8 (&br, quant->non_intra_quantiser_matrix[i], 8);
681 READ_UINT8 (&br, quant->load_chroma_intra_quantiser_matrix, 1);
682 if (quant->load_chroma_intra_quantiser_matrix) {
683 for (i = 0; i < 64; i++) {
684 READ_UINT8 (&br, quant->chroma_intra_quantiser_matrix[i], 8);
688 READ_UINT8 (&br, quant->load_chroma_non_intra_quantiser_matrix, 1);
689 if (quant->load_chroma_non_intra_quantiser_matrix) {
690 for (i = 0; i < 64; i++) {
691 READ_UINT8 (&br, quant->chroma_non_intra_quantiser_matrix[i], 8);
698 GST_WARNING ("error parsing \"Quant Matrix Extension\"");
703 * gst_mpeg_video_packet_parse_picture_extension:
704 * @packet: The #GstMpegVideoPacket that carries the data
705 * @ext: (out): The #GstMpegVideoPictureExt structure to fill
707 * Parse the @ext MPEG Video Picture Extension structure members from
710 * Returns: %TRUE if the picture extension could be parsed correctly,
716 gst_mpeg_video_packet_parse_picture_extension (const GstMpegVideoPacket *
717 packet, GstMpegVideoPictureExt * ext)
721 g_return_val_if_fail (ext != NULL, FALSE);
723 if (packet->size < 5)
726 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
728 if (gst_bit_reader_get_bits_uint8_unchecked (&br, 4) !=
729 GST_MPEG_VIDEO_PACKET_EXT_PICTURE) {
730 GST_DEBUG ("Extension is not a picture extension");
735 READ_UINT8 (&br, ext->f_code[0][0], 4);
736 READ_UINT8 (&br, ext->f_code[0][1], 4);
737 READ_UINT8 (&br, ext->f_code[1][0], 4);
738 READ_UINT8 (&br, ext->f_code[1][1], 4);
740 /* intra DC precision */
741 READ_UINT8 (&br, ext->intra_dc_precision, 2);
743 /* picture structure */
744 READ_UINT8 (&br, ext->picture_structure, 2);
746 /* top field first */
747 READ_UINT8 (&br, ext->top_field_first, 1);
749 /* frame pred frame dct */
750 READ_UINT8 (&br, ext->frame_pred_frame_dct, 1);
752 /* concealment motion vectors */
753 READ_UINT8 (&br, ext->concealment_motion_vectors, 1);
756 READ_UINT8 (&br, ext->q_scale_type, 1);
758 /* intra vlc format */
759 READ_UINT8 (&br, ext->intra_vlc_format, 1);
762 READ_UINT8 (&br, ext->alternate_scan, 1);
764 /* repeat first field */
765 READ_UINT8 (&br, ext->repeat_first_field, 1);
767 /* chroma_420_type */
768 READ_UINT8 (&br, ext->chroma_420_type, 1);
770 /* progressive_frame */
771 READ_UINT8 (&br, ext->progressive_frame, 1);
773 /* composite display */
774 READ_UINT8 (&br, ext->composite_display, 1);
776 if (ext->composite_display) {
779 READ_UINT8 (&br, ext->v_axis, 1);
782 READ_UINT8 (&br, ext->field_sequence, 3);
785 READ_UINT8 (&br, ext->sub_carrier, 1);
787 /* burst amplitude */
788 READ_UINT8 (&br, ext->burst_amplitude, 7);
790 /* sub_carrier phase */
791 READ_UINT8 (&br, ext->sub_carrier_phase, 8);
797 GST_WARNING ("error parsing \"Picture Coding Extension\"");
803 * gst_mpeg_video_packet_parse_picture_header:
804 * @packet: The #GstMpegVideoPacket that carries the data
805 * @pichdr: (out): The #GstMpegVideoPictureHdr structure to fill
807 * Parsers the @pichdr MPEG Video Picture Header structure members
810 * Returns: %TRUE if the picture sequence could be parsed correctly, %FALSE
816 gst_mpeg_video_packet_parse_picture_header (const GstMpegVideoPacket * packet,
817 GstMpegVideoPictureHdr * hdr)
821 if (packet->size < 4)
824 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
826 /* temperal sequence number */
827 if (!gst_bit_reader_get_bits_uint16 (&br, &hdr->tsn, 10))
832 if (!gst_bit_reader_get_bits_uint8 (&br, (guint8 *) & hdr->pic_type, 3))
836 if (hdr->pic_type == 0 || hdr->pic_type > 4)
837 goto bad_pic_type; /* Corrupted picture packet */
840 if (!gst_bit_reader_get_bits_uint16 (&br, &hdr->vbv_delay, 16))
843 if (hdr->pic_type == GST_MPEG_VIDEO_PICTURE_TYPE_P
844 || hdr->pic_type == GST_MPEG_VIDEO_PICTURE_TYPE_B) {
846 READ_UINT8 (&br, hdr->full_pel_forward_vector, 1);
848 READ_UINT8 (&br, hdr->f_code[0][0], 3);
849 hdr->f_code[0][1] = hdr->f_code[0][0];
851 hdr->full_pel_forward_vector = 0;
852 hdr->f_code[0][0] = hdr->f_code[0][1] = 0;
855 if (hdr->pic_type == GST_MPEG_VIDEO_PICTURE_TYPE_B) {
856 READ_UINT8 (&br, hdr->full_pel_backward_vector, 1);
858 READ_UINT8 (&br, hdr->f_code[1][0], 3);
859 hdr->f_code[1][1] = hdr->f_code[1][0];
861 hdr->full_pel_backward_vector = 0;
862 hdr->f_code[1][0] = hdr->f_code[1][1] = 0;
869 GST_WARNING ("Unsupported picture type : %d", hdr->pic_type);
875 GST_WARNING ("Not enough data to parse picture header");
881 * gst_mpeg_video_packet_parse_gop:
882 * @packet: The #GstMpegVideoPacket that carries the data
883 * @gop: (out): The #GstMpegVideoGop structure to fill
885 * Parses the @gop MPEG Video Group of Picture structure members from
888 * Returns: %TRUE if the gop could be parsed correctly, %FALSE otherwise.
893 gst_mpeg_video_packet_parse_gop (const GstMpegVideoPacket * packet,
894 GstMpegVideoGop * gop)
898 g_return_val_if_fail (gop != NULL, FALSE);
900 if (packet->size < 4)
903 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
905 READ_UINT8 (&br, gop->drop_frame_flag, 1);
907 READ_UINT8 (&br, gop->hour, 5);
909 READ_UINT8 (&br, gop->minute, 6);
911 /* skip unused bit */
912 if (!gst_bit_reader_skip (&br, 1))
915 READ_UINT8 (&br, gop->second, 6);
917 READ_UINT8 (&br, gop->frame, 6);
919 READ_UINT8 (&br, gop->closed_gop, 1);
921 READ_UINT8 (&br, gop->broken_link, 1);
926 GST_WARNING ("error parsing \"GOP\"");
931 * gst_mpeg_video_packet_parse_slice_header:
932 * @packet: The #GstMpegVideoPacket that carries the data
933 * @slice_hdr: (out): The #GstMpegVideoSliceHdr structure to fill
934 * @seqhdr: The #GstMpegVideoSequenceHdr header
935 * @seqscaleext: The #GstMpegVideoSequenceScalableExt header
937 * Parses the @GstMpegVideoSliceHdr structure members from @data
939 * Returns: %TRUE if the slice could be parsed correctly, %FALSE otherwise.
944 gst_mpeg_video_packet_parse_slice_header (const GstMpegVideoPacket * packet,
945 GstMpegVideoSliceHdr * slice_hdr, GstMpegVideoSequenceHdr * seqhdr,
946 GstMpegVideoSequenceScalableExt * seqscaleext)
951 guint8 bits, extra_bits;
952 guint8 vertical_position, vertical_position_extension = 0;
954 g_return_val_if_fail (seqhdr != NULL, FALSE);
956 if (packet->size < 1)
959 gst_bit_reader_init (&br, &packet->data[packet->offset], packet->size);
961 if (packet->type < GST_MPEG_VIDEO_PACKET_SLICE_MIN ||
962 packet->type > GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
963 GST_DEBUG ("Not parsing a slice");
966 vertical_position = packet->type - GST_MPEG_VIDEO_PACKET_SLICE_MIN;
968 height = seqhdr->height;
970 READ_UINT8 (&br, vertical_position_extension, 3);
972 slice_hdr->vertical_position = packet->type;
973 slice_hdr->vertical_position_ext = vertical_position_extension;
976 if (seqscaleext->scalable_mode ==
977 GST_MPEG_VIDEO_SEQ_SCALABLE_MODE_DATA_PARTITIONING)
978 READ_UINT8 (&br, slice_hdr->priority_breakpoint, 7);
980 READ_UINT8 (&br, slice_hdr->quantiser_scale_code, 5);
982 READ_UINT8 (&br, slice_hdr->slice_ext_flag, 1);
983 if (!slice_hdr->slice_ext_flag)
984 slice_hdr->intra_slice = 0;
986 READ_UINT8 (&br, slice_hdr->intra_slice, 1);
987 READ_UINT8 (&br, slice_hdr->slice_picture_id_enable, 1);
988 READ_UINT8 (&br, slice_hdr->slice_picture_id, 6);
990 READ_UINT8 (&br, bits, 1);
992 READ_UINT8 (&br, extra_bits, 8);
993 READ_UINT8 (&br, bits, 1);
997 slice_hdr->header_size = gst_bit_reader_get_pos (&br);
1000 slice_hdr->mb_row = (vertical_position_extension << 7) + vertical_position;
1002 slice_hdr->mb_row = vertical_position;
1004 slice_hdr->mb_column = -1;
1006 if (!decode_vlc (&br, &mb_inc, mpeg2_mbaddr_vlc_table,
1007 G_N_ELEMENTS (mpeg2_mbaddr_vlc_table))) {
1008 GST_WARNING ("failed to decode first macroblock_address_increment");
1011 slice_hdr->mb_column +=
1012 mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE ? 33 : mb_inc;
1013 } while (mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE);
1018 GST_WARNING ("error parsing \"Slice\"");
1023 * gst_mpeg_video_quant_matrix_get_raster_from_zigzag:
1024 * @out_quant: (out): The resulting quantization matrix
1025 * @quant: The source quantization matrix
1027 * Converts quantization matrix @quant from zigzag scan order to
1028 * raster scan order and store the resulting factors into @out_quant.
1030 * Note: it is an error to pass the same table in both @quant and
1031 * @out_quant arguments.
1036 gst_mpeg_video_quant_matrix_get_raster_from_zigzag (guint8 out_quant[64],
1037 const guint8 quant[64])
1041 g_return_if_fail (out_quant != quant);
1043 for (i = 0; i < 64; i++)
1044 out_quant[mpeg_zigzag_8x8[i]] = quant[i];
1048 * gst_mpeg_video_quant_matrix_get_zigzag_from_raster:
1049 * @out_quant: (out): The resulting quantization matrix
1050 * @quant: The source quantization matrix
1052 * Converts quantization matrix @quant from raster scan order to
1053 * zigzag scan order and store the resulting factors into @out_quant.
1055 * Note: it is an error to pass the same table in both @quant and
1056 * @out_quant arguments.
1061 gst_mpeg_video_quant_matrix_get_zigzag_from_raster (guint8 out_quant[64],
1062 const guint8 quant[64])
1066 g_return_if_fail (out_quant != quant);
1068 for (i = 0; i < 64; i++)
1069 out_quant[i] = quant[mpeg_zigzag_8x8[i]];