static gboolean gst_vdp_h264_dec_stop (GstVideoDecoder * video_decoder) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (video_decoder); g_object_unref (h264_dec->dpb); return GST_VIDEO_DECODER_CLASS (parent_class)->stop (video_decoder); }
static gboolean gst_vdp_h264_dec_flush (GstBaseVideoDecoder * base_video_decoder) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (base_video_decoder); h264_dec->got_idr = FALSE; gst_h264_dpb_flush (h264_dec->dpb, FALSE); return TRUE; }
static gboolean gst_vdp_h264_dec_set_format (GstVideoDecoder * video_decoder, GstVideoCodecState * state) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (video_decoder); if (h264_dec->input_state) gst_video_codec_state_unref (h264_dec->input_state); h264_dec->input_state = gst_video_codec_state_ref (state); GST_FIXME_OBJECT (video_decoder, "Do something when receiving input state ?"); return TRUE; }
static gboolean gst_vdp_h264_dec_start (GstVideoDecoder * video_decoder) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (video_decoder); h264_dec->got_idr = FALSE; h264_dec->current_sps = -1; h264_dec->got_idr = FALSE; h264_dec->dpb = g_object_new (GST_TYPE_H264_DPB, NULL); gst_h264_dpb_set_output_func (h264_dec->dpb, gst_vdp_h264_dec_output, h264_dec); return GST_VIDEO_DECODER_CLASS (parent_class)->start (video_decoder); }
static gint gst_vdp_h264_dec_scan_for_sync (GstBaseVideoDecoder * base_video_decoder, GstAdapter * adapter) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (base_video_decoder); gint m; if (h264_dec->packetized) return 0; m = gst_adapter_masked_scan_uint32 (adapter, 0xffffff00, 0x00000100, 0, gst_adapter_available (adapter)); if (m == -1) return gst_adapter_available (adapter) - SYNC_CODE_SIZE; return m; }
static gboolean gst_vdp_h264_dec_start (GstBaseVideoDecoder * base_video_decoder) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (base_video_decoder); h264_dec->packetized = FALSE; h264_dec->nal_length_size = SYNC_CODE_SIZE; h264_dec->got_idr = FALSE; h264_dec->sequence = NULL; h264_dec->parser = g_object_new (GST_TYPE_H264_PARSER, NULL); h264_dec->dpb = g_object_new (GST_TYPE_H264_DPB, NULL); gst_h264_dpb_set_output_func (h264_dec->dpb, gst_vdp_h264_dec_output, h264_dec); return GST_BASE_VIDEO_DECODER_CLASS (parent_class)->start (base_video_decoder); }
static gboolean gst_vdp_h264_dec_set_sink_caps (GstBaseVideoDecoder * base_video_decoder, GstCaps * caps) { GstVdpH264Dec *h264_dec; GstStructure *structure; const GValue *value; h264_dec = GST_VDP_H264_DEC (base_video_decoder); structure = gst_caps_get_structure (caps, 0); /* packetized video has a codec_data */ if ((value = gst_structure_get_value (structure, "codec_data"))) { GstBuffer *buf; GstBitReader reader; guint8 version; guint8 n_sps, n_pps; gint i; GST_DEBUG_OBJECT (h264_dec, "have packetized h264"); h264_dec->packetized = TRUE; buf = gst_value_get_buffer (value); GST_MEMDUMP ("avcC:", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf)); /* parse the avcC data */ if (GST_BUFFER_SIZE (buf) < 7) { GST_ERROR_OBJECT (h264_dec, "avcC size %u < 7", GST_BUFFER_SIZE (buf)); return FALSE; } gst_bit_reader_init_from_buffer (&reader, buf); READ_UINT8 (&reader, version, 8); if (version != 1) return FALSE; SKIP (&reader, 30); READ_UINT8 (&reader, h264_dec->nal_length_size, 2); h264_dec->nal_length_size += 1; GST_DEBUG_OBJECT (h264_dec, "nal length %u", h264_dec->nal_length_size); SKIP (&reader, 3); READ_UINT8 (&reader, n_sps, 5); for (i = 0; i < n_sps; i++) { guint16 sps_length; guint8 *data; READ_UINT16 (&reader, sps_length, 16); sps_length -= 1; SKIP (&reader, 8); data = GST_BUFFER_DATA (buf) + gst_bit_reader_get_pos (&reader) / 8; if (!gst_h264_parser_parse_sequence (h264_dec->parser, data, sps_length)) return FALSE; SKIP (&reader, sps_length * 8); } READ_UINT8 (&reader, n_pps, 8); for (i = 0; i < n_pps; i++) { guint16 pps_length; guint8 *data; READ_UINT16 (&reader, pps_length, 16); pps_length -= 1; SKIP (&reader, 8); data = GST_BUFFER_DATA (buf) + gst_bit_reader_get_pos (&reader) / 8; if (!gst_h264_parser_parse_picture (h264_dec->parser, data, pps_length)) return FALSE; SKIP (&reader, pps_length * 8); } } return TRUE; }
static GstFlowReturn gst_vdp_h264_dec_parse_data (GstBaseVideoDecoder * base_video_decoder, GstBuffer * buf, gboolean at_eos, GstVideoFrame * frame) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (base_video_decoder); GstBitReader reader; GstNalUnit nal_unit; guint8 forbidden_zero_bit; guint8 *data; guint size; gint i; GstFlowReturn ret = GST_FLOW_OK; GST_MEMDUMP ("data", GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf)); gst_bit_reader_init_from_buffer (&reader, buf); if (gst_bit_reader_get_remaining (&reader) < h264_dec->nal_length_size * 8 + 7) goto invalid_packet; /* skip nal_length or sync code */ gst_bit_reader_skip_unchecked (&reader, h264_dec->nal_length_size * 8); forbidden_zero_bit = gst_bit_reader_get_bits_uint8_unchecked (&reader, 1); if (forbidden_zero_bit != 0) { GST_WARNING ("forbidden_zero_bit != 0"); return GST_FLOW_ERROR; } nal_unit.ref_idc = gst_bit_reader_get_bits_uint16_unchecked (&reader, 2); GST_DEBUG ("nal_ref_idc: %u", nal_unit.ref_idc); /* read nal_unit_type */ nal_unit.type = gst_bit_reader_get_bits_uint16_unchecked (&reader, 5); GST_DEBUG ("nal_unit_type: %u", nal_unit.type); if (nal_unit.type == 14 || nal_unit.type == 20) { if (!gst_bit_reader_skip (&reader, 24)) goto invalid_packet; } nal_unit.IdrPicFlag = (nal_unit.type == 5 ? 1 : 0); data = GST_BUFFER_DATA (buf) + gst_bit_reader_get_pos (&reader) / 8; size = gst_bit_reader_get_remaining (&reader) / 8; i = size - 1; while ((gint) size > 0 && data[i] == 0x00) { size--; i--; } if (GST_VIDEO_FRAME_FLAG_IS_SET (frame, GST_H264_FRAME_GOT_PRIMARY)) { if (nal_unit.type == GST_NAL_SPS || nal_unit.type == GST_NAL_PPS || nal_unit.type == GST_NAL_SEI || nal_unit.type == GST_NAL_AU_DELIMITER || (nal_unit.type >= 14 && nal_unit.type <= 18)) ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE, &frame); } if (nal_unit.type >= GST_NAL_SLICE && nal_unit.type <= GST_NAL_SLICE_IDR) { GstH264Slice slice; if (!gst_h264_parser_parse_slice_header (h264_dec->parser, &slice, data, size, nal_unit)) goto invalid_packet; if (slice.redundant_pic_cnt == 0) { if (GST_VIDEO_FRAME_FLAG_IS_SET (frame, GST_H264_FRAME_GOT_PRIMARY)) { GstH264Slice *p_slice; guint8 pic_order_cnt_type, p_pic_order_cnt_type; gboolean finish_frame = FALSE; p_slice = &(GST_H264_FRAME_CAST (frame)->slice_hdr); pic_order_cnt_type = slice.picture->sequence->pic_order_cnt_type; p_pic_order_cnt_type = p_slice->picture->sequence->pic_order_cnt_type; if (slice.frame_num != p_slice->frame_num) finish_frame = TRUE; else if (slice.picture != p_slice->picture) finish_frame = TRUE; else if (slice.bottom_field_flag != p_slice->bottom_field_flag) finish_frame = TRUE; else if (nal_unit.ref_idc != p_slice->nal_unit.ref_idc && (nal_unit.ref_idc == 0 || p_slice->nal_unit.ref_idc == 0)) finish_frame = TRUE; else if ((pic_order_cnt_type == 0 && p_pic_order_cnt_type == 0) && (slice.pic_order_cnt_lsb != p_slice->pic_order_cnt_lsb || slice.delta_pic_order_cnt_bottom != p_slice->delta_pic_order_cnt_bottom)) finish_frame = TRUE; else if ((p_pic_order_cnt_type == 1 && p_pic_order_cnt_type == 1) && (slice.delta_pic_order_cnt[0] != p_slice->delta_pic_order_cnt[0] || slice.delta_pic_order_cnt[1] != p_slice->delta_pic_order_cnt[1])) finish_frame = TRUE; if (finish_frame) ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE, &frame); } if (!GST_VIDEO_FRAME_FLAG_IS_SET (frame, GST_H264_FRAME_GOT_PRIMARY)) { if (GST_H264_IS_I_SLICE (slice.type) || GST_H264_IS_SI_SLICE (slice.type)) GST_VIDEO_FRAME_FLAG_SET (frame, GST_VIDEO_FRAME_FLAG_KEYFRAME); GST_H264_FRAME_CAST (frame)->slice_hdr = slice; GST_VIDEO_FRAME_FLAG_SET (frame, GST_H264_FRAME_GOT_PRIMARY); } } gst_h264_frame_add_slice ((GstH264Frame *) frame, buf); } if (nal_unit.type == GST_NAL_SPS) { if (!gst_h264_parser_parse_sequence (h264_dec->parser, data, size)) goto invalid_packet; } if (nal_unit.type == GST_NAL_PPS) { if (!gst_h264_parser_parse_picture (h264_dec->parser, data, size)) goto invalid_packet; } gst_buffer_unref (buf); return ret; invalid_packet: GST_WARNING ("Invalid packet size!"); gst_buffer_unref (buf); return GST_FLOW_OK; }
static GstBaseVideoDecoderScanResult gst_vdp_h264_dec_scan_for_packet_end (GstBaseVideoDecoder * base_video_decoder, GstAdapter * adapter, guint * size, gboolean at_eos) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (base_video_decoder); guint avail; avail = gst_adapter_available (adapter); if (avail < h264_dec->nal_length_size) return GST_BASE_VIDEO_DECODER_SCAN_RESULT_NEED_DATA; if (h264_dec->packetized) { guint8 *data; gint i; guint32 nal_length = 0; data = g_slice_alloc (h264_dec->nal_length_size); gst_adapter_copy (adapter, data, 0, h264_dec->nal_length_size); for (i = 0; i < h264_dec->nal_length_size; i++) nal_length = (nal_length << 8) | data[i]; g_slice_free1 (h264_dec->nal_length_size, data); nal_length += h264_dec->nal_length_size; /* check for invalid NALU sizes, assume the size if the available bytes * when something is fishy */ if (nal_length <= 1 || nal_length > avail) { nal_length = avail - h264_dec->nal_length_size; GST_DEBUG ("fixing invalid NALU size to %u", nal_length); } *size = nal_length; } else { guint8 *data; guint32 start_code; guint n; data = g_slice_alloc (SYNC_CODE_SIZE); gst_adapter_copy (adapter, data, 0, SYNC_CODE_SIZE); start_code = ((data[0] << 16) && (data[1] << 8) && data[2]); g_slice_free1 (SYNC_CODE_SIZE, data); GST_DEBUG ("start_code: %d", start_code); if (start_code == 0x000001) return GST_BASE_VIDEO_DECODER_SCAN_RESULT_LOST_SYNC; n = gst_adapter_masked_scan_uint32 (adapter, 0xffffff00, 0x00000100, SYNC_CODE_SIZE, avail - SYNC_CODE_SIZE); if (n == -1) return GST_BASE_VIDEO_DECODER_SCAN_RESULT_NEED_DATA; *size = n; } GST_DEBUG ("NAL size: %d", *size); return GST_BASE_VIDEO_DECODER_SCAN_RESULT_OK; }
static GstFlowReturn gst_vdp_h264_dec_handle_frame (GstBaseVideoDecoder * base_video_decoder, GstVideoFrame * frame, GstClockTimeDiff deadline) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (base_video_decoder); GstH264Frame *h264_frame; GstH264Slice *slice; GstH264Picture *pic G_GNUC_UNUSED; GstH264Sequence *seq G_GNUC_UNUSED; GstFlowReturn ret; GstVdpVideoBuffer *outbuf; VdpPictureInfoH264 info; VdpBitstreamBuffer *bufs; guint n_bufs; GST_DEBUG ("handle_frame"); h264_frame = GST_H264_FRAME_CAST (frame); slice = &h264_frame->slice_hdr; pic = slice->picture; seq = pic->sequence; if (slice->nal_unit.IdrPicFlag) { ret = gst_vdp_h264_dec_idr (h264_dec, h264_frame); if (ret == GST_FLOW_OK) h264_dec->got_idr = TRUE; else { gst_base_video_decoder_skip_frame (base_video_decoder, frame); return GST_FLOW_OK; } } /* check if we've got a IDR frame yet */ if (!h264_dec->got_idr) { gst_base_video_decoder_skip_frame (base_video_decoder, frame); return GST_FLOW_OK; } gst_vdp_h264_dec_init_frame_info (h264_dec, h264_frame); info = gst_vdp_h264_dec_fill_info (h264_dec, h264_frame); bufs = gst_vdp_h264_dec_create_bitstream_buffers (h264_dec, h264_frame, &n_bufs); ret = gst_vdp_decoder_render (GST_VDP_DECODER (h264_dec), (VdpPictureInfo *) & info, n_bufs, bufs, &outbuf); g_free (bufs); if (ret != GST_FLOW_OK) { gst_base_video_decoder_skip_frame (base_video_decoder, frame); return ret; } frame->src_buffer = GST_BUFFER_CAST (outbuf); /* DPB handling */ if (slice->nal_unit.ref_idc != 0 && !slice->nal_unit.IdrPicFlag) { if (slice->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) { GstH264RefPicMarking *marking; guint i; marking = slice->dec_ref_pic_marking.ref_pic_marking; for (i = 0; i < slice->dec_ref_pic_marking.n_ref_pic_marking; i++) { switch (marking[i].memory_management_control_operation) { case 1: { guint16 pic_num; pic_num = slice->frame_num - (marking[i].difference_of_pic_nums_minus1 + 1); gst_h264_dpb_mark_short_term_unused (h264_dec->dpb, pic_num); break; } case 2: { gst_h264_dpb_mark_long_term_unused (h264_dec->dpb, marking[i].long_term_pic_num); break; } case 3: { guint16 pic_num; pic_num = slice->frame_num - (marking[i].difference_of_pic_nums_minus1 + 1); gst_h264_dpb_mark_long_term (h264_dec->dpb, pic_num, marking[i].long_term_frame_idx); break; } case 4: { g_object_set (h264_dec->dpb, "max-longterm-frame-idx", marking[i].max_long_term_frame_idx_plus1 - 1, NULL); break; } case 5: { gst_h264_dpb_mark_all_unused (h264_dec->dpb); g_object_set (h264_dec->dpb, "max-longterm-frame-idx", -1, NULL); break; } default: break; } } } else gst_h264_dpb_mark_sliding (h264_dec->dpb); } return gst_h264_dpb_add (h264_dec->dpb, h264_frame); }
static GstFlowReturn gst_vdp_h264_dec_handle_frame (GstVideoDecoder * video_decoder, GstVideoCodecFrame * frame) { GstVdpH264Dec *h264_dec = GST_VDP_H264_DEC (video_decoder); GstH264Meta *h264_meta; GstH264Frame *h264_frame; GList *tmp; GstFlowReturn ret; VdpPictureInfoH264 info; VdpBitstreamBuffer *bufs; GstH264SliceHdr *first_slice; guint i; GstMapInfo map; GST_DEBUG ("handle_frame"); h264_meta = gst_buffer_get_h264_meta (frame->input_buffer); if (G_UNLIKELY (h264_meta == NULL)) goto no_h264_meta; if (G_UNLIKELY (h264_meta->num_slices == 0)) goto no_slices; /* Handle PPS/SPS/SEI if present */ if (h264_meta->sps) { for (tmp = h264_meta->sps; tmp; tmp = tmp->next) { GstH264SPS *sps = (GstH264SPS *) tmp->data; GST_LOG_OBJECT (h264_dec, "Storing SPS %d", sps->id); h264_dec->sps[sps->id] = g_slice_dup (GstH264SPS, sps); } } if (h264_meta->pps) { for (tmp = h264_meta->pps; tmp; tmp = tmp->next) { GstH264PPS *pps = (GstH264PPS *) tmp->data; GST_LOG_OBJECT (h264_dec, "Storing PPS %d", pps->id); h264_dec->pps[pps->id] = g_slice_dup (GstH264PPS, pps); /* Adjust pps pointer */ h264_dec->pps[pps->id]->sequence = h264_dec->sps[pps->sps_id]; } } first_slice = &h264_meta->slices[0]; if (!h264_dec->got_idr && first_slice->slice_type != GST_H264_NAL_SLICE_IDR) goto no_idr; /* Handle slices */ for (i = 0; i < h264_meta->num_slices; i++) { GstH264SliceHdr *slice = &h264_meta->slices[i]; GST_LOG_OBJECT (h264_dec, "Handling slice #%d", i); slice->pps = h264_dec->pps[slice->pps_id]; } if (first_slice->slice_type == GST_H264_NAL_SLICE_IDR) { ret = gst_vdp_h264_dec_idr (h264_dec, frame, first_slice); if (ret == GST_FLOW_OK) h264_dec->got_idr = TRUE; else goto skip_frame; } h264_frame = g_slice_new0 (GstH264Frame); gst_video_codec_frame_set_user_data (frame, h264_frame, (GDestroyNotify) gst_h264_frame_free); gst_vdp_h264_dec_init_frame_info (h264_dec, h264_frame, first_slice); h264_frame->frame = frame; gst_vdp_h264_dec_fill_info (&info, h264_dec, h264_frame, first_slice); info.slice_count = h264_meta->num_slices; if (!gst_buffer_map (frame->input_buffer, &map, GST_MAP_READ)) goto map_fail; bufs = gst_vdp_h264_dec_create_bitstream_buffers (h264_dec, h264_meta, &map); ret = gst_vdp_decoder_render (GST_VDP_DECODER (h264_dec), (VdpPictureInfo *) & info, h264_meta->num_slices, bufs, frame); g_free (bufs); gst_buffer_unmap (frame->input_buffer, &map); if (ret != GST_FLOW_OK) goto render_fail; /* DPB handling */ return gst_vdp_h264_dec_handle_dpb (h264_dec, h264_frame, first_slice); /* EARLY exit */ no_idr: { GST_DEBUG_OBJECT (video_decoder, "Didn't see a IDR yet, skipping frame"); return gst_video_decoder_finish_frame (video_decoder, frame); } skip_frame: { GST_DEBUG_OBJECT (video_decoder, "Skipping frame"); return gst_video_decoder_finish_frame (video_decoder, frame); } /* ERRORS */ no_h264_meta: { GST_ERROR_OBJECT (video_decoder, "Input buffer doesn't have GstH264Meta"); return GST_FLOW_ERROR; } no_slices: { GST_ERROR_OBJECT (video_decoder, "Input buffer doesn't have any slices"); return GST_FLOW_ERROR; } map_fail: { GST_ERROR_OBJECT (video_decoder, "Failed to map input buffer for READ"); return GST_FLOW_ERROR; } render_fail: { GST_ERROR_OBJECT (video_decoder, "Failed to render : %s", gst_flow_get_name (ret)); gst_video_decoder_drop_frame (video_decoder, frame); return ret; } }