453 GST_DEBUG_OBJECT (trans, " to: %" GST_PTR_FORMAT, temp); |
483 GST_DEBUG_OBJECT (trans, " to: %" GST_PTR_FORMAT, temp); |
454 |
484 |
455 temp = gst_caps_make_writable (temp); |
485 temp = gst_caps_make_writable (temp); |
456 gst_caps_append (ret, temp); |
486 gst_caps_append (ret, temp); |
457 } else { |
487 } else { |
|
488 gint n = gst_caps_get_size (caps); |
458 /* we send caps with just one structure to the transform |
489 /* we send caps with just one structure to the transform |
459 * function as this is easier for the element */ |
490 * function as this is easier for the element */ |
460 for (i = 0; i < gst_caps_get_size (caps); i++) { |
491 for (i = 0; i < n; i++) { |
461 GstCaps *nth; |
492 GstCaps *nth; |
462 |
493 |
463 nth = gst_caps_copy_nth (caps, i); |
494 nth = gst_caps_copy_nth (caps, i); |
464 GST_DEBUG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth); |
495 GST_LOG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth); |
465 temp = klass->transform_caps (trans, direction, nth); |
496 temp = klass->transform_caps (trans, direction, nth); |
466 gst_caps_unref (nth); |
497 gst_caps_unref (nth); |
467 GST_DEBUG_OBJECT (trans, " to[%d]: %" GST_PTR_FORMAT, i, temp); |
498 GST_LOG_OBJECT (trans, " to[%d]: %" GST_PTR_FORMAT, i, temp); |
468 |
499 |
469 temp = gst_caps_make_writable (temp); |
500 temp = gst_caps_make_writable (temp); |
470 /* FIXME: here we need to only append those structures, that are not yet |
501 |
471 * in there |
502 /* here we need to only append those structures, that are not yet |
472 * gst_caps_append (ret, temp); |
503 * in there, we use the merge function for this */ |
473 */ |
|
474 gst_caps_merge (ret, temp); |
504 gst_caps_merge (ret, temp); |
|
505 |
|
506 GST_LOG_OBJECT (trans, " merged[%d]: %" GST_PTR_FORMAT, i, ret); |
475 } |
507 } |
476 GST_DEBUG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret)); |
508 GST_LOG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret)); |
477 /* now simplify caps |
509 /* FIXME: we can't do much simplification here because we don't really want to |
478 gst_caps_do_simplify (ret); |
510 * change the caps order |
479 GST_DEBUG_OBJECT (trans, "simplified: (%d)", gst_caps_get_size (ret)); |
511 gst_caps_do_simplify (ret); |
|
512 GST_DEBUG_OBJECT (trans, "simplified: (%d)", gst_caps_get_size (ret)); |
480 */ |
513 */ |
481 } |
514 } |
482 } else { |
515 } else { |
483 /* else use the identity transform */ |
516 GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps); |
|
517 /* no transform function, use the identity transform */ |
484 ret = gst_caps_ref (caps); |
518 ret = gst_caps_ref (caps); |
485 } |
519 } |
486 |
520 |
487 GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret), |
521 GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret), |
488 ret); |
522 ret); |
489 |
523 |
490 return ret; |
524 return ret; |
491 } |
525 } |
492 |
526 |
|
527 /* transform a buffer of @size with @caps on the pad with @direction to |
|
528 * the size of a buffer with @othercaps and store the result in @othersize |
|
529 * |
|
530 * We have two ways of doing this: |
|
531 * 1) use a custom transform size function, this is for complicated custom |
|
532 * cases with no fixed unit_size. |
|
533 * 2) use the unit_size functions where there is a relationship between the |
|
534 * caps and the size of a buffer. |
|
535 */ |
493 static gboolean |
536 static gboolean |
494 gst_base_transform_transform_size (GstBaseTransform * trans, |
537 gst_base_transform_transform_size (GstBaseTransform * trans, |
495 GstPadDirection direction, GstCaps * caps, |
538 GstPadDirection direction, GstCaps * caps, |
496 guint size, GstCaps * othercaps, guint * othersize) |
539 guint size, GstCaps * othercaps, guint * othersize) |
497 { |
540 { |
642 trans->negotiated = ret; |
711 trans->negotiated = ret; |
643 |
712 |
644 return ret; |
713 return ret; |
645 } |
714 } |
646 |
715 |
647 /* called when new caps arrive on the sink or source pad */ |
716 /* check if caps @in on @pad can be transformed to @out on the other pad. |
|
717 * We don't have a vmethod to test this yet so we have to do a somewhat less |
|
718 * efficient check for this. |
|
719 */ |
648 static gboolean |
720 static gboolean |
649 gst_base_transform_setcaps (GstPad * pad, GstCaps * caps) |
721 gst_base_transform_can_transform (GstBaseTransform * trans, GstPad * pad, |
650 { |
722 GstCaps * in, GstCaps * out) |
651 GstBaseTransform *trans; |
723 { |
|
724 GstCaps *othercaps; |
|
725 |
|
726 /* convert the in caps to all possible out caps */ |
|
727 othercaps = |
|
728 gst_base_transform_transform_caps (trans, GST_PAD_DIRECTION (pad), in); |
|
729 |
|
730 /* check if transform is empty */ |
|
731 if (!othercaps || gst_caps_is_empty (othercaps)) |
|
732 goto no_transform; |
|
733 |
|
734 /* check if the out caps is a subset of the othercaps */ |
|
735 if (!gst_caps_is_subset (out, othercaps)) |
|
736 goto no_subset; |
|
737 |
|
738 if (othercaps) |
|
739 gst_caps_unref (othercaps); |
|
740 |
|
741 GST_DEBUG_OBJECT (trans, "from %" GST_PTR_FORMAT, in); |
|
742 GST_DEBUG_OBJECT (trans, "to %" GST_PTR_FORMAT, out); |
|
743 |
|
744 return TRUE; |
|
745 |
|
746 /* ERRORS */ |
|
747 no_transform: |
|
748 { |
|
749 GST_DEBUG_OBJECT (trans, |
|
750 "transform returned useless %" GST_PTR_FORMAT, othercaps); |
|
751 if (othercaps) |
|
752 gst_caps_unref (othercaps); |
|
753 return FALSE; |
|
754 } |
|
755 no_subset: |
|
756 { |
|
757 GST_DEBUG_OBJECT (trans, "no subset"); |
|
758 if (othercaps) |
|
759 gst_caps_unref (othercaps); |
|
760 return FALSE; |
|
761 } |
|
762 } |
|
763 |
|
764 /* given a fixed @caps on @pad, create the best possible caps for the |
|
765 * other pad. |
|
766 * @caps must be fixed when calling this function. |
|
767 * |
|
768 * This function calls the transform caps vmethod of the basetransform to figure |
|
769 * out the possible target formats. It then tries to select the best format from |
|
770 * this list by: |
|
771 * |
|
772 * - attempt passthrough if the target caps is a superset of the input caps |
|
773 * - fixating by using peer caps |
|
774 * - fixating with transform fixate function |
|
775 * - fixating with pad fixate functions. |
|
776 * |
|
777 * this function returns a caps that can be transformed into and is accepted by |
|
778 * the peer element. |
|
779 */ |
|
780 static GstCaps * |
|
781 gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad, |
|
782 GstCaps * caps) |
|
783 { |
652 GstBaseTransformClass *klass; |
784 GstBaseTransformClass *klass; |
653 GstPad *otherpad, *otherpeer; |
785 GstPad *otherpad, *otherpeer; |
654 GstCaps *othercaps = NULL; |
786 GstCaps *othercaps; |
655 gboolean ret = TRUE; |
|
656 gboolean peer_checked = FALSE; |
787 gboolean peer_checked = FALSE; |
657 |
788 |
658 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad)); |
789 /* caps must be fixed here, this is a programming error if it's not */ |
|
790 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL); |
|
791 |
659 klass = GST_BASE_TRANSFORM_GET_CLASS (trans); |
792 klass = GST_BASE_TRANSFORM_GET_CLASS (trans); |
660 |
793 |
661 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad; |
794 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad; |
662 otherpeer = gst_pad_get_peer (otherpad); |
795 otherpeer = gst_pad_get_peer (otherpad); |
663 |
796 |
664 /* if we get called recursively, we bail out now to avoid an |
797 /* see how we can transform the input caps. We need to do this even for |
665 * infinite loop. */ |
798 * passthrough because it might be possible that this element cannot support |
666 if (GST_PAD_IS_IN_SETCAPS (otherpad)) |
799 * passthrough at all. */ |
667 goto done; |
|
668 |
|
669 /* caps must be fixed here */ |
|
670 if (!gst_caps_is_fixed (caps)) |
|
671 goto unfixed_caps; |
|
672 |
|
673 /* see how we can transform the input caps. */ |
|
674 othercaps = gst_base_transform_transform_caps (trans, |
800 othercaps = gst_base_transform_transform_caps (trans, |
675 GST_PAD_DIRECTION (pad), caps); |
801 GST_PAD_DIRECTION (pad), caps); |
676 |
802 |
677 /* The caps we can actually output is the intersection of the transformed |
803 /* The caps we can actually output is the intersection of the transformed |
678 * caps with the pad template for the pad */ |
804 * caps with the pad template for the pad */ |
679 if (othercaps) { |
805 if (othercaps) { |
680 GstCaps *intersect; |
806 GstCaps *intersect; |
681 const GstCaps *templ_caps; |
807 const GstCaps *templ_caps; |
682 |
808 |
683 templ_caps = gst_pad_get_pad_template_caps (otherpad); |
809 templ_caps = gst_pad_get_pad_template_caps (otherpad); |
|
810 GST_DEBUG_OBJECT (trans, |
|
811 "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps); |
|
812 |
684 intersect = gst_caps_intersect (othercaps, templ_caps); |
813 intersect = gst_caps_intersect (othercaps, templ_caps); |
685 |
814 |
686 gst_caps_unref (othercaps); |
815 gst_caps_unref (othercaps); |
687 othercaps = intersect; |
816 othercaps = intersect; |
688 } |
817 } |
796 goto peer_no_accept; |
945 goto peer_no_accept; |
797 |
946 |
798 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT |
947 GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT |
799 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps); |
948 ", and got final caps %" GST_PTR_FORMAT, caps, othercaps); |
800 |
949 |
801 trans->have_same_caps = gst_caps_is_equal (caps, othercaps); |
950 if (otherpeer) |
802 GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps); |
951 gst_object_unref (otherpeer); |
803 |
952 |
804 /* see if we have to configure the element now */ |
953 return othercaps; |
805 if (!trans->delay_configure) { |
954 |
806 GstCaps *incaps, *outcaps; |
955 /* ERRORS */ |
807 |
956 no_transform: |
808 /* make sure in and out caps are correct */ |
957 { |
809 if (pad == trans->sinkpad) { |
958 GST_DEBUG_OBJECT (trans, |
810 incaps = caps; |
959 "transform returned useless %" GST_PTR_FORMAT, othercaps); |
811 outcaps = othercaps; |
960 goto error_cleanup; |
812 } else { |
961 } |
813 incaps = othercaps; |
962 no_transform_possible: |
814 outcaps = caps; |
963 { |
815 } |
964 GST_DEBUG_OBJECT (trans, |
816 /* call configure now */ |
965 "transform could not transform %" GST_PTR_FORMAT |
817 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps))) |
966 " in anything we support", caps); |
818 goto failed_configure; |
967 goto error_cleanup; |
|
968 } |
|
969 could_not_fixate: |
|
970 { |
|
971 GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps); |
|
972 goto error_cleanup; |
|
973 } |
|
974 peer_no_accept: |
|
975 { |
|
976 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT |
|
977 " to accept %" GST_PTR_FORMAT, otherpad, othercaps); |
|
978 goto error_cleanup; |
|
979 } |
|
980 error_cleanup: |
|
981 { |
|
982 if (otherpeer) |
|
983 gst_object_unref (otherpeer); |
|
984 if (othercaps) |
|
985 gst_caps_unref (othercaps); |
|
986 return NULL; |
|
987 } |
|
988 } |
|
989 |
|
990 static gboolean |
|
991 gst_base_transform_acceptcaps (GstPad * pad, GstCaps * caps) |
|
992 { |
|
993 GstBaseTransform *trans; |
|
994 #if 0 |
|
995 GstPad *otherpad; |
|
996 GstCaps *othercaps = NULL; |
|
997 #endif |
|
998 gboolean ret = TRUE; |
|
999 |
|
1000 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad)); |
|
1001 |
|
1002 #if 0 |
|
1003 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad; |
|
1004 |
|
1005 /* we need fixed caps for the check, fall back to the default implementation |
|
1006 * if we don't */ |
|
1007 if (!gst_caps_is_fixed (caps)) |
|
1008 #endif |
|
1009 { |
|
1010 GstCaps *allowed, *intersect; |
|
1011 |
|
1012 GST_DEBUG_OBJECT (pad, "non fixed accept caps %" GST_PTR_FORMAT, caps); |
|
1013 |
|
1014 /* get all the formats we can handle on this pad */ |
|
1015 allowed = gst_pad_get_caps (pad); |
|
1016 if (!allowed) |
|
1017 goto no_transform_possible; |
|
1018 |
|
1019 /* intersect with the requested format */ |
|
1020 intersect = gst_caps_intersect (allowed, caps); |
|
1021 |
|
1022 GST_DEBUG_OBJECT (pad, "intersection %" GST_PTR_FORMAT, intersect); |
|
1023 |
|
1024 /* we can accept if the intersection is not empty */ |
|
1025 ret = !gst_caps_is_empty (intersect); |
|
1026 gst_caps_unref (intersect); |
|
1027 gst_caps_unref (allowed); |
|
1028 |
|
1029 if (!ret) |
|
1030 goto no_transform_possible; |
|
1031 } |
|
1032 #if 0 |
|
1033 else { |
|
1034 GST_DEBUG_OBJECT (pad, "accept caps %" GST_PTR_FORMAT, caps); |
|
1035 |
|
1036 /* find best possible caps for the other pad as a way to see if we can |
|
1037 * transform this caps. */ |
|
1038 othercaps = gst_base_transform_find_transform (trans, pad, caps); |
|
1039 if (!othercaps || gst_caps_is_empty (othercaps)) |
|
1040 goto no_transform_possible; |
|
1041 |
|
1042 GST_DEBUG_OBJECT (pad, "we can transform to %" GST_PTR_FORMAT, othercaps); |
|
1043 } |
|
1044 #endif |
|
1045 |
|
1046 done: |
|
1047 #if 0 |
|
1048 /* We know it's always NULL since we never use it */ |
|
1049 if (othercaps) |
|
1050 gst_caps_unref (othercaps); |
|
1051 #endif |
|
1052 gst_object_unref (trans); |
|
1053 |
|
1054 return ret; |
|
1055 |
|
1056 /* ERRORS */ |
|
1057 no_transform_possible: |
|
1058 { |
|
1059 GST_WARNING_OBJECT (trans, |
|
1060 "transform could not transform %" GST_PTR_FORMAT |
|
1061 " in anything we support", caps); |
|
1062 ret = FALSE; |
|
1063 goto done; |
|
1064 } |
|
1065 } |
|
1066 |
|
1067 /* called when new caps arrive on the sink or source pad, |
|
1068 * We try to find the best caps for the other side using our _find_transform() |
|
1069 * function. If there are caps, we configure the transform for this new |
|
1070 * transformation. |
|
1071 * |
|
1072 * FIXME, this function is currently commutative but this should not really be |
|
1073 * because we never set caps starting from the srcpad. |
|
1074 */ |
|
1075 static gboolean |
|
1076 gst_base_transform_setcaps (GstPad * pad, GstCaps * caps) |
|
1077 { |
|
1078 GstBaseTransform *trans; |
|
1079 GstPad *otherpad, *otherpeer; |
|
1080 GstCaps *othercaps = NULL; |
|
1081 gboolean ret = TRUE; |
|
1082 GstCaps *incaps, *outcaps; |
|
1083 |
|
1084 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad)); |
|
1085 |
|
1086 otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad; |
|
1087 otherpeer = gst_pad_get_peer (otherpad); |
|
1088 |
|
1089 /* if we get called recursively, we bail out now to avoid an |
|
1090 * infinite loop. */ |
|
1091 if (GST_PAD_IS_IN_SETCAPS (otherpad)) |
|
1092 goto done; |
|
1093 |
|
1094 GST_DEBUG_OBJECT (pad, "have new caps %" GST_PTR_FORMAT, caps); |
|
1095 |
|
1096 /* find best possible caps for the other pad */ |
|
1097 othercaps = gst_base_transform_find_transform (trans, pad, caps); |
|
1098 if (!othercaps || gst_caps_is_empty (othercaps)) |
|
1099 goto no_transform_possible; |
|
1100 |
|
1101 /* configure the element now */ |
|
1102 /* make sure in and out caps are correct */ |
|
1103 if (pad == trans->sinkpad) { |
|
1104 incaps = caps; |
|
1105 outcaps = othercaps; |
819 } else { |
1106 } else { |
820 /* set pending configure, the configure will happen later with the |
1107 incaps = othercaps; |
821 * caps we set on the pads above. */ |
1108 outcaps = caps; |
822 trans->pending_configure = TRUE; |
1109 } |
823 } |
1110 |
|
1111 #if 0 |
|
1112 /* if we have the same caps, we can optimize and reuse the input caps */ |
|
1113 if (gst_caps_is_equal (incaps, outcaps)) { |
|
1114 GST_INFO_OBJECT (trans, "reuse caps"); |
|
1115 gst_caps_unref (outcaps); |
|
1116 outcaps = gst_caps_ref (incaps); |
|
1117 } |
|
1118 #endif |
|
1119 |
|
1120 /* call configure now */ |
|
1121 if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps))) |
|
1122 goto failed_configure; |
824 |
1123 |
825 /* we know this will work, we implement the setcaps */ |
1124 /* we know this will work, we implement the setcaps */ |
826 gst_pad_set_caps (otherpad, othercaps); |
1125 gst_pad_set_caps (otherpad, othercaps); |
827 |
1126 |
828 if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) { |
1127 if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) { |
|
1128 /* FIXME hm? */ |
829 ret &= gst_pad_set_caps (otherpeer, othercaps); |
1129 ret &= gst_pad_set_caps (otherpeer, othercaps); |
830 if (!ret) { |
1130 if (!ret) { |
831 GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed", |
1131 GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed", |
832 othercaps); |
1132 othercaps); |
833 } |
1133 } |
844 gst_object_unref (trans); |
1144 gst_object_unref (trans); |
845 |
1145 |
846 return ret; |
1146 return ret; |
847 |
1147 |
848 /* ERRORS */ |
1148 /* ERRORS */ |
849 unfixed_caps: |
|
850 { |
|
851 GST_DEBUG_OBJECT (trans, "caps are not fixed %" GST_PTR_FORMAT, caps); |
|
852 ret = FALSE; |
|
853 goto done; |
|
854 } |
|
855 no_transform: |
|
856 { |
|
857 GST_DEBUG_OBJECT (trans, |
|
858 "transform returned useless %" GST_PTR_FORMAT, othercaps); |
|
859 ret = FALSE; |
|
860 goto done; |
|
861 } |
|
862 no_transform_possible: |
1149 no_transform_possible: |
863 { |
1150 { |
864 GST_DEBUG_OBJECT (trans, |
1151 GST_WARNING_OBJECT (trans, |
865 "transform could not transform %" GST_PTR_FORMAT |
1152 "transform could not transform %" GST_PTR_FORMAT |
866 " in anything we support", caps); |
1153 " in anything we support", caps); |
867 ret = FALSE; |
1154 ret = FALSE; |
868 goto done; |
1155 goto done; |
869 } |
1156 } |
870 could_not_fixate: |
1157 failed_configure: |
871 { |
1158 { |
872 GST_ERROR_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps); |
1159 GST_WARNING_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT |
873 ret = FALSE; |
|
874 goto done; |
|
875 } |
|
876 peer_no_accept: |
|
877 { |
|
878 GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT |
|
879 " to accept %" GST_PTR_FORMAT, otherpad, othercaps); |
1160 " to accept %" GST_PTR_FORMAT, otherpad, othercaps); |
880 ret = FALSE; |
1161 ret = FALSE; |
881 goto done; |
1162 goto done; |
882 } |
1163 } |
883 failed_configure: |
|
884 { |
|
885 GST_DEBUG_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT |
|
886 " to accept %" GST_PTR_FORMAT, otherpad, othercaps); |
|
887 ret = FALSE; |
|
888 goto done; |
|
889 } |
|
890 } |
1164 } |
891 |
1165 |
892 /* Allocate a buffer using gst_pad_alloc_buffer |
1166 /* Allocate a buffer using gst_pad_alloc_buffer |
893 * |
1167 * |
894 * This function does not do renegotiation on the source pad |
1168 * This function can do renegotiation on the source pad |
895 * |
1169 * |
896 * The output buffer is always writable. outbuf can be equal to |
1170 * The output buffer is always writable. outbuf can be equal to |
897 * inbuf, the caller should be prepared for this and perform |
1171 * inbuf, the caller should be prepared for this and perform |
898 * appropriate refcounting. |
1172 * appropriate refcounting. |
899 */ |
1173 */ |
900 static GstFlowReturn |
1174 static GstFlowReturn |
901 gst_base_transform_prepare_output_buffer (GstBaseTransform * trans, |
1175 gst_base_transform_prepare_output_buffer (GstBaseTransform * trans, |
902 GstBuffer * in_buf, gint out_size, GstCaps * out_caps, GstBuffer ** out_buf) |
1176 GstBuffer * in_buf, GstBuffer ** out_buf) |
903 { |
1177 { |
904 GstBaseTransformClass *bclass; |
1178 GstBaseTransformClass *bclass; |
|
1179 GstBaseTransformPrivate *priv; |
905 GstFlowReturn ret = GST_FLOW_OK; |
1180 GstFlowReturn ret = GST_FLOW_OK; |
906 gboolean copy_inbuf = FALSE; |
1181 guint outsize, newsize, expsize; |
|
1182 gboolean discard; |
|
1183 GstCaps *incaps, *oldcaps, *newcaps; |
907 |
1184 |
908 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans); |
1185 bclass = GST_BASE_TRANSFORM_GET_CLASS (trans); |
909 |
1186 |
910 /* we cannot reconfigure the element yet as we are still processing |
1187 priv = trans->priv; |
911 * the old buffer. We will therefore delay the reconfiguration of the |
1188 |
912 * element until we have processed this last buffer. */ |
1189 *out_buf = NULL; |
913 trans->delay_configure = TRUE; |
1190 |
914 |
1191 /* figure out how to allocate a buffer based on the current configuration */ |
915 /* out_caps is the caps of the src pad gathered through the GST_PAD_CAPS |
1192 if (trans->passthrough) { |
916 macro. If a set_caps occurs during this function this caps will become |
1193 GST_DEBUG_OBJECT (trans, "doing passthrough alloc"); |
917 invalid. We want to keep them during preparation of the output buffer. */ |
1194 /* passthrough, we don't really need to call pad alloc but we still need to |
918 if (out_caps) |
1195 * in order to get upstream negotiation. The output size is the same as the |
919 gst_caps_ref (out_caps); |
1196 * input size. */ |
920 |
1197 outsize = GST_BUFFER_SIZE (in_buf); |
921 /* see if the subclass wants to alloc a buffer */ |
1198 /* we always alloc and discard here */ |
|
1199 discard = TRUE; |
|
1200 } else { |
|
1201 gboolean want_in_place = (bclass->transform_ip != NULL) |
|
1202 && trans->always_in_place; |
|
1203 |
|
1204 if (want_in_place) { |
|
1205 GST_DEBUG_OBJECT (trans, "doing inplace alloc"); |
|
1206 /* we alloc a buffer of the same size as the input */ |
|
1207 outsize = GST_BUFFER_SIZE (in_buf); |
|
1208 /* only discard it when the input was not writable, otherwise, we reuse |
|
1209 * the input buffer. */ |
|
1210 discard = gst_buffer_is_writable (in_buf); |
|
1211 GST_DEBUG_OBJECT (trans, "discard: %d", discard); |
|
1212 } else { |
|
1213 GST_DEBUG_OBJECT (trans, "getting output size for copy transform"); |
|
1214 /* copy transform, figure out the output size */ |
|
1215 if (!gst_base_transform_transform_size (trans, |
|
1216 GST_PAD_SINK, GST_PAD_CAPS (trans->sinkpad), |
|
1217 GST_BUFFER_SIZE (in_buf), GST_PAD_CAPS (trans->srcpad), |
|
1218 &outsize)) { |
|
1219 goto unknown_size; |
|
1220 } |
|
1221 /* never discard this buffer, we need it for storing the output */ |
|
1222 discard = FALSE; |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 oldcaps = GST_PAD_CAPS (trans->srcpad); |
|
1227 |
922 if (bclass->prepare_output_buffer) { |
1228 if (bclass->prepare_output_buffer) { |
|
1229 GST_DEBUG_OBJECT (trans, |
|
1230 "calling prepare buffer with caps %" GST_PTR_FORMAT, oldcaps); |
923 ret = |
1231 ret = |
924 bclass->prepare_output_buffer (trans, in_buf, out_size, out_caps, |
1232 bclass->prepare_output_buffer (trans, in_buf, outsize, oldcaps, |
925 out_buf); |
1233 out_buf); |
926 if (ret != GST_FLOW_OK) |
1234 |
927 goto done; |
1235 /* get a new ref to the srcpad caps, the prepare_output_buffer function can |
928 |
1236 * update the pad caps if it wants */ |
929 /* decrease refcount again if vmethod returned refcounted in_buf. This |
1237 oldcaps = GST_PAD_CAPS (trans->srcpad); |
|
1238 |
|
1239 /* FIXME 0.11: |
|
1240 * decrease refcount again if vmethod returned refcounted in_buf. This |
930 * is because we need to make sure that the buffer is writable for the |
1241 * is because we need to make sure that the buffer is writable for the |
931 * in_place transform. The docs of the vmethod say that you should return |
1242 * in_place transform. The docs of the vmethod say that you should return |
932 * a reffed inbuf, which is exactly what we don't want :), oh well.. */ |
1243 * a reffed inbuf, which is exactly what we don't want :), oh well.. */ |
933 if (in_buf == *out_buf) |
1244 if (in_buf == *out_buf) |
934 gst_buffer_unref (in_buf); |
1245 gst_buffer_unref (in_buf); |
935 } |
1246 |
936 |
1247 /* never discard the buffer from the prepare_buffer method */ |
937 /* See if we want to prepare the buffer for in place output */ |
1248 if (*out_buf != NULL) |
938 if (*out_buf == NULL && GST_BUFFER_SIZE (in_buf) == out_size |
1249 discard = FALSE; |
939 && bclass->transform_ip) { |
1250 } |
940 if (gst_buffer_is_writable (in_buf)) { |
1251 |
941 if (trans->have_same_caps) { |
1252 if (ret != GST_FLOW_OK) |
942 /* Input buffer is already writable and caps are the same, return input as |
1253 goto alloc_failed; |
943 * output buffer. We don't take an additional ref since that would make the |
1254 |
944 * output buffer not writable anymore. Caller should be prepared to deal |
1255 if (*out_buf == NULL) { |
945 * with proper refcounting of input/output buffers. */ |
1256 GST_DEBUG_OBJECT (trans, "doing alloc with caps %" GST_PTR_FORMAT, oldcaps); |
946 *out_buf = in_buf; |
1257 |
947 GST_LOG_OBJECT (trans, "reuse input buffer"); |
1258 ret = gst_pad_alloc_buffer (trans->srcpad, |
|
1259 GST_BUFFER_OFFSET (in_buf), outsize, oldcaps, out_buf); |
|
1260 if (ret != GST_FLOW_OK) |
|
1261 goto alloc_failed; |
|
1262 } |
|
1263 |
|
1264 /* must always have a buffer by now */ |
|
1265 if (*out_buf == NULL) |
|
1266 goto no_buffer; |
|
1267 |
|
1268 /* check if we got different caps on this new output buffer */ |
|
1269 newcaps = GST_BUFFER_CAPS (*out_buf); |
|
1270 newsize = GST_BUFFER_SIZE (*out_buf); |
|
1271 if (!gst_caps_is_equal (newcaps, oldcaps)) { |
|
1272 GstCaps *othercaps; |
|
1273 gboolean can_convert; |
|
1274 |
|
1275 GST_DEBUG_OBJECT (trans, "received new caps %" GST_PTR_FORMAT, newcaps); |
|
1276 |
|
1277 incaps = GST_PAD_CAPS (trans->sinkpad); |
|
1278 |
|
1279 /* it's possible that the buffer we got is of the wrong size, get the |
|
1280 * expected size here, we will check the size if we are going to use the |
|
1281 * buffer later on. */ |
|
1282 gst_base_transform_transform_size (trans, |
|
1283 GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &expsize); |
|
1284 |
|
1285 /* check if we can convert the current incaps to the new target caps */ |
|
1286 can_convert = |
|
1287 gst_base_transform_can_transform (trans, trans->sinkpad, incaps, |
|
1288 newcaps); |
|
1289 |
|
1290 if (can_convert) { |
|
1291 GST_DEBUG_OBJECT (trans, "reconfigure transform for current buffer"); |
|
1292 /* caps not empty, try to renegotiate to the new format */ |
|
1293 if (!gst_base_transform_configure_caps (trans, incaps, newcaps)) { |
|
1294 /* not sure we need to fail hard here, we can simply continue our |
|
1295 * conversion with what we negotiated before */ |
|
1296 goto failed_configure; |
|
1297 } |
|
1298 /* new format configure, and use the new output buffer */ |
|
1299 gst_pad_set_caps (trans->srcpad, newcaps); |
|
1300 discard = FALSE; |
|
1301 /* if we got a buffer of the wrong size, discard it now and make sure we |
|
1302 * allocate a propertly sized buffer later. */ |
|
1303 if (newsize != expsize) { |
|
1304 if (in_buf != *out_buf) |
|
1305 gst_buffer_unref (*out_buf); |
|
1306 *out_buf = NULL; |
|
1307 } |
|
1308 outsize = expsize; |
|
1309 } else { |
|
1310 GST_DEBUG_OBJECT (trans, "cannot perform transform on current buffer"); |
|
1311 |
|
1312 /* we cannot convert the current buffer but we might be able to suggest a |
|
1313 * new format upstream, try to find what the best format is. */ |
|
1314 othercaps = |
|
1315 gst_base_transform_find_transform (trans, trans->srcpad, newcaps); |
|
1316 |
|
1317 if (!othercaps) { |
|
1318 GST_DEBUG_OBJECT (trans, "incompatible caps, ignoring"); |
|
1319 /* we received caps that we cannot transform. Upstream is behaving badly |
|
1320 * because it should have checked if we could handle these caps. We can |
|
1321 * simply ignore these caps and produce a buffer with our original caps. */ |
948 } else { |
1322 } else { |
949 /* Writable buffer, but need to change caps => subbuffer */ |
1323 guint size_suggest; |
950 *out_buf = gst_buffer_create_sub (in_buf, 0, GST_BUFFER_SIZE (in_buf)); |
1324 |
951 gst_caps_replace (&GST_BUFFER_CAPS (*out_buf), out_caps); |
1325 GST_DEBUG_OBJECT (trans, "getting size of suggestion"); |
952 GST_LOG_OBJECT (trans, "created sub-buffer of input buffer"); |
1326 |
|
1327 /* not a subset, we have a new upstream suggestion, remember it and |
|
1328 * allocate a default buffer. First we try to convert the size */ |
|
1329 if (gst_base_transform_transform_size (trans, |
|
1330 GST_PAD_SRC, newcaps, expsize, othercaps, &size_suggest)) { |
|
1331 |
|
1332 /* ok, remember the suggestions now */ |
|
1333 GST_DEBUG_OBJECT (trans, |
|
1334 "storing new caps and size suggestion of %u and %" GST_PTR_FORMAT, |
|
1335 size_suggest, othercaps); |
|
1336 |
|
1337 GST_OBJECT_LOCK (trans->sinkpad); |
|
1338 if (priv->sink_suggest) |
|
1339 gst_caps_unref (priv->sink_suggest); |
|
1340 priv->sink_suggest = gst_caps_ref (othercaps); |
|
1341 priv->size_suggest = size_suggest; |
|
1342 trans->priv->suggest_pending = TRUE; |
|
1343 GST_OBJECT_UNLOCK (trans->sinkpad); |
|
1344 } |
|
1345 gst_caps_unref (othercaps); |
953 } |
1346 } |
954 /* we are done now */ |
1347 if (in_buf != *out_buf) |
955 goto done; |
1348 gst_buffer_unref (*out_buf); |
|
1349 *out_buf = NULL; |
|
1350 } |
|
1351 } |
|
1352 |
|
1353 if (*out_buf == NULL) { |
|
1354 if (!discard) { |
|
1355 GST_DEBUG_OBJECT (trans, "make default output buffer of size %d", |
|
1356 outsize); |
|
1357 /* no valid buffer yet, make one */ |
|
1358 *out_buf = gst_buffer_new_and_alloc (outsize); |
|
1359 gst_buffer_copy_metadata (*out_buf, in_buf, |
|
1360 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS); |
956 } else { |
1361 } else { |
957 /* Make a writable buffer below and copy the data */ |
1362 GST_DEBUG_OBJECT (trans, "reuse input buffer"); |
958 copy_inbuf = TRUE; |
1363 *out_buf = in_buf; |
959 GST_LOG_OBJECT (trans, "need to copy input buffer to new output buffer"); |
|
960 } |
1364 } |
961 } |
1365 } else { |
962 |
1366 if (trans->passthrough && in_buf != *out_buf) { |
963 if (*out_buf == NULL) { |
1367 /* we are asked to perform a passthrough transform but the input and |
964 /* Sub-class didn't already provide a buffer for us. Make one */ |
1368 * output buffers are different. We have to discard the output buffer and |
965 ret = gst_pad_alloc_buffer (trans->srcpad, |
1369 * reuse the input buffer. */ |
966 GST_BUFFER_OFFSET (in_buf), out_size, out_caps, out_buf); |
1370 GST_DEBUG_OBJECT (trans, "passthrough but different buffers"); |
967 if (ret != GST_FLOW_OK || *out_buf == NULL) |
1371 discard = TRUE; |
968 goto done; |
1372 } |
969 |
1373 if (discard) { |
970 /* allocated buffer could be of different caps than what we requested */ |
1374 GST_DEBUG_OBJECT (trans, "discard buffer, reuse input buffer"); |
971 if (G_UNLIKELY (!gst_caps_is_equal (out_caps, GST_BUFFER_CAPS (*out_buf)))) { |
|
972 /* FIXME, it is possible we can reconfigure the transform with new caps at this |
|
973 * point but for now we just create a buffer ourselves */ |
|
974 gst_buffer_unref (*out_buf); |
1375 gst_buffer_unref (*out_buf); |
975 *out_buf = gst_buffer_new_and_alloc (out_size); |
1376 *out_buf = in_buf; |
976 gst_buffer_set_caps (*out_buf, out_caps); |
1377 } else { |
|
1378 GST_DEBUG_OBJECT (trans, "using allocated buffer"); |
|
1379 gst_buffer_copy_metadata (*out_buf, in_buf, |
|
1380 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS); |
977 } |
1381 } |
978 } |
1382 } |
979 |
1383 gst_buffer_set_caps (*out_buf, GST_PAD_CAPS (trans->srcpad)); |
980 /* If the output buffer metadata is modifiable, copy timestamps and |
1384 |
981 * buffer flags */ |
1385 /* clear the GAP flag when the subclass does not understand it */ |
982 if (*out_buf != in_buf && gst_buffer_is_metadata_writable (*out_buf)) { |
1386 if (!trans->priv->gap_aware) |
983 |
1387 GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP); |
984 if (copy_inbuf && gst_buffer_is_writable (*out_buf)) |
|
985 memcpy (GST_BUFFER_DATA (*out_buf), GST_BUFFER_DATA (in_buf), out_size); |
|
986 |
|
987 gst_buffer_copy_metadata (*out_buf, in_buf, |
|
988 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS); |
|
989 |
|
990 /* Unset the GAP flag if the element is _not_ GAP aware. Otherwise |
|
991 * it might create an output buffer that does not contain neutral data |
|
992 * but still has the GAP flag on it! */ |
|
993 if (!trans->priv->gap_aware) |
|
994 GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP); |
|
995 } |
|
996 |
|
997 done: |
|
998 if (out_caps) |
|
999 gst_caps_unref (out_caps); |
|
1000 |
|
1001 trans->delay_configure = FALSE; |
|
1002 |
1388 |
1003 return ret; |
1389 return ret; |
|
1390 |
|
1391 /* ERRORS */ |
|
1392 alloc_failed: |
|
1393 { |
|
1394 GST_WARNING_OBJECT (trans, "pad-alloc failed: %s", gst_flow_get_name (ret)); |
|
1395 return ret; |
|
1396 } |
|
1397 no_buffer: |
|
1398 { |
|
1399 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED, |
|
1400 ("Sub-class failed to provide an output buffer"), (NULL)); |
|
1401 return GST_FLOW_ERROR; |
|
1402 } |
|
1403 unknown_size: |
|
1404 { |
|
1405 GST_ERROR_OBJECT (trans, "unknown output size"); |
|
1406 return GST_FLOW_ERROR; |
|
1407 } |
|
1408 failed_configure: |
|
1409 { |
|
1410 GST_WARNING_OBJECT (trans, "failed to configure caps"); |
|
1411 return GST_FLOW_NOT_NEGOTIATED; |
|
1412 } |
1004 } |
1413 } |
1005 |
1414 |
1006 /* Given @caps calcultate the size of one unit. |
1415 /* Given @caps calcultate the size of one unit. |
1007 * |
1416 * |
1008 * For video caps, this is the size of one frame (and thus one buffer). |
1417 * For video caps, this is the size of one frame (and thus one buffer). |
1068 static GstFlowReturn |
1483 static GstFlowReturn |
1069 gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size, |
1484 gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size, |
1070 GstCaps * caps, GstBuffer ** buf) |
1485 GstCaps * caps, GstBuffer ** buf) |
1071 { |
1486 { |
1072 GstBaseTransform *trans; |
1487 GstBaseTransform *trans; |
|
1488 GstBaseTransformPrivate *priv; |
1073 GstFlowReturn res; |
1489 GstFlowReturn res; |
1074 guint new_size; |
1490 gboolean proxy, suggest, same_caps; |
1075 gboolean issinkcaps = TRUE; |
1491 GstCaps *sink_suggest; |
|
1492 guint size_suggest; |
1076 |
1493 |
1077 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad)); |
1494 trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad)); |
1078 |
1495 priv = trans->priv; |
1079 /* we cannot run this when we are transforming data and as such doing |
1496 |
1080 * another negotiation in the transform method. */ |
1497 GST_DEBUG_OBJECT (pad, "alloc with caps %" GST_PTR_FORMAT ", size %u", caps, |
1081 g_mutex_lock (trans->transform_lock); |
1498 size); |
1082 |
1499 |
|
1500 /* if the code below does not come up with a better buffer, we will return _OK |
|
1501 * and an empty buffer. This will trigger the core to allocate a buffer with |
|
1502 * given input size and caps. */ |
1083 *buf = NULL; |
1503 *buf = NULL; |
1084 |
1504 res = GST_FLOW_OK; |
1085 GST_DEBUG_OBJECT (trans, "allocating a buffer of size %d ...", size); |
1505 |
1086 if (offset == GST_BUFFER_OFFSET_NONE) |
1506 /* we remember our previous alloc request to quickly see if we can proxy or |
1087 GST_DEBUG_OBJECT (trans, "... and offset NONE"); |
1507 * not. We skip this check if we have a pending suggestion. */ |
1088 else |
1508 GST_OBJECT_LOCK (pad); |
1089 GST_DEBUG_OBJECT (trans, "... and offset %" G_GUINT64_FORMAT, offset); |
1509 same_caps = !priv->suggest_pending && caps && |
1090 |
1510 gst_caps_is_equal (priv->sink_alloc, caps); |
1091 /* if have_same_caps was previously set to TRUE we need to double check if it |
1511 GST_OBJECT_UNLOCK (pad); |
1092 * hasn't changed */ |
1512 |
1093 if (trans->have_same_caps) { |
1513 if (same_caps) { |
1094 GstCaps *sinkcaps; |
1514 /* we have seen this before, see below if we need to proxy */ |
1095 |
1515 GST_DEBUG_OBJECT (trans, "have old caps"); |
1096 GST_OBJECT_LOCK (trans->sinkpad); |
1516 sink_suggest = caps; |
1097 sinkcaps = GST_PAD_CAPS (trans->sinkpad); |
1517 size_suggest = size; |
1098 issinkcaps = sinkcaps && (gst_caps_is_equal (sinkcaps, caps)); |
1518 suggest = FALSE; |
1099 GST_OBJECT_UNLOCK (trans->sinkpad); |
|
1100 } |
|
1101 |
|
1102 /* before any buffers are pushed, have_same_caps is TRUE; allocating can trigger |
|
1103 * a renegotiation and change that to FALSE */ |
|
1104 |
|
1105 /* bilboed: This seems wrong, from all debug logs, have_same_caps is |
|
1106 * initialized to FALSE */ |
|
1107 |
|
1108 /* checking against trans->have_same_caps is not enough !! It should also |
|
1109 * check to see if the requested caps are equal to the sink caps */ |
|
1110 if (trans->have_same_caps && issinkcaps) { |
|
1111 /* request a buffer with the same caps */ |
|
1112 GST_DEBUG_OBJECT (trans, "requesting buffer with same caps, size %d", size); |
|
1113 |
|
1114 res = |
|
1115 gst_pad_alloc_buffer_and_set_caps (trans->srcpad, offset, size, caps, |
|
1116 buf); |
|
1117 } else { |
1519 } else { |
1118 /* if we are configured, request a buffer with the src caps */ |
1520 GstCaps *temp; |
1119 GstCaps *srccaps; |
1521 const GstCaps *templ; |
1120 GstCaps *sinkcaps; |
1522 gboolean empty; |
1121 gboolean configured; |
1523 |
1122 |
1524 GST_DEBUG_OBJECT (trans, "new format %" GST_PTR_FORMAT, caps); |
1123 /* take lock, peek if the caps are ok */ |
1525 |
1124 GST_OBJECT_LOCK (trans->sinkpad); |
1526 /* if we have a suggestion, pretend we got these as input */ |
1125 sinkcaps = GST_PAD_CAPS (trans->sinkpad); |
1527 GST_OBJECT_LOCK (pad); |
1126 configured = (sinkcaps == NULL || gst_caps_is_equal (sinkcaps, caps)); |
1528 if ((priv->sink_suggest && !gst_caps_is_equal (caps, priv->sink_suggest))) { |
1127 GST_OBJECT_UNLOCK (trans->sinkpad); |
1529 sink_suggest = gst_caps_ref (priv->sink_suggest); |
1128 if (!configured) |
1530 size_suggest = priv->size_suggest; |
1129 goto not_configured; |
1531 GST_DEBUG_OBJECT (trans, "have suggestion %" GST_PTR_FORMAT, |
1130 |
1532 sink_suggest); |
1131 /* take lock on srcpad to grab the caps, caps can change when pushing a |
1533 /* suggest is TRUE when we have a custom suggestion pending that we need |
1132 * buffer. */ |
1534 * to unref later. */ |
1133 GST_OBJECT_LOCK (trans->srcpad); |
1535 suggest = TRUE; |
1134 if ((srccaps = GST_PAD_CAPS (trans->srcpad))) |
1536 } else { |
1135 gst_caps_ref (srccaps); |
1537 GST_DEBUG_OBJECT (trans, "using caps %" GST_PTR_FORMAT, caps); |
1136 GST_OBJECT_UNLOCK (trans->srcpad); |
1538 sink_suggest = caps; |
1137 if (!srccaps) |
1539 size_suggest = size; |
1138 goto not_configured; |
1540 suggest = FALSE; |
1139 |
|
1140 GST_DEBUG_OBJECT (trans, "calling transform_size"); |
|
1141 if (!gst_base_transform_transform_size (trans, |
|
1142 GST_PAD_DIRECTION (pad), caps, size, srccaps, &new_size)) { |
|
1143 gst_caps_unref (srccaps); |
|
1144 goto unknown_size; |
|
1145 } |
1541 } |
1146 |
1542 priv->suggest_pending = FALSE; |
1147 res = |
1543 GST_OBJECT_UNLOCK (pad); |
1148 gst_pad_alloc_buffer_and_set_caps (trans->srcpad, offset, new_size, |
1544 |
1149 srccaps, buf); |
1545 /* check if we actually handle this format on the sinkpad */ |
1150 |
1546 if (sink_suggest) { |
1151 gst_caps_unref (srccaps); |
1547 templ = gst_pad_get_pad_template_caps (pad); |
1152 } |
1548 temp = gst_caps_intersect (sink_suggest, templ); |
1153 |
1549 |
1154 if (res == GST_FLOW_OK && !trans->have_same_caps) { |
1550 empty = gst_caps_is_empty (temp); |
1155 /* note that we might have had same caps before, but calling the |
1551 gst_caps_unref (temp); |
1156 alloc_buffer caused setcaps to switch us out of in_place -- in any case |
1552 |
1157 the alloc_buffer served to transmit caps information but we can't use the |
1553 if (empty) |
1158 buffer. fall through and allocate a buffer corresponding to our sink |
1554 goto not_supported; |
1159 caps, if any */ |
|
1160 GstCaps *sinkcaps; |
|
1161 GstCaps *srccaps; |
|
1162 |
|
1163 GST_OBJECT_LOCK (trans->sinkpad); |
|
1164 if ((sinkcaps = GST_PAD_CAPS (trans->sinkpad))) |
|
1165 gst_caps_ref (sinkcaps); |
|
1166 GST_OBJECT_UNLOCK (trans->sinkpad); |
|
1167 if (!sinkcaps) |
|
1168 goto not_configured; |
|
1169 |
|
1170 GST_OBJECT_LOCK (trans->srcpad); |
|
1171 if ((srccaps = GST_PAD_CAPS (trans->srcpad))) |
|
1172 gst_caps_ref (srccaps); |
|
1173 GST_OBJECT_UNLOCK (trans->srcpad); |
|
1174 if (!srccaps) { |
|
1175 gst_caps_unref (sinkcaps); |
|
1176 goto not_configured; |
|
1177 } |
1555 } |
1178 |
1556 |
1179 if (!gst_base_transform_transform_size (trans, |
1557 /* find the best format for the other side here we decide if we will proxy |
1180 GST_PAD_DIRECTION (trans->srcpad), srccaps, GST_BUFFER_SIZE (*buf), |
1558 * the caps or not. */ |
1181 sinkcaps, &new_size)) { |
1559 if (sink_suggest == NULL) { |
1182 gst_caps_unref (srccaps); |
1560 /* always proxy when the caps are NULL. When this is a new format, see if |
1183 gst_caps_unref (sinkcaps); |
1561 * we can proxy it downstream */ |
1184 goto unknown_size; |
1562 GST_DEBUG_OBJECT (trans, "null caps, marking for proxy"); |
|
1563 priv->proxy_alloc = TRUE; |
|
1564 } else { |
|
1565 GstCaps *othercaps; |
|
1566 |
|
1567 /* we have a new format, see what we need to proxy to */ |
|
1568 othercaps = gst_base_transform_find_transform (trans, pad, sink_suggest); |
|
1569 if (!othercaps || gst_caps_is_empty (othercaps)) { |
|
1570 /* no transform possible, we certainly can't proxy */ |
|
1571 GST_DEBUG_OBJECT (trans, "can't find transform, disable proxy"); |
|
1572 priv->proxy_alloc = FALSE; |
|
1573 } else { |
|
1574 /* we transformed into something */ |
|
1575 if (gst_caps_is_equal (caps, othercaps)) { |
|
1576 GST_DEBUG_OBJECT (trans, |
|
1577 "best caps same as input, marking for proxy"); |
|
1578 priv->proxy_alloc = TRUE; |
|
1579 } else { |
|
1580 GST_DEBUG_OBJECT (trans, |
|
1581 "best caps different from input, disable proxy"); |
|
1582 priv->proxy_alloc = FALSE; |
|
1583 } |
|
1584 } |
|
1585 if (othercaps) |
|
1586 gst_caps_unref (othercaps); |
1185 } |
1587 } |
1186 /* don't need the caps anymore now */ |
1588 } |
1187 gst_caps_unref (srccaps); |
1589 /* remember the new caps */ |
1188 |
1590 GST_OBJECT_LOCK (pad); |
1189 gst_buffer_unref (*buf); |
1591 gst_caps_replace (&priv->sink_alloc, sink_suggest); |
1190 |
1592 GST_OBJECT_UNLOCK (pad); |
1191 *buf = gst_buffer_new_and_alloc (new_size); |
1593 |
1192 GST_BUFFER_OFFSET (*buf) = offset; |
1594 proxy = priv->proxy_alloc; |
1193 /* set caps, gives away the ref */ |
1595 GST_DEBUG_OBJECT (trans, "doing default alloc, proxy %d", proxy); |
1194 GST_BUFFER_CAPS (*buf) = sinkcaps; |
1596 |
1195 |
1597 /* we only want to proxy if we have no suggestion pending, FIXME */ |
1196 res = GST_FLOW_OK; |
1598 if (proxy && !suggest) { |
1197 } |
1599 GstCaps *newcaps; |
1198 |
1600 |
1199 done: |
1601 GST_DEBUG_OBJECT (trans, "proxy buffer-alloc with caps %" GST_PTR_FORMAT |
1200 g_mutex_unlock (trans->transform_lock); |
1602 ", size %u", caps, size); |
|
1603 |
|
1604 /* we always proxy the input caps, never the suggestion. The reason is that |
|
1605 * We don't yet handle the caps of renegotiation in here. FIXME */ |
|
1606 res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf); |
|
1607 if (res != GST_FLOW_OK) |
|
1608 goto alloc_failed; |
|
1609 |
|
1610 /* check if the caps changed */ |
|
1611 newcaps = GST_BUFFER_CAPS (*buf); |
|
1612 |
|
1613 GST_DEBUG_OBJECT (trans, "got caps %" GST_PTR_FORMAT, newcaps); |
|
1614 |
|
1615 if (!gst_caps_is_equal (newcaps, caps)) { |
|
1616 GST_DEBUG_OBJECT (trans, "caps are new"); |
|
1617 /* we have new caps, see if we can proxy downstream */ |
|
1618 if (gst_pad_peer_accept_caps (pad, newcaps)) { |
|
1619 /* peer accepts the caps, return a buffer in this format */ |
|
1620 GST_DEBUG_OBJECT (trans, "peer accepted new caps"); |
|
1621 /* remember the format */ |
|
1622 GST_OBJECT_LOCK (pad); |
|
1623 gst_caps_replace (&priv->sink_alloc, newcaps); |
|
1624 GST_OBJECT_UNLOCK (pad); |
|
1625 } else { |
|
1626 GST_DEBUG_OBJECT (trans, "peer did not accept new caps"); |
|
1627 /* peer does not accept the caps, free the buffer we received and |
|
1628 * create a buffer of the requested format by the default handler. */ |
|
1629 gst_buffer_unref (*buf); |
|
1630 *buf = NULL; |
|
1631 } |
|
1632 } else { |
|
1633 GST_DEBUG_OBJECT (trans, "received required caps from peer"); |
|
1634 } |
|
1635 } |
|
1636 |
|
1637 if (suggest) { |
|
1638 /* there was a custom suggestion, create a buffer of this format and return |
|
1639 * it. Note that this format */ |
|
1640 *buf = gst_buffer_new_and_alloc (size_suggest); |
|
1641 GST_DEBUG_OBJECT (trans, |
|
1642 "doing suggestion of size %u, caps %" GST_PTR_FORMAT, size_suggest, |
|
1643 sink_suggest); |
|
1644 GST_BUFFER_CAPS (*buf) = sink_suggest; |
|
1645 } |
|
1646 |
1201 gst_object_unref (trans); |
1647 gst_object_unref (trans); |
1202 |
1648 |
1203 return res; |
1649 return res; |
1204 |
1650 |
1205 not_configured: |
1651 /* ERRORS */ |
|
1652 alloc_failed: |
1206 { |
1653 { |
1207 /* let the default allocator handle it... */ |
1654 GST_DEBUG_OBJECT (trans, "pad alloc failed: %s", gst_flow_get_name (res)); |
1208 GST_DEBUG_OBJECT (trans, "not configured"); |
1655 if (suggest) |
1209 gst_buffer_replace (buf, NULL); |
1656 gst_caps_unref (sink_suggest); |
1210 if (trans->passthrough) { |
1657 gst_object_unref (trans); |
1211 /* ...by calling alloc_buffer without setting caps on the src pad, which |
1658 return res; |
1212 * will force negotiation in the chain function. */ |
1659 } |
1213 res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf); |
1660 not_supported: |
1214 } else { |
|
1215 /* ...by letting the default handler create a buffer */ |
|
1216 res = GST_FLOW_OK; |
|
1217 } |
|
1218 goto done; |
|
1219 } |
|
1220 unknown_size: |
|
1221 { |
1661 { |
1222 /* let the default allocator handle it... */ |
1662 GST_DEBUG_OBJECT (trans, "pad alloc with unsupported caps"); |
1223 GST_DEBUG_OBJECT (trans, "unknown size"); |
1663 if (suggest) |
1224 gst_buffer_replace (buf, NULL); |
1664 gst_caps_unref (sink_suggest); |
1225 if (trans->passthrough) { |
1665 gst_object_unref (trans); |
1226 /* ...by calling alloc_buffer without setting caps on the src pad, which |
1666 return GST_FLOW_NOT_NEGOTIATED; |
1227 * will force negotiation in the chain function. */ |
|
1228 res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf); |
|
1229 } else { |
|
1230 /* ...by letting the default handler create a buffer */ |
|
1231 res = GST_FLOW_OK; |
|
1232 } |
|
1233 goto done; |
|
1234 } |
1667 } |
1235 } |
1668 } |
1236 |
1669 |
1237 static gboolean |
1670 static gboolean |
1238 gst_base_transform_sink_event (GstPad * pad, GstEvent * event) |
1671 gst_base_transform_sink_event (GstPad * pad, GstEvent * event) |
1426 goto skip; |
1885 goto skip; |
1427 } |
1886 } |
1428 } |
1887 } |
1429 |
1888 |
1430 no_qos: |
1889 no_qos: |
|
1890 |
|
1891 /* first try to allocate an output buffer based on the currently negotiated |
|
1892 * format. While we call pad-alloc we could renegotiate the srcpad format or |
|
1893 * have a new suggestion for upstream buffer-alloc. |
|
1894 * In any case, outbuf will contain a buffer suitable for doing the configured |
|
1895 * transform after this function. */ |
|
1896 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, outbuf); |
|
1897 if (G_UNLIKELY (ret != GST_FLOW_OK)) |
|
1898 goto no_buffer; |
|
1899 |
|
1900 /* now perform the needed transform */ |
1431 if (trans->passthrough) { |
1901 if (trans->passthrough) { |
1432 /* In passthrough mode, give transform_ip a look at the |
1902 /* In passthrough mode, give transform_ip a look at the |
1433 * buffer, without making it writable, or just push the |
1903 * buffer, without making it writable, or just push the |
1434 * data through */ |
1904 * data through */ |
1435 GST_LOG_OBJECT (trans, "element is in passthrough mode"); |
1905 if (bclass->transform_ip) { |
1436 |
1906 GST_DEBUG_OBJECT (trans, "doing passthrough transform"); |
1437 if (bclass->transform_ip) |
1907 ret = bclass->transform_ip (trans, *outbuf); |
1438 ret = bclass->transform_ip (trans, inbuf); |
1908 } else { |
1439 |
1909 GST_DEBUG_OBJECT (trans, "element is in passthrough"); |
1440 *outbuf = inbuf; |
1910 } |
1441 |
|
1442 goto done; |
|
1443 } |
|
1444 |
|
1445 want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place; |
|
1446 *outbuf = NULL; |
|
1447 |
|
1448 if (want_in_place) { |
|
1449 /* If want_in_place is TRUE, we may need to prepare a new output buffer |
|
1450 * Sub-classes can implement a prepare_output_buffer function as they |
|
1451 * wish. */ |
|
1452 GST_LOG_OBJECT (trans, "doing inplace transform"); |
|
1453 |
|
1454 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, |
|
1455 GST_BUFFER_SIZE (inbuf), GST_PAD_CAPS (trans->srcpad), outbuf); |
|
1456 if (G_UNLIKELY (ret != GST_FLOW_OK)) |
|
1457 goto no_buffer; |
|
1458 |
|
1459 ret = bclass->transform_ip (trans, *outbuf); |
|
1460 |
|
1461 } else { |
1911 } else { |
1462 GST_LOG_OBJECT (trans, "doing non-inplace transform"); |
1912 want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place; |
1463 |
1913 |
1464 /* not transforming inplace, figure out the output size */ |
1914 if (want_in_place) { |
1465 if (trans->always_in_place) { |
1915 GST_DEBUG_OBJECT (trans, "doing inplace transform"); |
1466 out_size = GST_BUFFER_SIZE (inbuf); |
1916 |
|
1917 if (inbuf != *outbuf) { |
|
1918 /* different buffers, copy the input to the output first, we then do an |
|
1919 * in-place transform on the output buffer. */ |
|
1920 memcpy (GST_BUFFER_DATA (*outbuf), GST_BUFFER_DATA (inbuf), |
|
1921 GST_BUFFER_SIZE (inbuf)); |
|
1922 } |
|
1923 ret = bclass->transform_ip (trans, *outbuf); |
1467 } else { |
1924 } else { |
1468 if (!gst_base_transform_transform_size (trans, |
1925 GST_DEBUG_OBJECT (trans, "doing non-inplace transform"); |
1469 GST_PAD_DIRECTION (trans->sinkpad), GST_PAD_CAPS (trans->sinkpad), |
1926 |
1470 GST_BUFFER_SIZE (inbuf), GST_PAD_CAPS (trans->srcpad), |
1927 if (bclass->transform) |
1471 &out_size)) { |
1928 ret = bclass->transform (trans, inbuf, *outbuf); |
1472 /* we have an error */ |
1929 else |
1473 goto no_size; |
1930 ret = GST_FLOW_NOT_SUPPORTED; |
1474 } |
|
1475 } |
1931 } |
1476 |
|
1477 /* no in place transform, get buffer, this might renegotiate. */ |
|
1478 ret = gst_base_transform_prepare_output_buffer (trans, inbuf, out_size, |
|
1479 GST_PAD_CAPS (trans->srcpad), outbuf); |
|
1480 if (ret != GST_FLOW_OK) |
|
1481 goto no_buffer; |
|
1482 |
|
1483 if (bclass->transform) |
|
1484 ret = bclass->transform (trans, inbuf, *outbuf); |
|
1485 else |
|
1486 ret = GST_FLOW_NOT_SUPPORTED; |
|
1487 } |
|
1488 |
|
1489 /* if we got renegotiated we can configure now */ |
|
1490 if (trans->pending_configure) { |
|
1491 gboolean success; |
|
1492 |
|
1493 success = |
|
1494 gst_base_transform_configure_caps (trans, |
|
1495 GST_PAD_CAPS (trans->sinkpad), GST_PAD_CAPS (trans->srcpad)); |
|
1496 |
|
1497 trans->pending_configure = FALSE; |
|
1498 |
|
1499 if (!success) |
|
1500 goto configure_failed; |
|
1501 } |
1932 } |
1502 |
1933 |
1503 skip: |
1934 skip: |
1504 /* only unref input buffer if we allocated a new outbuf buffer */ |
1935 /* only unref input buffer if we allocated a new outbuf buffer */ |
1505 if (*outbuf != inbuf) |
1936 if (*outbuf != inbuf) |
1506 gst_buffer_unref (inbuf); |
1937 gst_buffer_unref (inbuf); |
1507 |
1938 |
1508 done: |
|
1509 return ret; |
1939 return ret; |
1510 |
1940 |
1511 /* ERRORS */ |
1941 /* ERRORS */ |
1512 not_negotiated: |
1942 not_negotiated: |
1513 { |
1943 { |
1514 gst_buffer_unref (inbuf); |
1944 gst_buffer_unref (inbuf); |
1515 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED, |
1945 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED, |
1516 ("not negotiated"), ("not negotiated")); |
1946 ("not negotiated"), ("not negotiated")); |
1517 return GST_FLOW_NOT_NEGOTIATED; |
1947 return GST_FLOW_NOT_NEGOTIATED; |
1518 } |
1948 } |
1519 no_size: |
|
1520 { |
|
1521 gst_buffer_unref (inbuf); |
|
1522 GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED, |
|
1523 ("subclass did not specify output size"), |
|
1524 ("subclass did not specify output size")); |
|
1525 return GST_FLOW_ERROR; |
|
1526 } |
|
1527 no_buffer: |
1949 no_buffer: |
1528 { |
1950 { |
1529 gst_buffer_unref (inbuf); |
1951 gst_buffer_unref (inbuf); |
1530 GST_DEBUG_OBJECT (trans, "could not get buffer from pool: %s", |
1952 GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s", |
1531 gst_flow_get_name (ret)); |
1953 gst_flow_get_name (ret)); |
1532 return ret; |
1954 return ret; |
1533 } |
|
1534 configure_failed: |
|
1535 { |
|
1536 if (*outbuf != inbuf) |
|
1537 gst_buffer_unref (inbuf); |
|
1538 GST_DEBUG_OBJECT (trans, "could not negotiate"); |
|
1539 return GST_FLOW_NOT_NEGOTIATED; |
|
1540 } |
1955 } |
1541 } |
1956 } |
1542 |
1957 |
1543 static gboolean |
1958 static gboolean |
1544 gst_base_transform_check_get_range (GstPad * pad) |
1959 gst_base_transform_check_get_range (GstPad * pad) |
1995 /** |
2436 /** |
1996 * gst_base_transform_set_gap_aware: |
2437 * gst_base_transform_set_gap_aware: |
1997 * @trans: a #GstBaseTransform |
2438 * @trans: a #GstBaseTransform |
1998 * @gap_aware: New state |
2439 * @gap_aware: New state |
1999 * |
2440 * |
2000 * If @gap_aware is %FALSE (as it is by default) subclasses will never get |
2441 * If @gap_aware is %FALSE (the default), output buffers will have the |
2001 * output buffers with the %GST_BUFFER_FLAG_GAP flag set. |
2442 * %GST_BUFFER_FLAG_GAP flag unset. |
2002 * |
2443 * |
2003 * If set to %TRUE elements must handle output buffers with this flag set |
2444 * If set to %TRUE, the element must handle output buffers with this flag set |
2004 * correctly, i.e. they can assume that the buffer contains neutral data |
2445 * correctly, i.e. it can assume that the buffer contains neutral data but must |
2005 * but must unset the flag if the output is no neutral data. |
2446 * unset the flag if the output is no neutral data. |
|
2447 * |
|
2448 * MT safe. |
|
2449 * |
2006 * Since: 0.10.16 |
2450 * Since: 0.10.16 |
2007 * |
|
2008 * MT safe. |
|
2009 */ |
2451 */ |
2010 #ifdef __SYMBIAN32__ |
2452 #ifdef __SYMBIAN32__ |
2011 EXPORT_C |
2453 EXPORT_C |
2012 #endif |
2454 #endif |
2013 |
2455 |
2014 void |
2456 void |
2015 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware) |
2457 gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware) |
2016 { |
2458 { |
2017 g_return_if_fail (trans != NULL); |
2459 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans)); |
2018 |
2460 |
2019 GST_OBJECT_LOCK (trans); |
2461 GST_OBJECT_LOCK (trans); |
2020 trans->priv->gap_aware = gap_aware; |
2462 trans->priv->gap_aware = gap_aware; |
2021 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware); |
2463 GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware); |
2022 GST_OBJECT_UNLOCK (trans); |
2464 GST_OBJECT_UNLOCK (trans); |
2023 } |
2465 } |
|
2466 |
|
2467 /** |
|
2468 * gst_base_transform_suggest: |
|
2469 * @trans: a #GstBaseTransform |
|
2470 * @caps: caps to suggest |
|
2471 * @size: buffer size to suggest |
|
2472 * |
|
2473 * Instructs @trans to suggest new @caps upstream. A copy of @caps will be |
|
2474 * taken. |
|
2475 * |
|
2476 * Since: 0.10.21 |
|
2477 */ |
|
2478 #ifdef __SYMBIAN32__ |
|
2479 EXPORT_C |
|
2480 #endif |
|
2481 |
|
2482 void |
|
2483 gst_base_transform_suggest (GstBaseTransform * trans, GstCaps * caps, |
|
2484 guint size) |
|
2485 { |
|
2486 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans)); |
|
2487 |
|
2488 GST_OBJECT_LOCK (trans->sinkpad); |
|
2489 if (trans->priv->sink_suggest) |
|
2490 gst_caps_unref (trans->priv->sink_suggest); |
|
2491 if (caps) |
|
2492 caps = gst_caps_copy (caps); |
|
2493 trans->priv->sink_suggest = caps; |
|
2494 trans->priv->size_suggest = size; |
|
2495 trans->priv->suggest_pending = TRUE; |
|
2496 GST_DEBUG_OBJECT (trans, "new suggest %" GST_PTR_FORMAT, caps); |
|
2497 GST_OBJECT_UNLOCK (trans->sinkpad); |
|
2498 } |
|
2499 |
|
2500 /** |
|
2501 * gst_base_transform_reconfigure: |
|
2502 * @trans: a #GstBaseTransform |
|
2503 * |
|
2504 * Instructs @trans to renegotiate a new downstream transform on the next |
|
2505 * buffer. This function is typically called after properties on the transform |
|
2506 * were set that influence the output format. |
|
2507 * |
|
2508 * Since: 0.10.21 |
|
2509 */ |
|
2510 #ifdef __SYMBIAN32__ |
|
2511 EXPORT_C |
|
2512 #endif |
|
2513 |
|
2514 void |
|
2515 gst_base_transform_reconfigure (GstBaseTransform * trans) |
|
2516 { |
|
2517 g_return_if_fail (GST_IS_BASE_TRANSFORM (trans)); |
|
2518 |
|
2519 GST_OBJECT_LOCK (trans); |
|
2520 GST_DEBUG_OBJECT (trans, "marking reconfigure"); |
|
2521 trans->priv->reconfigure = TRUE; |
|
2522 gst_caps_replace (&trans->priv->sink_alloc, NULL); |
|
2523 GST_OBJECT_UNLOCK (trans); |
|
2524 } |